하지만

하지만

북클래스를 이용하고 있어요. 내 경우에는 일부 장에는 섹션이 포함되어 있고 다른 장에는 섹션이 포함되어 있지 않습니다. 방정식 번호를 다음과 같이 표현하고 싶습니다.

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}

원하는 결과를 얻었어요

하지만

\numberwithing나는 사례 1)과 2) 사이를 수동으로 전환하고 싶지 않습니다. 즉, 명시적으로 command 를 작성하거나 재정의 하고 싶지 않습니다 \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}

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

관련 정보