Tell me more ×
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|improve this question
1  
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

migrated from stackoverflow.com Sep 21 '11 at 16:43

1 Answer

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|improve this answer
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
2  
@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

Your Answer

 
discard

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