定义命令来注释文本

定义命令来注释文本

我想创建一个\comment{}不会编译 { } 内任何内容的命令。

为什么我不使用%或?因为我想将一些文本放在括号内并对其进行注释。\begin{comment} bla bla \end{comment}\usepackage{comment}

我想要这样做的原因是我使用了很多红色的文本\textcolor{red}{bla bla},我想重新定义,\textcolor{red}以便我可以从我的文档中删除所有红色的内容(请注意,我有很多红色的部分......)。

简而言之,我当前的代码

x=x+1 \\
\textcolor{red}{
    I don't want to see this
}

产生这个

在此处输入图片描述

相反,我希望它能产生这样的效果

在此处输入图片描述

而不必手动用 注释每个部分\textcolor{red}。我反而想重新定义此命令,以便它用红色注释文本。

笔记:我希望红色文本不会被编译,也不会显得不可见,从而产生空白。

答案1

\suppress 使用命令选择性地抑制排版输出的生成重新定义\textcolor

原始文件 X

重新定义\textcolor

X

\documentclass[12pt,a4paper]{article}

\usepackage{xcolor}
\usepackage{ifthen}

\let\oldtextcolor\textcolor 
\renewcommand{\textcolor}[2]{% <<<<<<<<<<<<<<<<<<<<<<
    \ifthenelse{\equal{red}{#1}}
    {\suppress#2\endsuppress}
    {\oldtextcolor{#1}{#2}}%
}

%% From https://tex.stackexchange.com/a/97360/161015
\makeatletter   
\font\dummyft@=dummy \relax
\def\suppress{%
    \begingroup\par
    \parskip\z@
    \offinterlineskip
    \baselineskip=\z@skip
    \lineskip=\z@skip
    \lineskiplimit=\maxdimen
    \dummyft@
    \count@\sixt@@n
    \loop\ifnum\count@ >\z@
    \advance\count@\m@ne
    \textfont\count@\dummyft@
    \scriptfont\count@\dummyft@
    \scriptscriptfont\count@\dummyft@
    \repeat
    \let\selectfont\relax
    \let\mathversion\@gobble
    \let\getanddefine@fonts\@gobbletwo
    \tracinglostchars\z@
    \frenchspacing
    \hbadness\@M}
\def\endsuppress{\par\endgroup}
\makeatother

\begin{document}

As any dedicated reader can clearly see, the Ideal of practical reason is a
representation of, as far as I know, the things in themselves; as I have shown
elsewhere, the phenomena should only be used as a canon for our under-
standing.

x=x+1 

\noindent \textcolor{red}{I don't want to see this \\ I don't want to see this  \\ I  don't want to see this.}

Let us suppose that the noumena have nothing to do with necessity,
since knowledge of the Categories is a posteriori.

{\color{red} But I want to see this.}

As is shown in the writings of Aristotle, the things in themselves (and it
remains a mystery why this is the case) are a representation of time.

\textcolor{green}{And also to see this.}

 Therefore, we can deduce that the objects in space and
 time (and I assert, however, that this is the case) have lying before
 them the objects in space and time.

\end{document}

来自引用的链接

然而,规则、根号、分数线和一些其他东西(主要是数学)可以逃脱。

相关内容