逐字环境中的用户定义命令
是否可以在逐字环境中创建带有参数和一些缩进的新命令?
\newcommand{codeblock}[1]{\begin{quote}\begin{verbatim}#1\end{verbatim}\end{quote}}
这是行不通的。还有其他办法吗?
is it possible to create a new command with a parameter in a verbatim environment and some indentation?
\newcommand{codeblock}[1]{\begin{quote}\begin{verbatim}#1\end{verbatim}\end{quote}}
This does not work. Is there an other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
\begin{verbatim}
的工作原理。简要而粗略地。\begin{verbatim}
扩展为\verbatim
。\verbatim
将每个特殊字符的类别代码设置为12。现在所有字符都像数字或标点符号。
\verbatim
设置字体、parindent 并调用\@xverbatim
。\@xverbatim
使用以下技巧捕获逐字结尾:<前><代码>\def\@xverbatim#1\end{#1\end}
然后
\end{verbatim}
完成工作。\newcommand{\codeblock}[1]{\begin{quote}\begin{verbatim}#1\end{verbatim}\end{quote}}
的工作原理。\codeblock{Some {}$&%^_}
读取其参数。<代码>#1 -->部分代码{}$&%^_
注意:
{
,}
,$
,&
,< code>%,^
,_
的类别为 1,2,3,4,6,7,8 而不是 11 或 12! !)\codeblock
扩展为\begin{quote}\begin{verbatim} Some {}$&%^_ \end {逐字}\end {引用}
。重要提示:
\end
的反斜杠属于类别 0 而不是 11。此外,
{
和}
的类别为 1 和 2,而不是 11。并且
$
、&
、%
、^
、_
的类别为 3, 4、6、7、8。\begin{quote}
扩展为\quote
并执行\quote
。\begin{verbatim}
扩展为\varbatim
。\varbatim
更改所有类别和字体。但是(重要)反斜杠的类别(在
\end
中)保持等于 0。{
、}
、$
Some 之后键入的 >、&
、%
、^
、_
仍然存在,因为的“参数读取”在\verbatim
更改所有类别之前执行。但是您需要所有 char 都具有类别 11 o 12。\verbatim
调用\@xverbatim
。\@xverbatim
尝试使用以下技巧来捕捉你的论点:<前><代码>\def\@xverbatim#1\end{#1\end}
但这是不可能的,因为
\@xverbatim
试图捕获\end
其中所有字母 (\
,e
,n
,d
) 都有第 12 类和第 11 类。但实际上还有四个字母带有其他类别代码:
\
为类别 0,e
、n
、d
为类别 11。有可能(我不确定)这个技巧更巧妙:
红色字符属于类别 12。绿色字符属于类别 11。
\def
、\@xverbatim
、\end
是带有\
(类别 0)和字母(类别 11)的宏)。\@xverbatim
正在尝试并尝试查找\end
,其中反斜杠 (\
) 的类别为 11,但是...文件在扫描 \@xverbatim
时结束
如果您想创建新的宏
\codeblock
您必须执行类似的操作上面的文字。How
\begin{verbatim}
works. briefly and roughly.\begin{verbatim}
is expanded to\verbatim
.\verbatim
sets category code of each special characters to 12.Now all chars is like digits or puncts.
\verbatim
sets font, parindent and calls\@xverbatim
.\@xverbatim
catches the end of verbatim using the following trick:Then
\end{verbatim}
finishes work.How
\newcommand{\codeblock}[1]{\begin{quote}\begin{verbatim}#1\end{verbatim}\end{quote}}
works.\codeblock{Some {}$&%^_}
reads its argument.#1 --> Some code {}$&%^_
Note:
{
,}
,$
,&
,%
,^
,_
have categories 1,2,3,4,6,7,8 rather than 11 or 12!!!)\codeblock
expands to\begin{quote}\begin{verbatim} Some {}$&%^_\end {verbatim}\end {quote}
.Important: backslash of
\end
has category 0 rather than 11.Moreover
{
and}
have categories 1 and 2 rather than 11.And
$
,&
,%
,^
,_
have categories 3,4,6,7,8.\begin{quote}
expands to\quote
and\quote
executes.\begin{verbatim}
expands to\varbatim
.\varbatim
changes all categories and font. But (important)the category of backslash (in
\end
) remains equal to 0. And the categories of{
,}
,$
,&
,%
,^
,_
typed afterSome
remains because of "argument reading" executes before\verbatim
changes all categories. But you need that all char has categories 11 o 12.\verbatim
calls\@xverbatim
.\@xverbatim
tries to catch your argument using the following trick:but it is impossible because of
\@xverbatim
tries to catch\end
where all letters (\
,e
,n
,d
) have the category 12 and 11.But in fact there are four letters with other category code:
\
with category 0 ande
,n
,d
with category 11.It is possible (I am not sure) that trick is more masterly:
Red chars have category 12. Green chars have category 11.
\def
,\@xverbatim
,\end
are macros with\
(category 0) and letters (category 11).\@xverbatim
is trying and trying to find\end
where backslash (\
) has category 11 but....File ended while scanning use of \@xverbatim
If you want to create new macro
\codeblock
you must do something like above text.看起来您希望在文档中包含代码,在这种情况下,包列表可能比逐字为您提供更好的服务。列表还为您提供了一些不错的功能,例如许多常见语言的行编号和语法突出显示。请参阅http://www.ctan.org/tex-archive/macros /latex/contrib/listings/(如果尚未随您的 LaTeX 发行版一起安装)。
It looks like you want to have code in your document, in which case you're probably better served by the package listings than by verbatim. Listings also gives you nice features like line numbering and syntax highlighting for many common languages. See http://www.ctan.org/tex-archive/macros/latex/contrib/listings/ if it's not already installed with your LaTeX distribution.