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.

I am a beginner to LaTeX, and I am reading Leslie Lamport's A Document Preparation System. In various places of his book, he puts the LaTeX source code beside the output obtained by running latex on the source. Even more interestingly, in order to keep the same height for both the source codes and the output on the page, some source codes are omitted by ellipsis. I am wondering how he has made it possible to omit the right portion of the source snippets such that the verbatim source codes occupy the same height as the output.

I have googled about the various verbatim packages. But it seems that none of them can be smart enough to keep the verbatim source codes occupy the same vertical distance as the output. I am now skeptical about if there exists such packages or you have to do it manually.

EDIT I have screen-shot an example. I wonder if the author manually did the verbatim part or there is a package that can substitute the text with ellipsis when you require the height of the verbatim text (on the right of the example) to be the same as the real output (on the left of the example). enter image description here

share|improve this question
4  
Do you mean showexpl? –  Marco Daniel Jun 18 '12 at 16:40
    
I saw some example renderings online. It seems that this package shows the latex sources in full, beside the typeset output. What if I just want to show a snippet of the source? If the source codes are shown in full length, it may be pretty long and the there will be excessive blanks above/below the real output. –  Rich Jun 18 '12 at 17:02
2  
The showexpl package Marco Daniel recommended is very good at showing the output of code snippets in documents. Alternatively you could use the listings package (which is used internally by showexpl) to format the source code and include the output via a separate image (but this is error prone so you'll need to verify that the output is indeed produced by the associated code snippet). –  Peter Grill Jun 18 '12 at 17:09
    
See for instance How can other programming languages and tools be used to create content for TeX documents?. Here is an example where I used showexpl to show conde snippets to explain my answer: How do I stop tables from messing with alignment?. You could do the same in your documents. –  Peter Grill Jun 18 '12 at 17:13
    
I really dislike that kind of presentation, but if that is what you desire you should use listings for the code and include an image of the output, although it should be possible to automate it, but would need to be done for each case where you want to do this. –  Peter Grill Jun 18 '12 at 17:35
show 4 more comments

2 Answers

up vote 6 down vote accepted

Here is an adaptation of Herbert's solution. The macro \DisplayAsLdots{} is used to wrap the contents of the source code which you want replaced with .... It uses \immediate\write18 to pre-processes the \jobname.vrb file through a sed script (thanks to David Carlisle and egreg) to produce the source code displayed on the right:

enter image description here

Notes:

  • This will only work on a Unix OS.
  • As Stephen Lehmke pointed out, there can not be any line breaks within the \DisplayAsLdots{} macro.
  • Since sed is greedy it will effectively gobble any other macros (and any content in between) that begin and end on the same line. So, code such as:

    \DisplayAsLdots{foo} xyz \label{bar}
    

    will be treated as if were (note where the parameter to \DisplayAsLdots ends):

    \DisplayAsLdots{foo xyz \label{bar}}
    

Further Enhancements:

  • Adapt to work on non-unix operating systems (perhaps a pure LaTeX solution that does not require temporary files).
  • Use \ldots instead of three periods.
  • Enhance the regular expression for sed to allow for line breaks in \DisplayAsLdots, and terminate the replacement with the proper matching closing brace (to fix 3rd point in Notes section above).

Comment on this Presentation Style:

As mentioned in the comments, I really dislike this method of presentation. This stems from the fact that this documentation appears as if it is intended to illustrate how to use itemize lists to novice users -- My feeling is that anything that could possibly distract the reader should be minimized. In this example there are two issues that come to mind:

  • The use of the . . . (and the associated missing text).
  • The fact that there is an alignment between the input and the output.

With LaTeX there really is no such thing as a line by line comparison with the input and the output, so to show an example where things appear to be aligned will require additional work from the reader to comprehend.

I would rather see the full input text and output so I can see the correspondence between the input and the output.

Keep in mind that this is just personal opinion, others might prefer this style.

Code:

\documentclass{article}
\usepackage{enumitem}
\usepackage{blindtext}
\usepackage{fancyvrb}
\usepackage{listings}

\setlist[itemize]{leftmargin=*}%

\newcommand*{\DisplayAsLdots}[1]{#1}%

\newlength\CodeWidth
\makeatletter
\newenvironment{ShowCode}[2][]
  {\lstset{#1}\VerbatimEnvironment%
   \global\CodeWidth=#2%
   \begin{VerbatimOut}{\jobname.vrb}}
  {\end{VerbatimOut}\settowidth\@tempdima{\lstinputlisting{\jobname.vrb}}%
   \ReplaceDisplayedLdots%
   \noindent%
   \minipage[t]{\CodeWidth}\vspace{0pt}\input{\jobname.vrb}\endminipage\hfill%
   \minipage[t]{\dimexpr\@tempdima-2\fboxsep-2\fboxrule\relax}
     \vspace{0pt}\lstinputlisting{\jobname.vrb.new}\endminipage}

\newcommand{\ReplaceDisplayedLdots}{%
    \immediate\write18{%
          cat \jobname.vrb%
        | sed -e 's|\string\\DisplayAsLdots{.*}|...|'%
        > ./\jobname.vrb.new%
    }%
}%
\makeatother


\begin{document}
\blindtext

\begin{ShowCode}[language={[LaTeX]TeX},frame=single,columns=fixed,
   basicstyle=\small\ttfamily,breaklines,
   keywordstyle=\bfseries,
]{0.50\linewidth}
\begin{itemize}
\item Each list is \DisplayAsLdots{marked with a \emph{label}.  The labels in this itemized list are}  bullets.
\item List can be \DisplayAsLdots{nested within} one another.
\begin{enumerate}
   \item The item labels \DisplayAsLdots{in an enumerated list are numberals or} letters.
   \item A list should \DisplayAsLdots{have at least} two items.
\end{enumerate}
\LaTeX\ permits at least four levels \DisplayAsLdots{of nested lists, which is} more than enough.

\item Blank lines \DisplayAsLdots{before an item} have no effect.
\end{itemize}
\end{ShowCode}
\end{document}
share|improve this answer
    
Great idea! Note though that this is slightly fragile because sed doesn't count braces and strictly looks at code lines. So the closing brace of \DisplayAsLdots should always be the last brace on the line and on the same line as the opening brace. –  Stephan Lehmke Jun 27 '12 at 4:17
    
@StephanLehmke: Yep, realized that while developing this and it took me soo long to get it right that I forgot to mention it. Have added a note to reflect this. –  Peter Grill Jun 27 '12 at 4:50
    
Good addition. Maybe you should also mention that any following arguments on a line (like \DisplayAsLdots{foo}...\label{bar}) will be added to the text clipped out by sed. –  Stephan Lehmke Jun 27 '12 at 5:00
    
@StephanLehmke: Thanks again. Have added a note about that as well. –  Peter Grill Jun 27 '12 at 20:23
add comment

here is a simple example for an environment:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.82]{beramono}
\usepackage{blindtext}
\usepackage{fancyvrb}
\usepackage{listings}
\newlength\CodeWidth
\makeatletter
\newenvironment{ShowCode}[2][]
  {\lstset{#1}\VerbatimEnvironment%
   \global\CodeWidth=#2%
   \begin{VerbatimOut}{\jobname.vrb}}
  {\end{VerbatimOut}%
   \noindent%
   \minipage[t]{\CodeWidth}\vspace{0pt}\input{\jobname.vrb}\endminipage%
   \minipage[t]{\dimexpr\linewidth-\CodeWidth-2\fboxsep-2\fboxrule\relax}
     \vspace{0pt}\lstinputlisting{\jobname.vrb}\endminipage}
\makeatother

\begin{document}
\blindtext

\begin{ShowCode}[language={[LaTeX]TeX},frame=single,columns=fixed,
   basicstyle=\small\ttfamily,breaklines,postbreak=\mbox{$\hookrightarrow$},
   keywordstyle=\bfseries]{0.6\linewidth}
\begin{itemize}
\item This is a item with no sense in its text
\item another one
\end{itemize}
\end{ShowCode}

\end{document}

enter image description here

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.