目前,使用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 returned by end of line」的錯誤。
有沒有一種方法可以(重新)定義\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}