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?

Delta t
  • 253

1 Answers1

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

  • 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 Feb 15 '15 at 13:45
  • 2
    @Deltat: Verbatim content in command arguments is tricky ;-). It might be possible with xparse and its \NewDocumentCommand or \NewDocumentEnvironment features –  Feb 15 '15 at 13:46