17

I would like to write some LaTeX code, and without repeating myself, I would like that to be used for displaying that particular piece of code as-is and its typeset version, next to each other.

The solution in my head involves creating a command which shall take one required argument, which will be my LaTeX code; and this command shall use its argument to display it both inside some verbatim-like environment where it shall remain as-is, and outside where it shall be typeset.

However, something as in below does not work:

\newcommand{\codewithoutput}[1]
{
    \begin{verbatim}
        #1
    \end{verbatim}
    #1
}

And this is it, this is the best I can. I am completely stuck at here for the past 2 hours.

Since I am trying to display a LaTeX code here, using \texttt[1] or anything like that will not help, I think.

27

The tcolorbox package can nicely display literal LaTeX code and its output when being called in a very configurable manner, depending on the argument of the tcblisting environment. This environment displays by default LaTeX code literally but the coding/scripting etc. language can be changed to anything known to the underlying listings package too (or minted, if needed)

Furthermore it allows documentation of LaTeX code with the documentation library (I use it for as documenation tool for my personal packages too)

Please note that using complete documents to show LaTeX code is much more different (and difficult), of course.

\documentclass{article}

\usepackage[most]{tcolorbox}
\begin{document}

\begin{tcblisting}{% Some options here
}
\newcommand{\codewithoutput}[1]
{
  This is a nice command and has the argument #1
}
\codewithoutput{\LaTeX}
\end{tcblisting}

% With some configuration of the tcblisting environment:     
\begin{tcblisting}{enhanced jigsaw,colback=yellow!40!white,drop lifted shadow}
\newcommand{\codewithoutput}[1]
{
  This is a nice command and has the argument #1
}
\codewithoutput{\LaTeX}
\end{tcblisting}


\end{document}

enter image description here

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.