MAS

MAS

Estou usando a aula de livro. No meu caso, alguns capítulos contêm seções e outros não. Eu gostaria de expressar o número da equação da seguinte maneira:

1) Se o capítulo contiver seções eu gostaria que o número da equação fosse assim (section.equation)[aviso:nenhum capítulo];

2) Se o capítulo não contiver nenhuma seção (equation).

Claro que gostaria de ter o mesmo no número de referência. Aqui está um 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}

Eu obtenho o resultado desejado

MAS

Não quero alternar manualmente entre os casos 1) e 2), ou seja, não quero escrever explicitamente \numberwithingcommand nem redefinir \theequation. Eu gostaria que o LaTeX fizesse isso em tempo de compilação:

a) perceber se o capítulo contém alguma ação;

b) se sim a numeração da equação é caso 1);

c) caso contrário, é o caso 2).

Responder1

Algo como

\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}

Responder2

Com 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} 

insira a descrição da imagem aqui

Responder3

Eu faria o seguinte (independentemente de a solicitação ser diferente (o que, na minha opinião, leva à perda na navegação do livro):

\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

informação relacionada