我正在使用书籍类。在我的例子中,有些章节包含章节,而有些则不包含。我想用以下方式表达方程编号:
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}