如何计算并显示数字复合计数器的递增值,例如\thesubsection
?我是否尝试分别提取和递增“小数”部分,然后将结果重新组合在一起?
\documentclass{article}
\begin{document}
\section{Section}
Next section would be: \the\numexpr\thesection+1
% outputs "2"
\subsection{Subsection}
Next subsection would be: \the\numexpr\thesubsection+1
% prints literary "1.1+1" instead of "1.2"
\end{document}
答案1
你不能使用复合展示原样。您需要提取价值计数器(使用\value
)可能与其不同表示(例如\alph
或\roman
),执行计算(可能使用\numexpr
),然后再次显示合成(使用\the
或\number
)。以下是实现此目的的一些方法:
\documentclass{article}
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\section{Section}
Next section would be: \the\numexpr\value{section}+1\relax \par
Next section would be: \ref{sec:another_section} \par
Next section would be: \stepcounter{section}\thesection\addtocounter{section}{-1}
\subsection{Subsection}
Next subsection would be: \thesection.\the\numexpr\value{subsection}+1\relax \par
Next subsection would be: \ref{sec:another_subsection} \par
Next subsection would be: \stepcounter{subsection}\thesubsection\addtocounter{subsection}{-1}
\subsection{Another subsection}\label{sec:another_subsection}
\section{Another section}\label{sec:another_section}
\end{document}
使用\label
-\ref
就更好了,就是这样。