我正在尝试将该filecontents
包与listings
我正在尝试按照以下建议如何定义仅进行参数替换的宏?以便并排显示 LaTeX 代码和相应输出。
以下是我所拥有的:
\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}
预期输出:
实际结果:
ERROR: Missing number, treated as zero.
--- TeX said ---
<to be read again>
\openin
l.1 Some lines?
答案1
我发现\tcbverbatimwrite
非常方便。它也tcolorbox
支持,并且可以以多种方式配置:listings
\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}
改良版
\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}