我正在尝试使用 创建一个包含居中代码片段的新环境minted
。首先,我遇到了一个minipage
包,但是它需要宽度规范。我只希望它根据其内容长度动态变化。所以我选择了varwidth
看起来完全符合我需求的。但是当我尝试将其设置为:
\newenvironment{ra}[3][]{
\VerbatimEnvironment
\begin{listing}
\caption{#2}
\label{#3}
\begin{center}
\begin{varwidth}{15em}
\begin{minted}[escapeinside=||,
mathescape=true,
autogobble,
frame=single,
style=vs,
#1]{text}}
{
\end{minted}
\end{varwidth}
\end{center}
\end{listing}
}
它的行为与 一样minipage
。根本不调整。用法:
\begin{ra}{some caption}{code:label}
Example
\end{ra}
我做错了什么吗?谢谢
答案1
你想Verbatim
做成BVerbatim
:
\documentclass{article}
\usepackage{minted}
\newenvironment{ra}[3][]
{%
\VerbatimEnvironment
\begin{listing}%
\caption{#2}\label{#3}%
\centering
\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
\begin{minted}[
escapeinside=||,
mathescape=true,
autogobble,
frame=single,
style=vs,
#1
]{text}%
}
{%
\end{minted}
\end{listing}
}
\begin{document}
\begin{ra}{some caption}{code:label}
Example
\end{ra}
\end{document}
frame
但不支持该选项BVerbatim
。补救措施是手动构图。
\documentclass{article}
\usepackage{minted}
\newenvironment{ra}[3][]
{%
\VerbatimEnvironment
\begin{listing}%
\caption{#2}\label{#3}%
\centering
\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
\begin{lrbox}{\rabox}%
\begin{minted}[
escapeinside=||,
mathescape=true,
autogobble,
frame=single,
style=vs,
#1
]{text}%
}
{%
\end{minted}
\end{lrbox}
\fbox{\usebox{\rabox}}
\end{listing}
}
\newsavebox{\rabox}
\begin{document}
\begin{ra}{some caption}{code:label}
Example
\end{ra}
\end{document}