0

I want to define a macro to add comment in a .tex file. As shown in the following code, I want to comment any thing between \begincomment and \endcomment. However, it doesn't work. An error says "incomplete \iffalse". If I write

   \iffalse xxx \fi

directly, then all things work exactly the way I want. However, I think this code like \iffalse xx \fi is not so readable and it is better to give this sequence another name. Something like

    \begincomment
    \endcomment

Anyone can help me? I know some package that realize the macro \begin{comment}\end{comment}, but it doesn't work in math mode.

\documentclass{amsart}

\def\begincomment{\iffalse}
\def\endcomment{\fi}
\begin{document}

    ------------------

    \begincomment
    aaaaaaaaaaaaaaaa
    \endcomment

    ------------------
\end{document}

3 Answers 3

3

after \begincomment everything is skipped so \endcomment is not expanded to see the \fi

use

\let\ifcomment\iffalse

then you can use

\ifcomment

commented out stuff

\fi
5
  • Is there any way to close a 'if' statement without explicitly using 'fi' ? I means in any way to do this thing. For example, rename the command 'fi' to something else
    – gaoqiang
    May 3, 2020 at 13:14
  • 3
    @gaoqiang you can use \let\wibble=\fi then use \wibble but I'd advise against it. As you saw \if...\fi matching is different to normal tex group matching and has its own scanning rules that allow correctly nesting \if...\fi inside skipped sections, it is much easier to understand what is happening if all the if..fi matching commands start \ifxxx and end \fi May 3, 2020 at 15:02
  • Thank you very much! Learned a lot!
    – gaoqiang
    May 7, 2020 at 1:06
  • Actually, what I want is something that more readable to human, not more readable to the "lex program". I feel like that the \if ... \fi matching is not so readable for human, since I don't know what the \fi close (I mean it takes a while). If I work with another person to complete a single document, he/she may be confused and it's not friendly for my coworker. A \begincomment \endcomment style is more readable for human.
    – gaoqiang
    May 7, 2020 at 1:12
  • @gaoqiang sure but when the human is looking at \ifcomment zzzz \iffalse www \endcomment zzz \fi they may be confused and not realise that \endcomment is an alias for \fi and matches the \iffalse and the \fi matches the \ifcomment. It works but I'm not sure it is easier for humans to understand. Perhaps you would prefer the comment package and \begin{comment}...\end{comment} May 7, 2020 at 8:19
3

If the ignored text is balanced (i.e. it does not include {} or it includes them but only matching in pairs), then you can define

\long\def\begincomment #1\endcomment{}

All balanced text followed by \begincomment is read to the #1 parameter. This reading is terminated when the token \endcomment is found. The macro expands to nothing and the #1 parameter is simply forgotten.

1
  • Thank you very much! Learned a lot! I don't know whether it works in complex context.
    – gaoqiang
    May 7, 2020 at 1:16
1

If you use xparse, then you can define this as an environment:

\documentclass{article}
\usepackage{xparse}
\NewDocumentEnvironment{comment}{+b}{\iffalse #1\fi}{}

\begin{document}

  \begin{comment}
    This is a comment.
  \end{comment}

\end{document}
1
  • Thank you very much! That's exactly what I want. I tried the verbatim package, but it doesn't work well in complex contex, such as in math mode.
    – gaoqiang
    May 7, 2020 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.