1

I am writing a simple LaTeX guide, aimed at editors of a student paper. The paper is for students of mathematics and physics at my university, and so we use LaTeX.

Anyways, I want to include lots of examples in the guide, but it has become a bit cumbersome. I am using the following format right now:

\hspace{20pt}
\begin{framed}
  \begin{minipage}{0.8\textwidth}
    \begin{verbatim}
      Some code
    \end{verbatim}
    $\quad \Rightarrow$\\
    \mbox{}\\
      Some code
  \end{minipage}
\end{framed}

The line `` Some code '' Is first shown verbatim, and then put outside in order for it to compile completely, showing the output.

I wanted to simplify this into a single command, something like

\newcommand{\verandshow}{1}
{
\hspace{20pt}
\begin{framed}
   \begin{minipage}{0.8\textwidth}
    \begin{verbatim}
      #1
    \end{verbatim}
    $\quad \Rightarrow$\\
    \mbox{}\\
      #1
  \end{minipage}
\end{framed}
}

but the verbatim environment messes everything up. The same goes for creating a new environment, and putting the \begin{verbatim} and \end{verbatim} statements in each end.

I have heard of alternative ways of showing code, like listingsand others, but they did not work all that well. And even if they did, a question remains unanswered, one that I haven't found a simple answer to, which is: How do you define a command or environment that puts arguments into a verbatim environment?

4
  • What does did not work all that well mean? Please have a look on the the tcolorbox package and its listings library. It does show latex code and it's output (for not too complicated code of course)
    – user31729
    Commented Feb 15, 2015 at 13:30
  • First it didn't look as good as I had hoped, but my main problem somehow had to do with inputenc. listings, in particular, didn't take kindly to certain Norwegian letters, and I don't want to go too far down the rabbit hole just to get some code examples to show.
    – Delta t
    Commented Feb 15, 2015 at 13:33
  • with package fvrb-ex and fancyvrb.
    – touhami
    Commented Feb 15, 2015 at 13:38
  • 1
    Related (with examples for both tcolorbox and showexpl): Side-by-side source and output when documenting a style file Commented Feb 15, 2015 at 14:41

1 Answer 1

2

This shows only a small amount of the tcolorbox and listings features:

\documentclass{article}

\usepackage[most]{tcolorbox}


\begin{document}
\begin{tcblisting}{listing options={numbers=left},colback=green,arc=0mm,auto outer arc}
Some Code with math output: 

\[ E = mc^2 \]

\end{tcblisting}

% And now listing only

\begin{tcblisting}{colback=yellow,arc=0mm,auto outer arc,listing only}
Some Code with math output: \[ E = mc^2 \]

\end{tcblisting}


\end{document}

enter image description here

2
  • While this is a neat way of presenting everything, it sort of misses the question I most wanted answered. How can I create a command that places the arguments in verbatim? How can one get around the fact that once you place the start of a verbatim, there is no way of placing anything in between it and the closer that doesn't get printed verbatim?
    – Delta t
    Commented Feb 15, 2015 at 13:45
  • 2
    @Deltat: Verbatim content in command arguments is tricky ;-). It might be possible with xparse and its \NewDocumentCommand or \NewDocumentEnvironment features
    – user31729
    Commented Feb 15, 2015 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .