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}
\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}

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}
\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}

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