私は book クラスを使用しています。私の場合、一部の章にはセクションが含まれ、他の章には含まれません。方程式番号を次のように表現したいと思います。
1) 章にセクションが含まれている場合、方程式の番号は次のようになります(section.equation)
[注意:章なし];
2) 章にセクションが含まれていない場合(equation)
。
もちろん、参照番号にも同じものを入れたいと思います。これが MWE です:
\documentclass{book}
\usepackage{amsmath}
\begin{document}
\numberwithin{equation}{section}
\renewcommand{\theequation}{\arabic{section}.\arabic{equation}}
\chapter{First}
\section{First First}
\begin{equation}\label{eq:1}
E=\gamma m
\end{equation}
\section{First Second}
\begin{equation}\label{eq:2}
0=0
\end{equation}
\chapter{Second}
\numberwithin{equation}{chapter}
\renewcommand{\theequation}{\arabic{equation}}
\begin{equation}\label{eq:3}
e^{i\pi}+1=0
\end{equation}
\chapter{Third}
\begin{equation}\label{eq:4}
f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
\end{equation}
\chapter{Last}
\eqref{eq:1}, \eqref{eq:2}, \eqref{eq:3}, \eqref{eq:4}
\end{document}
望んだ結果が得られました
しかし
1) と 2) のケースを手動で切り替えたくありません。つまり、明示的に\numberwithing
コマンドを書いたり、再定義したりしたくありません\theequation
。LaTeX でコンパイル時に実行したいと思います。
a) その章に何らかの行動が含まれているかどうかを認識する。
b) はいの場合、式の番号付けはケース 1 になります。
c) そうでない場合はケース 2) となります。
答え1
何かのようなもの
\documentclass{book}
\usepackage{amsmath}
\begin{document}
\numberwithin{equation}{section}
\renewcommand{\theequation}{\ifnum\value{section}>0 \arabic{section}.\fi\arabic{equation}}
\chapter{First}
\section{First First}
\begin{equation}\label{eq:1}
E=\gamma m
\end{equation}
\section{First Second}
\begin{equation}\label{eq:2}
0=0
\end{equation}
\chapter{Second}
\begin{equation}\label{eq:3}
e^{i\pi}+1=0
\end{equation}
\chapter{Third}
\begin{equation}\label{eq:4}
f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
\end{equation}
\chapter{Last}
\eqref{eq:1}, \eqref{eq:2}, \eqref{eq:3}, \eqref{eq:4}
\end{document}
答え2
とetoolbox
:
\documentclass{book}
\usepackage{mathtools}
\usepackage{etoolbox, chngcntr}
\counterwithin*{equation}{section}}
\renewcommand{\theequation}{\ifnumcomp{\value{section}}{=}{0}{}{\arabic{section}.}\arabic{equation}}
\begin{document}
\chapter{First}
\section{First First}
\begin{equation}\label{eq:1}
E=\gamma m
\end{equation}
\section{First Second}
\begin{equation}\label{eq:2}
0=0
\end{equation}
\chapter{Second}
\begin{equation}\label{eq:3}
e^{i\pi}+1=0
\end{equation}
\chapter{Third}
\begin{equation}\label{eq:4}
f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
\end{equation}
\chapter{Last}
\eqref{eq:1}, \eqref{eq:2}, \eqref{eq:3}, \eqref{eq:4}
\end{document}
答え3
私は次のことを行います(リクエストが異なっていても(私の意見では、本のナビゲーションで失われることになりますが)、
\documentclass{book}
\usepackage{amsmath}
\numberwithin{equation}{section}
\renewcommand{\theequation}{\ifnum\value{section}>0
\thesection-\arabic{equation}%
\else
\thechapter-\arabic{equation}%
\fi}
\begin{document}
\chapter{One}
\section{First section in the first chaper}
\begin{equation}\label{eq:1}
E=\gamma m
\end{equation}
\section{Second section in the first chapter}
\begin{equation}\label{eq:2}
0=0
\end{equation}
\chapter{Two}
\begin{equation}\label{eq:3}
e^{i\pi}+1=0
\end{equation}
\chapter{Three}
\begin{equation}\label{eq:4}
f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
\end{equation}
\section{First section in third chapter}
\begin{equation}\label{eq:5}
E=\gamma m
\end{equation}
\chapter{four}
\eqref{eq:1}, \eqref{eq:2}, \eqref{eq:3}, \eqref{eq:4} and \eqref{eq:5}
\end{document}