我正在尝试创建自己的具有背景颜色的逐字环境,但由于某种原因它无法编译(出于各种原因,我想使用自己的宏):
\documentclass{article}
\usepackage{verbatim}
\usepackage{mdframed}
\makeatletter
\newenvironment{myspverbatim}{\begin{mdframed}[backgroundcolor=red]\begin{verbatim}}{\end{verbatim}\end{mdframed}}
\makeatother
\begin{document}
\begin{myspverbatim}
doesn't work?
\end{myspverbatim}
\end{document}
我不明白为什么。
错误:
Runaway argument? ! File ended while scanning use of \next. <inserted
text>
\par
答案1
你不能\begin{verbatim}
那样使用(并且它在的手册中有解释verbatim
)。
\documentclass{article}
\usepackage{xcolor}
\usepackage{verbatim}
\usepackage{mdframed}
\newenvironment{myspverbatim}
{\mdframed[backgroundcolor=red!90!green]\verbatim}
{\endverbatim\endmdframed}
\begin{document}
\begin{myspverbatim}
doesn't work?
\end{myspverbatim}
\end{document}
您可能希望使用fancyvrb
比更灵活的方法verbatim
。
\documentclass{article}
\usepackage{xcolor}
\usepackage{fancyvrb}
\usepackage{mdframed}
\newenvironment{myspverbatim}{%
\VerbatimEnvironment
\begin{mdframed}[backgroundcolor=red!90!green]% <-- no pure color
\begin{Verbatim}%
}{\end{Verbatim}\end{mdframed}}
\begin{document}
\begin{myspverbatim}
doesn't work?
\end{myspverbatim}
\end{document}
答案2
您可以使用 tcolorbox 包:
\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcblisting{myspverbatim}{colback=red,listing only}
\begin{document}
\begin{myspverbatim}
doesn't work?
\end{myspverbatim}
\end{document}