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.

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:

\documentclass{scrbook}

\usepackage{showexpl}
\usepackage{soul}

\providecommand{\IfDefined}[2]{%
\ifcsname #1\endcsname
   #2 %
\else
     % do nothing
\fi
}
\begin{document}
\IfDefined{so}{
\begin{LTXexample}
\so{letterspacing}, \ul{underlining} 
\st{overstriking} and \hl{highlighting}. 
\end{LTXexample}
}
\end{document}

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.

share|improve this question
    
A dirty workaround may be to use a separate file for each example and use \input and \verbatiminput (or anything fancier). –  Tsuyoshi Ito Sep 30 '11 at 22:30
    
Useful link: Why doesn’t verbatim work within …? –  Peter Grill Oct 1 '11 at 16:35
add comment

3 Answers

up vote 4 down vote accepted

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

\startbuffer[so-example]
\so{letterspacing}, \ul{underlining} 
\st{overstriking} and \hl{highlighting}
\stopbuffer

\doifdefined\so{\typebuffer[so-example]}

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

\begin{filecontents}{\jobname-so-example}
\begin{LTXexample}
\so{letterspacing}, \ul{underlining} 
\st{overstriking} and \hl{highlighting}. 
\end{LTXexample}
\end{filecontents}

\IfDefined\so{\input{\jobname-so-example}}

You can also wrap the last bit behind a macro:

\def\ShowExample#1
   {\IfDefined{\csname#1\endcsame}{\input{\jobname-#1-example}}
share|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
add comment

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.

\documentclass{article}
\usepackage{wasysym}
\usepackage{verbatim}
\nofiles

\newcommand{\opluslhrim}{\mathbin{\rlap{$\Leftcircle$}{+}}}
\newcommand{\oplusrhrim}{\mathbin{{+}\llap{$\Rightcircle$}}}

\begin{document}

\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
\]

\vspace{3\baselineskip}
\verbatiminput{\jobname.tex}

\end{document}

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.

share|improve this answer
add comment

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

\makeatletter
\newcommand\IdDefined[1]
    {\ifcsname #1\endcsname
      \else
        \expandafter\@gobble
      \fi}
\makeatother
share|improve this answer
add comment

Your Answer

 
discard

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.