TeX - LaTeX Asked by user1771398 on April 21, 2021
I use the comment
package and wish to replace the begin{comment}
and end{comment}
, together with format commands, with macro shorter spelled such as ans
et sna
.
I tried this :
documentclass[10pt]{article}
usepackage[dvipsnames]{xcolor}
usepackage{ifthen}
usepackage{comment}
begin{document}
setlengthparindent{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}
It fails. In fact, it is the sna
that makes it fail. ans
works, sort of : the Plum colour is ineffective except if I define ans like that :
newcommand{ans}{color{Plum}
begin{detail}
}
I understand the problem is around the "The opening and closing commands should appear on a line of their own. No starting spaces, nothing after it." stated in the documentation for the "comments" package. But I managed to work it around for ans
but not for sna
. I saw the questions comment-package and macro definitions and A macro that processes and hides text? but didn’t find the solution for my problem.
Is there a way to define sna
so that it does the job ?
Would something like this work for you?
documentclass{article}
usepackage{xcolor}
longdefansin#1sna{color{red}#1color{black}}
longdefansex#1sna{}
newcommandexcludecomment{letansansex}
newcommandincludecomment{letansansin}
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}
Correct answer by Steven B. Segletes on April 21, 2021
The best option for you here is to avoid the functionality of comment
and use your own interface. To do this, you def
ine a long
version of ans
that captures everything up to sna
, after which you can do with it what you want. If you don't want to include anything between ans
...sna
, you can then just discard it. The example below provides both those options; keep both definitions and uncomment whichever you need:
documentclass{article}
usepackage[dvipsnames]{xcolor}
% Similar to includecomment for ans...sna
longdefans#1sna{{%
color{Plum}#1ignorespaces%
}}
% Similar to excludecomment for ans...sna
%longdefans#1sna{ignorespaces}
begin{document}
setlengthparindent{0mm}
Hello !
ans
I say hello because I have no better idea !
sna
How are you ?
end{document}
When you use the "gobble" version (second, similar to excludecomment
), the output resembles this:
Answered by Werner on April 21, 2021
With environments defined via includecomment
/excludecomment
, the end of an instance of such an environment needs to be detected.
The detection-mechanism "assumes" that the phrase denoting the end of the environment can be derived by prepending to the name of the environment the phrase end{
and appending the phrase }
.
The detection-mechanism does not trigger expansion of things for detecting that phrase but "expects" to find it verbatim.
Therefore with
ans
→ begin{detail}
and sna
→ end{detail}
you can, e.g., do
excludecomment{detail}
... ans
... end{detail}
, but you cannot do
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}
By the way:
excludecomment{detail}
... begin{detail}
... end⎵{detail}
(space between end
and {detail}
) doesn't work either. It is not the same as end{detail}
(without space).
If you really absolutely want ans
..sna
to work like begin{detail}
..end{detail}
, you can define a command ans
which saves the current color and hacks the commands Enddetailtest
and AfterdetailComment
(which are components of the detail
-environment) in order to have the detail
-environment scan for the phrase sna
instead of the phrase end{detail}
. You need to be picky about the catcode-régime under which things get tokenized—therefore this is tricky.
You can do neither begin{detail}
.. sna
nor ans
..end{detail}
.
With ans
..sna
the same rules apply as with begin{detail}
..end{detail}
. E.g., sna
must be on a line of its own, not preceeded by spaces, ...
documentclass[10pt]{article}
usepackage[dvipsnames]{xcolor}
usepackage{comment}
setlengthparindent{0mm}
begingroup
makeatletter
catcode`^^M=12relax%%%
@firstofone{%
endgroup%%%
newcommand{ans}{%
letsavedEnddetailTest=EnddetailTest%%%
letsavedAfterdetailComment=AfterdetailComment%%%
colorlet{savedcurrentcolor}{.}%
{%
escapechar=-1relax%%%
expandafter%%%
}expandafterdefexpandafterEnddetailTestexpandafter{stringsna}%
toks@expandafter{%
AfterdetailComment%%%
letEnddetailTest=savedEnddetailTest%%%
letAfterdetailComment=savedAfterdetailComment%%%
color{savedcurrentcolor}%
}%
edefAfterdetailComment{thetoks@}%
begin{detail}^^Mcolor{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}
With the example above with the ans
-command the catcode of the return-character (^^M
in TeX's ^^
-notation) is changed to 12 in order to have the color-specification processed as if occurring on a new line.
Therefore with the ans
..sna
-command things are in the color Plum
while with begin{detail}
..end{detail}
things are in the color black
.
If you wish the color Plum
both with ans
..sna
and with begin{detail}
..end{detail}
, you can omit this catcode-trickery and define the detail
-environment in terms of specialcomment
instead of includecomment
:
documentclass[10pt]{article}
usepackage[dvipsnames]{xcolor}
usepackage{comment}
setlengthparindent{0mm}
newcommand{ans}{%
letsavedEnddetailTest=EnddetailTest
letsavedAfterdetailComment=AfterdetailComment
{%
escapechar=-1relax
expandafter
}expandafterdefexpandafterEnddetailTestexpandafter{stringsna}%
csname toks@endcsnameexpandafter{%
AfterdetailComment
letEnddetailTest=savedEnddetailTest
letAfterdetailComment=savedAfterdetailComment
}%
edefAfterdetailComment{thecsname 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}
To the question:
Is there a way to define sna so that it does the job ?
With the previous two examples the command sna
isn't defined at all. ;-)
When writing this answer and compiling/testing the code, according to the .log-file the following files/releases/versions were in use:
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
The .log-file only revealed the location of the file comment.sty
, not the version.
Looking at the file at the denoted location revealed:
% Comment.sty version 3.8, July 2016
% copyright 1998-2016 Victor Eijkhout
Answered by Ulrich Diez on April 21, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP