5

I'm trying to use the filecontents package in combination with listings as suggested in How to define macro that only makes argument substitution? in order to produce a side-by-side view of LaTeX code and the corresponding output.

Here's what I have:

\documentclass{article}
\usepackage{listings,filecontents}

\begin{document}

\newenvironment{testenv}{%
  \csname filecontents*\endcsname{\jobname.tmp}}{%
  \csname endfilecontents*\endcsname
  \noindent
  \begin{minipage}{0.5\textwidth}%
    \input{\jobname.tmp}%
  \end{minipage}%
  \begin{minipage}{0.5\textwidth}%
    \lstinputlisting[basicstyle=\ttfamily]{\jobname.tmp}%
  \end{minipage}%
}%

\begin{testenv}
Some lines?
of text !!!
here.
\end{testenv}

\end{document}

Expected output:

enter image description here

Actual result:

ERROR: Missing number, treated as zero.

--- TeX said ---
<to be read again> 
                   \openin 
l.1 Some lines?
  • @ChristianHupfer I copied this part from somewhere, but I think it's because the body of a filecontents* environment is treated as verbatim text, and then normal rules don't apply. – Ernest A Jul 21 '16 at 16:10
  • The verbatim content is indeed a problem here! – user31729 Jul 21 '16 at 16:14
4

I find \tcbverbatimwrite from tcolorbox very convenient. It has support for listings as well and can be configured in multiple ways:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\begin{document}



\newenvironment{testenv}{%
  \begingroup%
  \tcbverbatimwrite{\jobname.tmp}}%
{\endtcbverbatimwrite\endgroup%
  \noindent
  \begin{minipage}{0.5\textwidth}%
    \input{\jobname.tmp}%
  \end{minipage}%
  \begin{minipage}{0.5\textwidth}%
    \lstinputlisting[basicstyle=\ttfamily]{\jobname.tmp}%
  \end{minipage}%
}

\begin{testenv}
Some lines?
of text !!!
here.
\end{testenv}

\end{document}

Improved version

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\begin{document}



\newenvironment{testenv}{%
  \begingroup%
  \tcbverbatimwrite{\jobname.tmp}}%
{\endtcbverbatimwrite\endgroup%
  \noindent
  \begin{minipage}{0.5\textwidth}%
    \input{\jobname.tmp}%
  \end{minipage}%
  \begin{minipage}{0.5\textwidth}%
    \lstinputlisting[basicstyle=\ttfamily]{\jobname.tmp}%
  \end{minipage}%
}


\newenvironment{othertestenv}{%
  \begingroup%
  \tcbverbatimwrite{\jobname.tmp}}%
{\endtcbverbatimwrite\endgroup%
  \noindent
  \tcbinputlisting{colback=white!30!yellow,text side listing,listing options={basicstyle=\ttfamily},listing file=\jobname.tmp}%
}

\begin{testenv}
Some lines?
of text !!!
here.
\end{testenv}

\begin{othertestenv}
Some lines?
of text !!!
here.
\end{othertestenv}


\end{document}

enter image description here

3

So with tcolorbox you only need

\usepackage[listings]{tcolorbox}

...

\newtcblisting{testenv}{%
  colback=white,text side listing,boxrule=0pt,opacityframe=0}

and that's it!

\begin{testenv}
Some lines?
of \emph{text} !!!
here.
\end{testenv}

produces

enter image description here

  • Yes, that would be a simpler version then – user31729 Jul 21 '16 at 17:53

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.