在环境中使用 \input 失败

在环境中使用 \input 失败

我正在尝试将该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}

在此处输入图片描述

答案2

所以tcolorbox你只需要

\usepackage[listings]{tcolorbox}

...

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

就是这样!

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

生产

在此处输入图片描述

相关内容