我使用该包并希望用更短的宏(例如et )comment
替换\begin{comment}
and以及格式命令。\end{comment}
\ans
\sna
我尝试了这个:
\documentclass[10pt]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{ifthen}
\usepackage{comment}
\begin{document}
\setlength\parindent{0mm}
\newcommand{\ans}{\begin{detail}
\color{Plum}}
\newcommand{\sna}{
\end{detail}
\color{Black}}
\excludecomment{detail}
%\includecomment{detail}
Hello !\\
%\begin{detail}
\ans
I say hello because I have no better idea !\\
\sna
% \end{detail}
How are you ?
\end{document}
它失败了。事实上,正是它\sna
导致它失败。\ans
有点管用:除非我像这样定义 \ans,否则 Plum 颜色无效:
\newcommand{\ans}{\color{Plum}
\begin{detail}
}
我理解问题出在“comments”包文档中提到的“开始和结束命令应该出现在一行上。没有开始空格,后面没有任何内容。”。但我设法解决了这个问题,\ans
但没有解决\sna
。我看到了问题注释包和宏定义和处理和隐藏文本的宏?但没有找到解决我的问题的方法。
有没有办法定义\sna
它以完成这项工作?
答案1
这样的事情对你有用吗?
\documentclass{article}
\usepackage{xcolor}
\long\def\ansin#1\sna{\color{red}#1\color{black}}
\long\def\ansex#1\sna{}
\newcommand\excludecomment{\let\ans\ansex}
\newcommand\includecomment{\let\ans\ansin}
\excludecomment
\begin{document}
Hello!\\
\ans
I say hello because I have no better idea !\\
\sna
\includecomment
How are you?\\
Separate \ans lines are not \sna required.
\end{document}
答案2
\includecomment
对于通过/定义的环境\excludecomment
,需要检测此类环境实例的结束。
检测机制“假定”表示环境结束的短语可以通过在环境名称前面添加短语\end{
并附加短语来得出}
。
检测机制不会触发事物的扩展来检测该短语,但“期望”找到它逐字。
因此,使用
\ans
→\begin{detail}
和\sna
→\end{detail}
你可以执行
\excludecomment{detail}
... \ans
... \end{detail}
,但是你不能做
\excludecomment{detail}
... \ans
... \sna
:
\documentclass[10pt]{article}
\usepackage{comment}
\begin{document}
\newcommand{\ans}{\begin{detail}}
\newcommand{\sna}{\end{detail}}
\excludecomment{detail}
%\includecomment{detail}
Hello !\\
\ans
I say hello because I have no better idea !\\
\end{detail}
How are you ?
\end{document}
顺便一提:
\excludecomment{detail}
…… \begin{detail}
(和\end⎵{detail}
之间的空格)也不起作用。它与(没有空格)不一样。\end
{detail}
\end{detail}
如果你真的绝对希望\ans
..像..\sna
一样工作,你可以定义一个命令来保存当前颜色并破解命令和(它们是-environment 的组件),以便让-environment 扫描短语而不是短语。你需要对事物被标记化的 catcode-régime 挑剔——因此这很棘手。\begin{detail}
\end{detail}
\ans
\Enddetailtest
\AfterdetailComment
detail
detail
\sna
\end{detail}
你既不能做\begin{detail}
..\sna
也不能做\ans
..。\end{detail}
使用\ans
..时适用与..\sna
相同的规则。例如,必须独占一行,前面不能有空格,...\begin{detail}
\end{detail}
\sna
\documentclass[10pt]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{comment}
\setlength\parindent{0mm}
\begingroup
\makeatletter
\catcode`\^^M=12\relax%%%
\@firstofone{%
\endgroup%%%
\newcommand{\ans}{%
\let\savedEnddetailTest=\EnddetailTest%%%
\let\savedAfterdetailComment=\AfterdetailComment%%%
\colorlet{savedcurrentcolor}{.}%
{%
\escapechar=-1\relax%%%
\expandafter%%%
}\expandafter\def\expandafter\EnddetailTest\expandafter{\string\\sna}%
\toks@\expandafter{%
\AfterdetailComment%%%
\let\EnddetailTest=\savedEnddetailTest%%%
\let\AfterdetailComment=\savedAfterdetailComment%%%
\color{savedcurrentcolor}%
}%
\edef\AfterdetailComment{\the\toks@}%
\begin{detail}^^M\color{Plum}%
}%
}%
\begin{document}
\includecomment{detail}
Hello !\\
\ans
I say hello because I have no better idea !\\
\sna
Hello !\\
\begin{detail}
I say hello because I have no better idea !\\
\end{detail}
Hello !\\
\ans
I say hello because I have no better idea !\\
\sna
Hello !\\
\begin{detail}
I say hello because I have no better idea !\\
\end{detail}
\bigskip
\hrule
\bigskip
\excludecomment{detail}
Hello !\\
\ans
I say hello because I have no better idea !\\
\sna
Hello !\\
\begin{detail}
I say hello because I have no better idea !\\
\end{detail}
Hello !\\
\ans
I say hello because I have no better idea !\\
\sna
Hello !\\
\begin{detail}
I say hello because I have no better idea !\\
\end{detail}
\end{document}
在上述示例中,使用 -command 后,\ans
返回字符的 catcode(^^M
在 TeX 的^^
-notation 中)会更改为 12,以便将颜色规范处理为出现在新行上。
因此,使用\ans
..\sna
命令时事物为颜色Plum
,而使用\begin{detail}
..命令时\end{detail}
事物为颜色black
。
Plum
如果您希望同时使用\ans
..\sna
和\begin{detail}
..的颜色\end{detail}
,则可以省略这个 catcode-trickery 并detail
根据\specialcomment
而不是 来定义环境\includecomment
:
\documentclass[10pt]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{comment}
\setlength\parindent{0mm}
\newcommand{\ans}{%
\let\savedEnddetailTest=\EnddetailTest
\let\savedAfterdetailComment=\AfterdetailComment
{%
\escapechar=-1\relax
\expandafter
}\expandafter\def\expandafter\EnddetailTest\expandafter{\string\\sna}%
\csname toks@\endcsname\expandafter{%
\AfterdetailComment
\let\EnddetailTest=\savedEnddetailTest
\let\AfterdetailComment=\savedAfterdetailComment
}%
\edef\AfterdetailComment{\the\csname toks@\endcsname}%
\begin{detail}%
}%
\begin{document}
\specialcomment{detail}%
{\colorlet{savedcurrentcolor}{.}\color{Plum}}%
{\color{savedcurrentcolor}}
Hello !\\
\ans
I say hello because I have no better idea !\\
\sna
Hello !\\
\begin{detail}
I say hello because I have no better idea !\\
\end{detail}
Hello !\\
\ans
I say hello because I have no better idea !\\
\sna
Hello !\\
\begin{detail}
I say hello because I have no better idea !\\
\end{detail}
\bigskip
\hrule
\bigskip
\excludecomment{detail}
Hello !\\
\ans
I say hello because I have no better idea !\\
\sna
Hello !\\
\begin{detail}
I say hello because I have no better idea !\\
\end{detail}
Hello !\\
\ans
I say hello because I have no better idea !\\
\sna
Hello !\\
\begin{detail}
I say hello because I have no better idea !\\
\end{detail}
\end{document}
对于这个问题:
有没有办法定义 \sna 以使其完成这项工作?
在前两个例子中,命令\sna
根本没有定义。;-)
在编写此答案和编译/测试代码时,根据.log文件,正在使用以下文件/版本/版本:
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=pdflatex 2021.1.17) 29 JAN 2021 23:13
LaTeX2e patch level 4
L3 programming layer <2021-01-09> xparse <2020-03-03>
[...]
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
[...]
Package: xcolor 2016/05/11 v2.12 LaTeX color extensions (UK)
[...]
File: dvipsnam.def 2016/06/17 v3.0m Driver-dependent file (DPC,SPQR)
[...]
File: l3backend-pdftex.def 2021-01-09 L3 backend support: PDF output (pdfTeX)
[...]
/usr/local/texlive/2020/texmf-dist/tex/latex/comment/comment.sty
.log 文件仅显示文件的位置comment.sty
,而不是版本。
查看指定位置的文件显示:
% Comment.sty version 3.8, July 2016
% copyright 1998-2016 Victor Eijkhout
答案3
这里最好的选择是避免使用comment
并使用您自己的界面。为此,您\def
需要创建一个\long
版本\ans
,该版本可以捕获到的所有内容\sna
,之后您可以根据需要使用它。如果您不想在\ans
...之间包含任何内容\sna
,则可以将其丢弃。下面的示例提供了这两个选项;保留两个定义并取消注释您需要的任何一个:
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
% Similar to \includecomment for \ans...\sna
\long\def\ans#1\sna{{%
\color{Plum}#1\ignorespaces%
}}
% Similar to \excludecomment for \ans...sna
%\long\def\ans#1\sna{\ignorespaces}
\begin{document}
\setlength\parindent{0mm}
Hello !\\
\ans
I say hello because I have no better idea !\\
\sna
How are you ?
\end{document}
当您使用“gobble”版本(第二种,类似于\excludecomment
)时,输出类似于以下内容: