0

I am using two different commands, one allocates the number of section/subsection/subsubsection with a choosable name as argument, and the other one gets the number, depending on the depth of the sectionnumber. When I print the current numbers, everything is according to the actual number. When I use my commands the subsubsection number seems to be wrong. In the following MWE it shows with the subsubsections 1.1.1 to 1.1.3 but the commands always gets 1.1.1. In my actual document it happens at 4.2.1 to 4.2.3 but curiously enough, it always gives me 4.2.2, even when I use the commands in 4.2.1.

The MWE:

\documentclass[12pt,a4paper]{scrartcl}

\newcommand{\setMyNumber}[1]{\newcounter{tocS#1}
\setcounter{tocS#1}{\value{section}}
\newcounter{tocSS#1}
\setcounter{tocSS#1}{\value{subsection}}
\newcounter{tocSSS#1}
\setcounter{tocSSS#1}{\value{subsubsection}}}

\newcommand{\getMyNumber}[1]{
\ifnum\value{tocSSS#1}=0
\ifnum\value{tocSS#1}=0
\arabic{tocS#1}
\else
\arabic{tocS#1}.\arabic{tocSS#1}
\fi
\else
\arabic{tocS#1}.\arabic{tocSS#1}.\arabic{tocSS#1}
\fi
}

\begin{document}
\section{Section 1}
\subsection{Section 1.1}
\subsubsection{Section 1.1.1}
The current number is \arabic{section}.\arabic{subsection}.\arabic{subsubsection}.
\subsubsection{Section 1.1.2}
Now the number is \arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\\
But \setMyNumber{Test} sets the number to \getMyNumber{Test}.
\subsubsection{Section 1.1.3}
Here \setMyNumber{TryAgain} still gives \getMyNumber{TryAgain}.
\subsection{Section 1.2}
At this point the command \setMyNumber{ItWorks} seems to work again: \getMyNumber{ItWorks}
\end{document}
4
  • This is a little unclear. Perhaps you could alter the MWE so that the text within the subsections explains what number you WANT the sub/sub/section to have? For me, the section title numbers are currently 1, 1.1, 1.1.1, 1.1.2, 1.1.3, 1.2, which seems reasonable, but I guess that is not what you are trying to achieve?
    – Fintan
    May 12, 2020 at 13:41
  • 1
    what is the intention of these commands? it seems like you are implementing a version of \label and \ref ? May 12, 2020 at 13:46
  • I suppose \arabic{tocS#1}.\arabic{tocSS#1}.\arabic{tocSS#1} should be\arabic{tocS#1}.\arabic{tocSS#1}.\arabic{tocSSS#1}. Apart from that: Three count-registers get allocated whenever \SetMyNumber is called/carried out. This way you might run out of count-registers quickly. May 12, 2020 at 18:14
  • It actually has a similar use to label and ref, I remember having problems with it in the existing file structure, but I will definitely look at it again. Is the number of latex count-registers limited? That would definitely be a problem to consider.
    – buecki
    May 13, 2020 at 6:25

1 Answer 1

3

There was a missing S in the counter for the subsubsection, so the section counter was repeated by \getMyNumber.

\documentclass[12pt,a4paper]{scrartcl}

\newcommand{\setMyNumber}[1]{%
  \newcounter{tocS#1}%
  \setcounter{tocS#1}{\the\value{section}}%
  \newcounter{tocSS#1}%
  \setcounter{tocSS#1}{\the\value{subsection}}%
  \newcounter{tocSSS#1}%
  \setcounter{tocSSS#1}{\the\value{subsubsection}}%
}

\newcommand{\getMyNumber}[1]{%
\ifnum\value{tocSSS#1}=0
  \ifnum\value{tocSS#1}=0
    \arabic{tocS#1}%
  \else
    \arabic{tocS#1}.\arabic{tocSS#1}%
  \fi
\else
  \arabic{tocS#1}.\arabic{tocSS#1}.\arabic{tocSSS#1}% <- changed, there was a S missing
\fi
}

\begin{document}
\section{Section 1}
\subsection{Section 1.1}
\subsubsection{Section 1.1.1}
The current number is \arabic{section}.\arabic{subsection}.\arabic{subsubsection}.
\subsubsection{Section 1.1.2}
Now the number is \arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\\
But\setMyNumber{Test} sets the number to \getMyNumber{Test}.
\subsubsection{Section 1.1.3}
Here\setMyNumber{TryAgain} still gives \getMyNumber{TryAgain}.
\subsection{Section 1.2}
At this point the command \setMyNumber{ItWorks} seems to work again: \getMyNumber{ItWorks}
\end{document}

Note that I have commented line ends to remove spurious spaces.

But maybe \label and \ref do what you want:

\documentclass[a4paper]{scrartcl}
\begin{document}
\section{Section 1}
\subsection{Section 1.1}
\subsubsection{Section 1.1.1}
The current number is \thesubsubsection.
\subsubsection{Section 1.1.2}\label{Test}
Now the number is \thesubsubsection.\\
And \verb|\label{Test}| refers  to \ref{Test}.
\subsubsection{Section 1.1.3}\label{TryAgain}
Here \verb|\label{TryAgain}| refers to \ref{TryAgain}.
\subsection{Section 1.2}\label{ItWorks}
At this point the command \verb|\label{ItWorks}| seems to work again: \ref{ItWorks}
\end{document}

Run twice to get

enter image description here

2
  • Oh dear, shame on me. I searched for hours for potential mistakes in my file structure but missed this obvious mistake. Thank you. I remember some difficulties with label and ref in the existing file structure, but I will definitely reconsider this. Just out of interest: the \verb prints the command as text? Does it make any difference if you use | or + or any other character?
    – buecki
    May 13, 2020 at 6:22
  • @buecki You can use | or + or any other character for \verb, but no letter and no character which is inside the verbatim text.
    – esdd
    May 13, 2020 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .