目前,使用shortvrb
,可以定义一个字符来单独启动内联逐字命令:
\documentclass{article}
\usepackage{shortvrb}
\begin{document}
\MakeShortVerb{\|}
foo |this is verbatim|
\end{document}
或者类似地fancyvrb
,
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\DefineShortVerb{\|}
foo |this is verbatim|
\end{document}
在这两种情况下——与 一样\verb!some verbatim text!
——都需要再次使用符号来结束逐字部分。如果省略了它,则会产生类似于“\verb 结束于行尾”的错误。
有没有办法(重新)定义\MakeShortVerb
或类似,使得换行符仅仅结束命令,而不是产生错误?理想情况下,可以写类似这样的代码:
\DefineAsymmetricVerb{\|}
This is some regular text.
Now some verbatim code that only needs a symbol at the start of the line, like:
| if 1 == 1
| print 'All is well'
这将允许逐字逐句的文本块(或单行),而不会产生
| if 1 == 1 |
| print 'All is well' |
或者
\begin{verbatim}
if 1 == 1
print 'All is well'
\end{verbatim}
答案1
你可以,但你不应该。
\documentclass{article}
\makeatletter
\catcode`|=\active
\def|{%
\par
\begingroup
\parindent\z@\mbox{}%
\owj@eol\catcode`\^^M=\active
\let\do\@makeother\dospecials
\verbatim@font\@noligs
\@vobeyspaces\frenchspacing
}
\def\owj@par{\par\endgroup}
\begingroup\lccode`~=`\^^M\lowercase{\endgroup
\def\owj@eol{\let~\owj@par}
}
\makeatother
\begin{document}
This is some regular text.
Now some verbatim code that only needs a symbol at the start of the line, like:
| if 1 == 1
| print 'All is well'
|
| else
| error
| fi
\noindent This is some regular text
\end{document}