3

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

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 ?

2
  • 3
    hiding environments behind commands is a bad habit to get into, for verbatim like environments (as here) it doesn't work at all, also environments like ams alignments and some table environments. Jan 29, 2021 at 20:04
  • @Ulrich Oups ! I edited my post. Jan 30, 2021 at 3:01

3 Answers 3

3

Would something like this work for you?

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

enter image description here

1
  • based on the same idea as Werner, ready to use ! I tried it in the real context and it works nicely ! Even "lighter" that to use a package. Great solution !! Jan 30, 2021 at 3:39
5

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}

enter image description here


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}

enter image description here


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
1
  • wow ! A complete study here ! Jan 30, 2021 at 3:20
2

The best option for you here is to avoid the functionality of comment and use your own interface. To do this, you \define 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:

enter image description here

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

When you use the "gobble" version (second, similar to \excludecomment), the output resembles this:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .