Ich verwende die Buchklasse. In meinem Fall enthalten einige Kapitel Abschnitte und andere nicht. Ich möchte die Gleichungsnummer folgendermaßen ausdrücken:
1) Wenn das Kapitel Abschnitte enthält, möchte ich, dass die Gleichungsnummer wie folgt lautet (section.equation)
[Hinweis:kein Kapitel];
2) Wenn das Kapitel keinen Abschnitt enthält (equation)
.
Das gleiche hätte ich natürlich auch gerne in der Referenznummer. Hier ein 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}
Ich bekomme das gewünschte Ergebnis
ABER
Ich möchte nicht manuell zwischen den Fällen 1) und 2) wechseln, d. h. ich möchte weder explizit \numberwithing
command schreiben noch redefinieren \theequation
. Ich möchte, dass LaTeX dies zur Kompilierungszeit erledigt:
a) festzustellen, ob das Kapitel einen Beschluss enthält;
b) wenn ja, ist die Gleichungsnummerierung Fall 1);
c) wenn nicht, liegt Fall 2 vor.
Antwort1
Etwas wie
\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}
Antwort2
Mit 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}
Antwort3
Ich würde Folgendes tun (unabhängig davon, ob die Anfrage unterschiedlich ist (was meiner Meinung nach dazu führt, dass man sich in der Buchnavigation verliert):
\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}