我想制作一个宏\mintedInOutTable
,将一些代码放入铸造环境中,然后将其放入表格中(例如,显示 REPL 的输入和输出)。
对于此代码,
\mintedInOutTable{2 + 2}{4}
我希望输出看起来像这样(使用 Minted 的语法高亮):
这是我尝试过的(array
已加载包):
\newcommand{\mintedInOutTable}[2]{
\begin{tabular}{p{0.1\textwidth}p{0.8\textwidth}}
In & \begin{minted}{Python}#1\end{minted} \\
Out & \begin{minted}{Python}#2\end{minted}
\end{tabular}
}
但这不起作用。
答案1
如果您的代码片段只有一行,请使用\mintinline
。但它也可以处理更长的代码。
\documentclass{article}
\usepackage{array}
\usepackage{minted}
\newcommand{\mintedInOutTable}[2]{%
\begin{tabular}{@{}w{l}{0.1\textwidth}w{l}{0.8\textwidth}@{}}
In & \mintinline{python}{#1} \\
Out & \mintinline{python}{#2}
\end{tabular}%
}
\newsavebox{\pythonbox}
\NewDocumentEnvironment{longmintedInOutTable}{m}
{%
\VerbatimEnvironment
\begin{lrbox}{\pythonbox}
\begin{minipage}[t]{0.8\textwidth}
\begin{minted}{python}%
}
{%
\end{minted}\end{minipage}\end{lrbox}%
\begin{tabular}{@{}w{l}{0.1\textwidth}w{l}{0.8\textwidth}@{}}
In & \usebox{\pythonbox} \\
\noalign{\vspace{\dp\strutbox}}
Out & \mintinline{python}{#1}
\end{tabular}%
}
\begin{document}
\mintedInOutTable{2 + 2}{4}
\begin{longmintedInOutTable}{Hello, Jack}
def greet(name):
print ('Hello', name)
greet('Jack')
\end{longmintedInOutTable}
\end{document}