TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following code is the simplified version of my real scenario. It might look trivial.

I forgot to say that the following code is the Minimal Not Working Example (MNWE):


    \advance \counter by 1

When \counter=1, for example, I want the input part (source part) to get rendered as


rather than



Error messages when it is compiled with pdflatex, latex, xelatex:


Package Listings Warning: Text dropped after begin of listing on input line 21.

! Emergency stop.
<*> counterlistings

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on counterlistings.log.

How to execute \LTXexample repeatitively with a looping macro such that its counter value gets rendered in both input/output parts?

share|improve this question
About the MNWE: Note that all MWE (of questions, not answers) should "not work", so they can show the issue. – Martin Scharrer Aug 11 '11 at 21:19

The issue here is that \loop reads everything till \repeat as argument and therefore doesn't allow verbatim environments like LTXexample:

\long macro:#1\repeat ->\def \iterate {#1\relax \expandafter \iterate \fi }\iterate \let \iterate \relax 

You need to look for a different way to loop over it. Note that showexpl uses listings which allows for escape characters. You could use these to escape around the counter value and have \number\counter there. This should write the number to the temp file.

share|improve this answer
Only the first two lines of your answer are understandable. The remaining ones are difficult to understand even I read multiple times. :-) – xport Aug 11 '11 at 21:43
Simply open the listings manual and search for escape! :-) Anyway, this doesn't work for write-to-file listings. They escape characters are then ignored :-( – Martin Scharrer Aug 11 '11 at 21:45
OK. I read the manual until page 24 yesterday. I will continue the remaining later. I will decide whether or not I should execute the green mark on this answer later after grasping the knowledge. :-) – xport Aug 11 '11 at 22:01

Your Answer


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.