# \stepcounter increments by two, when used with listings [closed]

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?

-

## migration rejected from stackoverflow.comMar 7 at 11:46

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

## closed as unclear what you're asking by Jubobs, Martin Schröder, Jesse, Guido, Svend TveskægMar 7 at 11:46

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

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

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