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

I am further working on my code for LaTeX Examples. I want to use the package showexpl for the simultaneus printing of code and results. Unfortunately I get an '! Emergency stop.'


  \expandafter\providecommand\csname demo@content@#2\endcsname{#3}%
  \expandafter\providecommand\csname demo@options@#2\endcsname{#1}%

  { % print 
    \renewcommand{\demo@printhis}{\expandafter\csname demo@content@#1\endcsname}

\AddDemo{text}{Sample Text}% end: \AddDemo



If this would work, the next problem would be that the listings shall only show the expanded version. So If I wrote:


the listings package would output '\PrintDemo{text}' instead of its contents.

share|improve this question
You don't need the commandexpandafter in the redefinition of \renewcommand{\demo@printhis}{\csname demo@content@#1\endcsname}. You can simple use \csuse{demo@content@#1}. The second problem: LTXexample uses listings and you can use verbatim-material inside a command. – Marco Daniel Sep 23 '11 at 8:52
A small question: Why do you use providecommand? – Marco Daniel Sep 23 '11 at 8:56
providecommand is wrong, I exchanged the definition of AddDemo to \providecommand\AddDemo[2]{ \ifcsdef{demo@content@#1}{ \expandafter\renewcommand\csname demo@content@#1\endcsname{#2}% }{ \expandafter\newcommand\csname demo@content@#1\endcsname{#2}% } } – Matthias Pospiech Sep 25 '11 at 14:14
up vote 2 down vote accepted

There are many problems in your code. The main one is that LTXexample is an environment much like verbatim and cannot be used in the argument of a command.

It's possible to circumvent this problem in some cases, via \scantokens. However your way to proceed won't respect line breaks in the input; at least not without heavy surgery with category codes that will break in any case because the code would be read many times; once a character token enters TeX its category code is fixed. With \scantokens it can be reread and category code reassigned, but once it's passed to LTXexample the problem will reappear.

Just to show a possibility, here is an attempt:

  \expandafter\newcommand\csname demo@content@#2\endcsname{#3}%
  \expandafter\newcommand\csname demo@options@#2\endcsname{#1}%

  { % print 


\AddDemo{text}{Sample Text}% end: \AddDemo

You can try it and verify it seems to work as intended. But


will print exactly in the same way (which is usually not intended in code samples). The major problem, however, is that

\AddDemo{text}{Sample Text \today}

will break listings (that is used by LTXexample). Try it.

Some comments

Your use of \providecommand is wrong; \providecommand\AddDemo will be silently ignored if \AddDemo is already defined. Say that some package defines it: you'll be very puzzled about the result.

It's wrong also inside the definition of \AddDemo; say that


is, by mistake, used: \PrintDemo{text} would always use demo1 and you'll not be notified about the double definition.

Also \expandafter\csname demo@content@#1\endcsname is questionable: the \expandafter will try to expand d (which of course is not expandable); the effect is exactly the same as \csname demo@content@#1\endcsname. In other situations a misplaced \expandafter can produce disasters.

I can understand that you'd like to have the code samples all in one place, but typesetting TeX code requires subtleties that make this goal quite difficult (if not impossible) to attain.

share|improve this answer
\expandafter\newcommand\csname demo@content@#2\endcsname{#3}% can be changed to \csdef{demo@content@#2}{#3}. It's shorter ;-) – Marco Daniel Sep 25 '11 at 14:19
I agree that providecommand is the wrong approach. However if the \expandafter code is incorrect, which was adviced in a different question, I would like to know what would be correct? – Matthias Pospiech Sep 25 '11 at 14:20
Just \csname characters\endcsname is sufficient; the first step expands \csname and forms the token from characters, then the token is expanded (or executed, depending on its meaning). – egreg Sep 25 '11 at 15:00

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.