All my ideas on how to do clever things with code examples and listings results further or later in 'Emergency Stop' errors.

Let me briefly review what I am doing now and what I actually wanted to achieve: I have provided for some years now a LaTeX template which includes a demo.tex which shows all the possibilities of the template. For me it is used mainly for testing purposes (and I found many bugs in packages using it). For the users it is a great 'howto manual' provided with their template.

Currently the users have to look into the code to see how the example worked. For the upcoming release I want to change that by providing the source in the pdf. Also it would have been great if it was possible to provide everything as a style file, and not just a .tex file. The idea was to provide \AddDemo and \PrintDemo, so that these could be defined wherever and printed on request. Especially this would allow not only me, but also package writers to provide their own example code. However from the answers of related questions posted here in the last few days I have given up on this idea.

Still there is the idea to show code and results side by side (or top/bottom). Here the showexpl package provides something useful.

BUT: it fails if placed in a command. This makes it unusable, since I must test if the example can be executed. The code below demonstrates this:



\ifcsname #1\endcsname
   #2 %
     % do nothing
\so{letterspacing}, \ul{underlining} 
\st{overstriking} and \hl{highlighting}. 

Any other \ifdef command would probably result in the same error.

If there is no way to use verbatim material in any kind of command I will have to fall back to simple lstlisting environments and placing every code twice.

Any improvement for my code related to my idea of providing general example code is welcome.


FWIW, ConTeXt provides a buffer environment which is very useful for such situations. In ConTeXt MkII (pdftex and xetex engines), the contents of the buffer are stored in an external file, while in MkIV (luatex engine), the contents of a buffer are stored in memory. So, one could do something like what you want in ConTeXt as follows

\so{letterspacing}, \ul{underlining} 
\st{overstriking} and \hl{highlighting}


LaTeX does not really have anything equivalent to buffers. What comes closet is the filecontents package (see this question). So you can try (untested)

\so{letterspacing}, \ul{underlining} 
\st{overstriking} and \hl{highlighting}. 


You can also wrap the last bit behind a macro:

| improve this answer | |
  • Any code that is based on the creation of files would create more than 100 files, which is not acceptable since the planed code is part of a package and I do not want to fill the users folder with +100 unwanted files. – Matthias Pospiech Oct 2 '11 at 8:58
  • @MatthiasPospiech: To avoid clutter, you can store files in a separate sub-folder. If you can use luatex then buffers are kept in-memory, although I don't know of any luatex packages for LaTeX which implements the functionality. – Aditya Oct 2 '11 at 12:33
  • I have not control over the usage of a package. Any user may choose to use luatex or not. – Matthias Pospiech Oct 2 '11 at 15:18

this doesn't answer the question completely or directly, but maybe the technique can be adapted for your purpose. here is a small example file that "does its thing" and then prints itself out following the demonstration.




\section*{Semidirect sums}
Constructed using \texttt{wasy}'s left and right half circles:\\
$\Leftcircle \quad + \quad A \opluslhrim B \quad
+ \quad \Rightcircle \quad C \oplusrhrim D$

In display:
\Leftcircle \quad + \quad A \opluslhrim B \quad
+ \quad \Rightcircle \quad C \oplusrhrim D



i've used this technique to accumulate a collection of "howto" files that give solutions for problems that are already solved, for reference by the ams production staff. of course, this technique does assume that there is nothing in the test code that will cause the job to halt with an error.

| improve this answer | |

If you don't mind an extra group around the output, then you can use:

    {\ifcsname #1\endcsname
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.