因此,%
用于开始行末注释,这很明显。
但是,有没有办法像许多其他编程语言一样一次性注释掉部分代码呢?
例子:
- HTML:
<!-- comment -->
- C++:
/* comment */
- 矩阵:
%{ comment %}
这在两种情况下可能有用:
- 一次性注释掉一堆代码(而不是
%
在每一行前面都加上 - 在段落/函数/宏内“内联”注释
我知道许多编辑器都有评论文本块的快捷方式,但我并不需要它。
答案1
comment
包在这方面非常擅长,即使环境中的部分无效或在序言中。
\documentclass{article}
\usepackage{lipsum}
\usepackage{comment}
\begin{comment}
\usepackage{lipsumx}: I try to make an invalid package loading command
\usepackage{lipsumx}
\usepackage{lipsumx}
\usepackage{lipsumx}
\usepackage{lipsumx}
\usepackage{lipsumx}
\usepackage{lipsumx}
\usepackage{lipsumx}
\usepackage{lipsumx}
\usepackage{lipsumx}
\end{comment}
\begin{document}
\lipsum[1-2]
\begin{comment}
\begin{enumerate}: No \ end{enumerate}, but still compilable!
\end{comment}
\begin{equation}
1
\begin{comment}
blah blah: or in math mode...
\end{comment}
+2=3
\end{equation}
\end{document}
但是,正如 Ulrich Diez 在评论中所说,该环境仍然不完善。例如,无法进行内联注释,因为它基于verbatim
AFAIK。希望/* */
将来能够添加像 C 中的注释字符。
答案2
您可以根据需要尝试使用以下代码:
\iffalse
\lipsum[1-2]...
\fi
答案3
无法进行内联评论。
%
可以通过 (或在读取和标记相关输入时类别代码为 14(注释) 的任何其他字符)进行行尾注释。
(您还可以使用类别代码 5(行尾)的字符让 (La)TeX 停止对当前输入行进行标记。与使用类别代码 14(注释)的字符的细微差别是:如果您使用类别代码 5(行尾)的字符,则根据阅读器的状态,对于此类别代码\par
5 字符,您将获得(在状态 N = 新行)一个 -token 或(在状态 M = 行中)一个空格标记或(在状态 S = 跳过空格)根本没有标记。之后,(La)TeX 将开始对下一行进行标记,因此阅读器将处于状态 N。(La)TeX 逐行读取输入并逐个字符地处理每一行。通常 (La)TeX 会从行尾中删除空格字符(空格字符的代码点在 ASCII 和 UTF-8 中均为 32),然后附加一个字符,其代码点的编号在 (La)TeX 的内部编码中为(是 ASCII 或 UTF-8)等于\endlinechar
在进一步处理该行输入之前整数参数的数量。通常\endlinechar
具有数字 13,而 13 是 (La)TeX 内部编码(ASCII 或 UTF-8)中返回字符的代码点的数量,并且返回字符/字符 13 具有类别代码 5。这就是为什么通常输入中的空行/两个连续换行符会产生标记,\par
因为在这两个换行符中的第二个,当读取设备处于状态 N 时,(La)TeX 将遇到类别代码为 5 的结束行字符。)
有一些方法旨在让 (La)TeX 读取和标记内容,然后以一种让标记消失而不产生可见输出的方式执行生成的标记。例如使用 comment-package 的方法。例如,使用 或 之类的方法\iffalse... comment...\fi
。 \@gobble{...comment...}
但所有这些方法都不是万无一失的。
例如,你不能用它们来评论宏观论证或⟨平衡文本⟩定义:
如果你
\newcommand\foobar{%
This is the definition of foobar.
\begin{comment}
This is a comment.
\end{comment}
}
,你将拥有构成评论环境的所有标记⟨替换文本⟩除此之外\foobar
,在收集 ⟨替换文本⟩例如\foobar
,该短语\end{comment}
将在正常的 catcode-régime 下被标记,而注释环境背后的机制“期望”它在 verbatim-catcode-régime 下被标记,因此将被破坏。
据我所知,唯一万无一失的注释方法是通过%
类别代码 14(注释)的字符以行尾注释的形式进行注释,因为这是唯一一种在标记化阶段输入的字符根本不产生标记的方法。
有时可以做的另一件事是将注释等放在代码末尾,即在\bye
- 或\stop
- 或\end{document}
- 或 -\endinput
命令后面。
如果您希望经常在注释和不注释某些输入行之间切换,您可以通过用通常不会出现在 .tex 输入文件中的字符开头这些行,并将该字符的 catcode 在 14(注释)和 9(忽略)之间切换来实现。您可以通过^^
-notation 输入字符。
例如,我从不使用 ASCII 和 UTF-8 中代码点编号为 1 的“标题起始”字符,而在 (La)TeX 中,在正常的 catcode 条件下,人们可以将该字符输入为^^A
。
例如,
\documentclass{article}
\edef\savedcatcode{\the\catcode`\^^A}%
\catcode`\^^A=14 % <- lines starting with ^^A are comments.
%\catcode`\^^A=9 % <- lines starting with ^^A are processed as it the ^^A wasn't there.
% Make sure that the things behind the ^^A, i.e., the things which either
% are to be kept or are to be commented out/are to be ignored, don't contain \input-
% directives or the like for reading and tokenizing input-files where the
% catcode of ^^A should not be changed.
\newcommand\mycommand{%
This does always occur.\\
^^A 1 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 2 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 3 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 4 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 5 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
This also does always occur.
}%
\catcode`\^^A=\savedcatcode\relax
\begin{document}
\mycommand
\end{document}
This is a nice remark which does not get processed by LaTeX.
This is another nice remark which does not get processed by LaTeX.
产量:
, 尽管
\documentclass{article}
\edef\savedcatcode{\the\catcode`\^^A}%
%\catcode`\^^A=14 % <-lines starting with ^^A are comments.
\catcode`\^^A=9 % <- lines starting with ^^A are processed as it the ^^A wasn't there.
% Make sure that the things behind the ^^A, i.e., the things which either
% are to be kept or are to be commented out/are to be ignored, don't contain \input-
% directives or the like for reading and tokenizing input-files where the
% catcode of ^^A should not be changed.
\newcommand\mycommand{%
This does always occur.\\
^^A 1 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 2 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 3 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 4 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 5 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
This also does always occur.
}%
\catcode`\^^A=\savedcatcode\relax
\begin{document}
\mycommand
\end{document}
This is a nice remark which does not get processed by LaTeX.
This is another nice remark which does not get processed by LaTeX.
产量:
如果您愿意,您可以将此类 catcode 分配放入宏中,以便您可以通过在序言中选择适当的宏定义或通过设置/检查 -switch\if...
或其他方式在注释和保留内容之间切换:
\documentclass{article}
\newif\ifKeepTheThings
\KeepTheThingstrue
%\KeepTheThingsfalse
%---\SetTheCatcodeOfTheBeginHeaderChar / \RestoreTheCatcodeOfTheBeginHeaderChar
\newcommand\SetTheCatcodeOfTheBeginHeaderChar{%
\edef\SavedCatcodeOfTheBeginHeaderChar{\the\catcode`\^^A}%
\catcode`\^^A=\ifKeepTheThings9\else14\fi\relax
}%
\newcommand\RestoreTheCatcodeOfTheBeginHeaderChar{%
\catcode`\^^A=\SavedCatcodeOfTheBeginHeaderChar\relax
}%
\newcommand\SavedCatcodeOfTheBeginHeaderChar{}%
%
% Now you can use \SetTheCatcodeOfTheBeginHeaderChar and
% \RestoreTheCatcodeOfTheBeginHeaderChar throughout the document.
%
% The behavior of all instances of \SetTheCatcodeOfTheBeginHeaderChar and thus
% commenting/keeping can be switched by setting the if-switch \ifKeepThings
% in the preamble.
%
% Make sure that the things behind the ^^A, i.e., the things which either
% are to be kept or are to be commented out/are to be ignored, don't contain \input-
% directives or the like for reading and tokenizing input-files where the
% catcode of ^^A should not be changed.
%-------------------------------------------------------------------------------
\SetTheCatcodeOfTheBeginHeaderChar
\newcommand\mycommand{%
This does always occur.\\
^^A 1 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 2 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 3 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 4 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
^^A 5 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
This also does always occur.
}%
\RestoreTheCatcodeOfTheBeginHeaderChar
\begin{document}
\mycommand
\end{document}
This is a nice remark which does not get processed by LaTeX.
This is another nice remark which does not get processed by LaTeX.
顺便一提:
我希望没有人会为内联注释引入另一个类别代码或其他内容:这将要求 (La)TeX 关心匹配内联注释字符等,这将提供另一个可能变得混乱的领域。;-)