如果我想写一篇关于如何verbatim
在 TeX 中使用的文章,我只需举一个例子
\documentclass[a4paper,11pt]{article}
\begin{document}
How to use verbatim in TeX:
\begin{verbatim}
start with \begin{verbatim}
and end with \end{verbatim}
And now you have to write
\end{verbatim}
In your own
\end{document}
那么它将无法编译,因为第一个\end{verbatim}
。我发现的一个解决方案是
\documentclass[a4paper,11pt]{article}
\usepackage{fancyvrb}
\usepackage{fvextra}
\begin{document}
The following is printed verbatim, but with line breaks:
\begin{Verbatim}[breaklines=true]
This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
\begin{verbatim}
test
\end{verbatim}
And now you have to write
\end{Verbatim}
on your own
\end{document}
即将它们与不同的环境堆叠在一起。是否还有其他解决方案?所以我的要求是是否有相同的环境定义,这样我就不必在不同的verbatim
/ Verbatim
/等之间切换。
答案1
一可能考虑使用堆栈,但这需要非常深入的手术,如果内部verbatim
不平衡,则无法解决问题。我认为您没有这么多出现verbatim
这种情况的环境\end{verbatim}
。
抱歉,但最实际的解决方案(可能也是唯一可行的解决方案)是使用不同的外部环境名称。如果您不想使用fancyvrb
,则可以使用较旧的verbatim
。
\documentclass[a4paper,11pt]{article}
\usepackage{verbatim}
\newenvironment{overbatim}{\verbatim}{\endverbatim}
\begin{document}
The following is printed verbatim, but with line breaks:
\begin{overbatim}
This is a line to start; never indent `verbatim'
unless you really want indented lines
\begin{verbatim}
test
\end{verbatim}
And now you have to write
\end{overbatim}
on your own
\end{document}
使用fancyvrb
,如果你想嵌套Verbatim
在里面
\documentclass[a4paper,11pt]{article}
\usepackage{fvextra}
\DefineVerbatimEnvironment{oVerbatim}{Verbatim}{}
\begin{document}
The following is printed verbatim, but with line breaks:
\begin{oVerbatim}[breaklines=true]
This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
\begin{Verbatim}
test
\end{Verbatim}
And now you have to write
\end{oVerbatim}
on your own
The following is printed verbatim, but with line breaks:
\begin{oVerbatim}[breaklines=true]
This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
\begin{Verbatim}
test
\end{Verbatim}
And now you have to write
\end{oVerbatim}
on your own
\end{document}
所以你就会明白为什么我建议永远不要缩进逐字环境。
如果您想要传递breaklines=true
给所有oVerbatim
环境,您可以利用的最后一个参数\DefineVerbatimEnvironment
,它接受任何选项列表fancyvrb
。
\documentclass[a4paper,11pt]{article}
\usepackage{fvextra}
\DefineVerbatimEnvironment{oVerbatim}{Verbatim}{breaklines=true}
\begin{document}
The following is printed verbatim, but with line breaks:
\begin{oVerbatim}[breaklines=true]
This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
\begin{Verbatim}
test
\end{Verbatim}
And now you have to write
\end{oVerbatim}
on your own
The following is printed verbatim, but with line breaks:
\begin{oVerbatim}
This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
\begin{Verbatim}
test
\end{Verbatim}
And now you have to write
\end{oVerbatim}
on your own
\end{document}
答案2
解决方案几乎只有两种:一种是verbatim
像您那样使用不同名称的环境,因为verbatim
环境实际上正在寻找\end{verbatim}
其结尾的文字字符串。这是任何标记语言中逐字打印的常见问题。
另一个选项是使用包\verbatiminput
中的外部文件verbatim
包含所需的文本。(如果您更喜欢该包的功能,则提供等效的命令)。verbatim
fancyvrb
\VerbatimInput