НО

НО

Я использую класс книги. В моем случае некоторые главы содержат разделы, а некоторые нет. Я хотел бы выразить номер уравнения следующим образом:

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 делал это во время компиляции:

а) понять, содержит ли глава какие-либо разделы;

б) если да, то нумерация уравнений соответствует случаю 1);

в) если нет, то это случай 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}

[1]: https://i.stack.imgur.com/SQpW3.png

Связанный контент