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.

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}

What's wrong about that?
And how can I achieve the counter to be incremented by one?

share

migration rejected from stackoverflow.com Mar 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æg Mar 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.

2  
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
2  
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

1 Answer 1

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.

share
    
Thank you for your reply. It sounds like a good idea. Do you know a way around this behaviour? –  user8006 Sep 21 '11 at 17:10
3  
@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