![在环境中使用 \input 失败](https://linux22.com/image/337989/%E5%9C%A8%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%BD%BF%E7%94%A8%20%5Cinput%20%E5%A4%B1%E8%B4%A5.png)
我正在尝试将该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}