# \stepcounter is not working in subsection heading

I have defined a new counter as:

\newcounter{lecCounter}
\newcommand{\lecID}{\stepcounter{lecCounter}\thelecCounter}


And I wish to use it as:

\subsection{Lecture \lecID }


But it gives error:

! Missing \endcsname inserted.
\csname\endcsname
l.1 \subsection{Lecture \lecID }

?


If I put Lecture \lecID outside of the \subsection{}, then it compiles.

What's the solution?

• A quick tip: If you indent lines of code by four spaces -- easily accomplished by highlighting the lines in question and clicking on the {} "button" in the ribbon above the edit window -- the site software will pretty-print them automatically. – Mico Dec 22 '14 at 6:28

Don't use \stepcounter in the argument of \subsection. Even if preceding it with \protect in order to avoid the error, this will have adverse effect when \tableofcontents is used. The following example shows it clearly (I used \section, but it's exactly the same with \subsection, provided it goes in the table of contents).

\documentclass{article}

\newcounter{lecCounter}
\newcommand{\lecID}{\protect\stepcounter{lecCounter}\thelecCounter}

\begin{document}

\tableofcontents

\section{Lecture \lecID}

Text.

\end{document}


What you should do is to define a new command:

\newcommand{\lecture}{%
\stepcounter{lecCounter}%
\subsection{Lecture \thelecCounter}%
}


Fine tuning is possible, depending on your needs; for instance if you need to refer to the lecture number rather than to the subsection number. Another possibility would be adding an optional argument for a specific lecture title. Better encoding all this into a command, rather than chasing into the document if a change is needed.

A sample document might be

\documentclass{article}

\newcounter{lecCounter}
\newcommand{\lecture}{%
\stepcounter{lecCounter}%
\subsection{Lecture \thelecCounter}%
}

\begin{document}

\tableofcontents

\section{Group of lectures}

\lecture

Text.

\lecture

Text.

\end{document}


The command \stepcounter is a fragile command, meaning it can lead to errors when put inside of a moving argument. Arguments to \section are moving arguments, since LaTeX will also puts the section name (the argument) in the table of contents and elsewhere. You need to "protect" \stepcounter to make sure it doesn't cause issues as a fragile command. We can either precede \lecID with \protect when it is used or just define \lecID as a robust command through \DeclareRobustCommand. (As Mico noted, we define \lecId with \refstepcounter in case you might need to refer to the lecCounter (\lecCounter) such that it will match up with the respective subsection.):

\DeclareRobustCommand{\lecID}{\refstepcounter{lecCounter}\thelecCounter}


Furthermore, since \lecID is executed every time that it shows up, lecCounter is inappropriately incremented when it shows up elsewhere, such as with \tableofcontents. Thus, we use the optional argument of \subsection to put in only the counter without \stepcounter and \refstepcounter to change the value. LaTeX will therefore only increment lecCounter the first time it sees \subsection

\subsection[Lecture \thelecCounter]{Lecture \lecID }


The issue with fragile commands and moving arguments is explained more thoroughly here: What is the difference between Fragile and Robust commands?.

\documentclass{article}
\usepackage[utf8]{inputenc}

\documentclass{article}
\usepackage[utf8]{inputenc}

\newcounter{lecCounter}
\DeclareRobustCommand{\lecID}{\refstepcounter{lecCounter}\thelecCounter}

\begin{document}

\tableofcontents

\subsection[Lecture \thelecCounter]{Lecture \lecID}

\subsection[Lecture \thelecCounter]{Lecture \lecID}

\end{document}


• Just in case there are any plans to cross-reference the subsection by lecture number, it's preferable to use \refstepcounter instead of \stepcounter. – Mico Dec 22 '14 at 6:24
• This will have peculiar effects if \tableofcontents is used. – egreg Dec 22 '14 at 9:31