嵌套逐字注释

嵌套逐字注释

如果我想写一篇关于如何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包含所需的文本。(如果您更喜欢该包的功能,则提供等效的命令)。verbatimfancyvrb\VerbatimInput

答案3

根据您的编码,这可能会起作用或不起作用。

\我在和之间引入了一个零宽度空格(unicode U + 200B)end来破坏内部\end命令的功能,但不会在视觉上破坏它,并且它呈现得很好:

\begin{verbatim}\begin{verbatim}\​end{verbatim}\end{verbatim}

在此处输入图片描述

可以理解的是,上面的代码中看不到这个零宽度空格。Overleaf 的编辑器将其渲染为带有小图钉的红线:

在此处输入图片描述

相关内容