3

How do I calculate and display an incremented value of a numeric composite counter, such as \thesubsection? Do I try to extract and increment the "decimal" part separately and then glue the result back together?

\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} 
  • 4
    Next subsection would be: \thesection.\the\numexpr\value{subsection}+1\relax – Marijn Aug 17 '17 at 12:36
  • Obviously you wouldn't use this to refer to the next section or subsection, however. – cfr Aug 17 '17 at 12:59
1

You can't use the composite display as-is. You need to extract the value of the counter (using \value) which might be different from its representation (like \alph or \roman), perform your calculation (perhaps using \numexpr), and display the composition again (with \the or \number). Here are some ways of achieving this:

enter image description here

\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} 

Using \label-\ref is far better, period.

|improve this answer|||||
  • Thanks Werner, for a good summary of methods. Indeed I was particularly interested in subsections and hoped to manipulate its representation, as in the headings where I need them the section and subsection counters are usually wrong. I solved it now by asking titlesec to save extramarks. Your answer was helpful. – user612313 Aug 24 '17 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.