![但](https://rvso.com/image/353371/%E4%BD%86.png)
我正在使用書籍課程。就我而言,有些章節包含章節,而其他章節則不包含。我想用以下方式表達方程編號:
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
command 也不想重新定義\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}