5

I've been trying to change the numbering behavior of subsection to use a global counter. I first did:

\newcounter{maincount}
\renewcommand{\thesubsection}{\stepcounter{maincount}\arabic{maincount}}

and got:

! Missing \endcsname inserted.
<to be read again> 
                   \csname\endcsname
l.64 \subsection{Orientation}

I then did it by defining \mysubsection:

\newcounter{maincount}
\renewcommand{\thesubsection}{\arabic{maincount}
\renewcommand{\mysubsection}[1]{\stepcounter{maincount}\subsection{#1}}

And that works fine but requires me to change every instance of \subsection with \mysubsection in the document. Is there any way to decorate a LaTeX command? As in redefining \subsection in a way that is able to call the previous implementation of it. This generates an infinite loop:

\renewcommand{\subsection}[1]{\stepcounter{maincount}\subsection{#1}}
2

3 Answers 3

4

You can copy the meaning of a macro (aka command sequence) to another using \let\newmacro=\oldmacro (the = is optional):

\let\origsubsection\subsection
\renewcommand{\subsection}[1]{\stepcounter{maincount}\origsubsection{#1}}

You shoudln't put \stepcounter into a \the... macro. This makes it fragile and will might break certain \label or similar code.

6
  • 1
    My answer shows the general way to redefine a macro which uses its old definition. If you only want to make the subsection counter independent of the section counter then @locksteps answer is the way to go. I think I misunderstood the question because I thought maincount is also used and incremented elsewhere. The main part makes it look like it. Commented May 27, 2011 at 21:09
  • 1
    @Martin: Wouldn’t it be better to use \refstepcounter to make the count work with \label-\ref-combinations?
    – Tobi
    Commented May 27, 2011 at 21:36
  • 1
    @Tobi: That's correct, however the \refstepcounter{subsection} will overwrite the label again. I didn't changed the OP original code in this point but merely wanted to show the redefinition technique. He doesn't state what exactly he is doing with it, so I kept it general Commented May 27, 2011 at 21:42
  • This works perfectly. As others have mentioned the way I went about this is probably not the best way to "change the count for subsections" but your solution is the correct one for "how to decorate a tex macro". Thanks Commented May 28, 2011 at 0:31
  • And maincount is indeed incremented elsewhere but I just incremented subsection instead and that works. Commented May 28, 2011 at 0:42
8

I suggest to simply use the chngcntr package.

\documentclass{article}

\usepackage{chngcntr}
\counterwithout{subsection}{section}

\begin{document}

\section{bla}

\section{blubb}

\subsection{foo}

\subsection{bar}

\end{document}
1
  • 1
    This is a much better solution for the numbering problem than the hack I was going for. Thanks. Commented May 28, 2011 at 0:40
1

Similar to lockstep's answer is xassoccnt and its \CounterWithout macro, which allows for a comma separated list of counters to be removed from the driving counter reset list:

\documentclass{article}

\usepackage{xassoccnt}
\CounterWithout{subsection,subsubsection}{section}

\begin{document}

\section{bla}

\section{blubb}

\subsection{foo}

\subsection{bar}

\subsubsection{foobar}

\end{document}

enter image description here

You must log in to answer this question.

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