Estoy usando la clase de libro. En mi caso algunos capítulos contienen secciones y otros no. Me gustaría expresar el número de ecuación de la siguiente manera:
1) Si el capítulo contiene secciones, me gustaría que el número de ecuación fuera así (section.equation)
[aviso:sin capitulo];
2) Si el capítulo no contiene ningún apartado (equation)
.
Por supuesto que me gustaría tener el mismo en el número de referencia. Aquí hay un 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}
obtengo el resultado deseado
PERO
No quiero cambiar manualmente entre los casos 1) y 2), es decir, no quiero escribir \numberwithing
comandos explícitamente ni redefinir \theequation
. Me gustaría que LaTeX lo hiciera en tiempo de compilación:
a) se dé cuenta si el capítulo contiene alguna sanción;
b) en caso afirmativo, la numeración de la ecuación es el caso 1);
c) si no es el caso 2).
Respuesta1
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}
Respuesta2
Con 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}
Respuesta3
Haría lo siguiente (independientemente de que la solicitud sea diferente (lo que en mi opinión hace que se pierda en la navegación del libro):
\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}