为什么这不(https://texblog.org/2013/04/03/reverse-numbering-for-chapters-sections-etc/) 适用于小节吗?
\documentclass[10pt]{scrartcl}
\usepackage{totcount}
\usepackage{blindtext}
\regtotcounter{subsection}
\makeatletter
\renewcommand{\thesubsection}{\number\numexpr\c@subsection@totc-\c@subsection+1\relax}
\makeatother
\begin{document}
\tableofcontents
\section{First Section}
\subsection{First Subsection}
\subsection{Second Subsection}
\subsection{Third Subsection}
\subsection{Fourth Subsection}
\Blindtext
\section{Second Section}
\Blindtext
\section{Third Section}
\Blindtext
\section{Fourth Section}
\Blindtext
\end{document}
我希望是 4、3、2、1
答案1
您需要在新章节开始时(以及在文档结束时)保存子章节的总数。因此您可以进行减法(在运行几次 LaTeX 之后)。
\documentclass{article}
\makeatletter
\AddToHook{cmd/section/before}{\save@subsection}
\AtEndDocument{\save@subsection}
\newcommand{\save@subsection}{%
\immediate\write\@auxout{%
\string\savesubsectionnumber{\arabic{section}}{\arabic{subsection}}%
}%
}
\newcommand{\savesubsectionnumber}[2]{%
\global\@namedef{savedsubsection#1}{#2}%
}
\renewcommand{\thesubsection}{%
\the\numexpr
\ifcsname savedsubsection\arabic{section}\endcsname
\csname savedsubsection\arabic{section}\endcsname+1-
\fi
\value{subsection}%
\relax
}
\makeatother
\begin{document}
\tableofcontents
\section{First Section}
\subsection{First Subsection}
\subsection{Second Subsection}
\subsection{Third Subsection}
\subsection{Fourth Subsection}
\section{Second Section}
\subsection{First Subsection}
\subsection{Second Subsection}
\subsection{Third Subsection}
\end{document}
这个想法是,将和\section
的当前值以一种允许定义由扩展到子节数的节号索引的宏的形式保存在辅助文件中。section
subsection