Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}}
share|improve this question
2  
What do you mean with "to decorate a LaTeX command"? It doesn't make much sense to me. –  Martin Scharrer May 27 '11 at 21:00
    
I meant in in the sense of the OO pattern: en.wikipedia.org/wiki/Decorator_pattern –  Pedro Côrte-Real Jan 1 at 3:41

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
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. –  Martin Scharrer May 27 '11 at 21:09
1  
@Martin: Wouldn’t it be better to use \refstepcounter to make the count work with \label-\ref-combinations? –  Tobi May 27 '11 at 21:36
    
@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 –  Martin Scharrer May 27 '11 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 –  Pedro Côrte-Real May 28 '11 at 0:31
    
And maincount is indeed incremented elsewhere but I just incremented subsection instead and that works. –  Pedro Côrte-Real May 28 '11 at 0:42

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}
share|improve this answer
1  
This is a much better solution for the numbering problem than the hack I was going for. Thanks. –  Pedro Côrte-Real May 28 '11 at 0:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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