我想在报告文档中的方程式中包含章节编号,我在文章文档中找到了此工作代码
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{amssymb, amsmath, amsbsy} %
\usepackage{upgreek} %
\usepackage{cancel} %
\usepackage{mathdots} %
\usepackage{mathrsfs} %
\usepackage{stackrel} %
\usepackage{graphicx}
\usepackage{caption}
\usepackage{chngcntr}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage[a4paper,hmargin=2cm,vmargin={2cm,2.5cm}]{geometry}
%\counterwithin*{equation}{chapter}
\counterwithin*{equation}{section}
\counterwithin*{equation}{subsection}
\makeatletter
\renewcommand\theequation{%
% \ifnum\value{chapter}>0 \thechapter.\else
\ifnum\value{subsection}>0 \thesubsection.\else
\ifnum\value{section}>0 \thesection.\fi\fi%\fi
\arabic{equation}
}
\AtBeginDocument{%
\renewcommand\theHequation{%
% \ifnum\value{chapter}>0 \theHchapter.\else
\ifnum\value{subsection}>0 \theHsubsection.\else
\ifnum\value{section}>0 \theHsection.\fi\fi%\fi
\arabic{equation}
}%
}
\makeatother
\begin{document}
\raggedright
%\chapter{Test}
\section{Test}
\subsection{Test}
\begin{equation}
a = b
\end{equation}
\end{document}
但是,这个未注释的代码(和文档类型改变)不能像我预期的那样工作
\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{amssymb, amsmath, amsbsy} %
\usepackage{upgreek} %
\usepackage{cancel} %
\usepackage{mathdots} %
\usepackage{mathrsfs} %
\usepackage{stackrel} %
\usepackage{graphicx}
\usepackage{caption}
\usepackage{chngcntr}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage[a4paper,hmargin=2cm,vmargin={2cm,2.5cm}]{geometry}
\counterwithin*{equation}{chapter}
\counterwithin*{equation}{section}
\counterwithin*{equation}{subsection}
\makeatletter
\renewcommand\theequation{%
\ifnum\value{chapter}>0 \thechapter.\else
\ifnum\value{subsection}>0 \thesubsection.\else
\ifnum\value{section}>0 \thesection.\fi\fi\fi
\arabic{equation}
}
\AtBeginDocument{%
\renewcommand\theHequation{%
\ifnum\value{chapter}>0 \theHchapter.\else
\ifnum\value{subsection}>0 \theHsubsection.\else
\ifnum\value{section}>0 \theHsection.\fi\fi\fi
\arabic{equation}
}%
}
\makeatother
\begin{document}
\raggedright
\chapter{Test}
\section{Test}
\subsection{Test}
\begin{equation}
a = b
\end{equation}
\end{document}
有什么想法吗?谢谢!
答案1
你的构建\theequation
应该按相反的顺序进行,从最深的层次开始(\thesubsection
),直到\thechapter
:
\documentclass{report}
\usepackage{chngcntr}
\counterwithin*{equation}{chapter}
\counterwithin*{equation}{section}
\counterwithin*{equation}{subsection}
\makeatletter
\renewcommand\theequation{%
\ifnum\value{subsection}>0 \thesubsection.\else
\ifnum\value{section}>0 \thesection.\else
\ifnum\value{chapter}>0 \thechapter.\fi\fi\fi
\arabic{equation}%
}
\makeatother
\newcommand{\insertequation}{%
\begin{equation}
f(x) = ax^2 + bx + c
\end{equation}
}
\begin{document}
\chapter{A chapter}
\insertequation
\section{A section}
\insertequation
\subsection{A subsection}
\insertequation
\insertequation
\section{Another section}
\insertequation
\clearpage
\chapter{Second chapter}
\insertequation
\setcounter{section}{4}
\section{Fifth section}
\insertequation
\setcounter{subsection}{6}
\subsection{Seventh subsection}
\insertequation
\insertequation
\section{Sixth section}
\insertequation
\end{document}