# \stepcounter increments by two, when used with listings

Users,

my counter usually gets incremented by 1, when I use \stepcounter{mycounter}

But surprisingly, it gets incremented by 2, when I use it together with listings:

\lstinputlisting[
title={\stepcounter{mycounter}\arabic{mycounter},
firstline=1, lastline=10, firstnumber=1
]{mytextfile.txt}


And how can I achieve the counter to be incremented by one?

-
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. – N.N. Sep 21 '11 at 16:52
I cannot see it with your code, it is incremented by 1! Create a complete example which shows your behaviour! – Herbert Sep 21 '11 at 17:25

## migrated from stackoverflow.comSep 21 '11 at 16:43

I bet listings measures width of the title by typestting it into a box which is then discarded, before typesetting it for real, or something similar. Then you get the counter incremented twice.
@Timo: Perhaps put the \stepcounter before the listing, rather than in the title? It has only one title and, logically, the numbering belongs to the listing and not to the title itself. – Ryan Reich Sep 21 '11 at 17:18
If you use it repeatedly, wrap it in a macro like \def\myListing#1{\stepcounter{mycounter}\lstinputlisting[title={\arabic{mycount‌​er}},firstline=1,lastline=10,firstnumber=1]{#1}} and then use \myListing{mytextfile.txt}. – eudoxos Sep 21 '11 at 17:54