2

So % is used to start an end of line comment, that's clear.
But is there a way to comment a part of code at once like in many other programming languages?

Examples:

  • HTML: <!-- comment -->
  • C++ : /* comment */
  • Matlab: %{ comment %}

This could be useful in two cases:

  1. To comment out a bunch of code at once (instead of putting a % in front of every line
  2. To comment "inline" within a paragraph/function/macro

I know many editors have shortcuts to comment text blocks, but I'm not looking for that.

5
  • 3
    Note that meny editors have features to comment out large chunks of text and also uncomment such a marked text.
    – daleif
    Mar 27, 2019 at 10:23
  • @daleif I'm aware of that but imagine having 200 lines of text to comment and uncomment frequently, you could place a <!-- at the beginning and an empty <!-- --> at the end, and removing the first <!-- will uncomment the whole text without having to select it all every time. (just an example) Mar 27, 2019 at 11:40
  • Actually the editor plus syntax highlighting is very fast. And if your editor does not support say <!-- ... --> highlighting, then you get the same issue as with the comment package, it is easy to overlook that this section of code is out commented. Commenting each line with % is just much more visible.
    – daleif
    Mar 27, 2019 at 11:49
  • Defining an "ignore" command could be useful for short stretches of, say, a few words: \newcommand{\ignore}[1]{}. But beware -- this isn't foolproof, and it will almost certainly mess up the spacing of the output. Mar 27, 2019 at 12:41
  • @Superuser You could as well use at the beginning of a line a character that does not occur in the input and switch its category code between 14 (comment) and 9 (ignored character). Mar 27, 2019 at 14:29

3 Answers 3

1

comment package is very good at this, even when the part inside the environment is invalid or in the preamble.

\documentclass{article}
\usepackage{lipsum} 
\usepackage{comment}
\begin{comment}
\usepackage{lipsumx}: I try to make an invalid package loading command
\usepackage{lipsumx} 
\usepackage{lipsumx} 
\usepackage{lipsumx} 
\usepackage{lipsumx} 
\usepackage{lipsumx} 
\usepackage{lipsumx} 
\usepackage{lipsumx} 
\usepackage{lipsumx} 
\usepackage{lipsumx} 
\end{comment}
\begin{document}
\lipsum[1-2]
\begin{comment}
\begin{enumerate}: No \ end{enumerate}, but still compilable!
\end{comment}
\begin{equation}
1
\begin{comment}
blah blah: or in math mode...
\end{comment}
+2=3
\end{equation}
\end{document}

However, as Ulrich Diez said in the comments, the environment is still not perfect. For example, in-line comment is impossible, because it is based on verbatim AFAIK. Hope that comment characters like /* */ in C will be added in the future.

3
  • 2
    It should be mentioned that the comment package neither is suitable for "inline-commenting" nor is suitable for commenting out blocks of text that occur within macro arguments or within the <balanced-text>s of assignments like, e.g., <definition>s or <token variable>-assignments. Mar 27, 2019 at 10:57
  • That's a good start! But as @UlrichDiez mentions, I was looking for a more flexible solution which also allows e.g. inline comments. Mar 27, 2019 at 11:38
  • @Superuser27 Endline-Comments via % are possible with (La)TeX. But Inline-Comments and the like are not possible with (La)TeX. Looking for more flexible solutions implies wasting time... ;-) Mar 27, 2019 at 11:52
1

You can try using below codes, on need basis:

\iffalse
 \lipsum[1-2]... 
\fi
6
  • You should note that it won't work with advanced coding. See here for more information. Btw, you can force a line break in Markdown using <br>, and format your code using four white spaces before each line of code. "Regards,..." are not necessary.
    – user156344
    Mar 27, 2019 at 12:37
  • @JouleV it should also be noted that a code block can be started and ended by using 3x ` signs. This is fairly new but won't require 4 spaces in front of each line. Mar 27, 2019 at 13:29
  • @Superuser27 And what about <pre><code>some code</code></pre> ? ;-) Mar 27, 2019 at 13:42
  • @Superuser27 Like LaTeX, there are many ways to do it ;)
    – user156344
    Mar 27, 2019 at 14:50
  • I have yet to come across an issue with \iffalse ... \fi in my normal documents. Of course unmatched \if... \fi constructs are an issue but one should (hopefully) rarely encounter those in the wild. As far as those limitations go it is probably even better than \begin{comment}...\end{comment} in many situations.
    – moewe
    Mar 27, 2019 at 16:46
1

Inline-comments are not possible.

Endline-comments by means of % (or any other character whose category-code at the time of reading and tokenizing the input in question is 14(comment) ) are possible.

(You can also use characters of category-code 5(end of line) for having (La)TeX cease tokenizing the current line of input. The subtle difference to using a character of category code 14(comment) is: If you use a character of category code 5 (end of line), depending on the state of the reading-apparatus you get (in state N=new line) a \par-token or (in state M=middle of line) a space-token or (in state S=skipping blanks) no token at all for this category-code-5-character. Afterwards (La)TeX will start tokenizing the next line and thus the reading-apparatus will be in state N. (La)TeX reads input line by line and processes each line character by character. Usually (La)TeX does remove space-characters (the space character's code-point has the number 32 both in ASCII and in UTF-8) from line-endings and then does attach a character whose code point's number in (La)TeX's internal encoding (which either is ASCII or is UTF-8) equals the number of the integer-parameter \endlinechar before further processing that line of input. Usually \endlinechar has the number 13 and 13 is the number of the code point of the return-character in (La)TeX's internal encoding (which either is ASCII or is UTF-8), and the return-character/character 13 has category code 5. That's why usually an empty line/two consecutive line-breaks in the input yield the token \par because at the second of these two linebreaks, (La)TeX will encounter the endline-character of category code 5 while the reading apparatus is in state N.)

There are approaches which aim at having (La)TeX read and tokenize things and then carry out the resulting tokens in a way where they vanish without yielding visible output. E.g. the approach of using the comment-package. E.g., the approach of using things like \iffalse... comment...\fi or \@gobble{...comment...}. But all those approaches are not foolproof.

E.g., you cannot use them for whatsoever commenting inside macro-arguments or within the ⟨balanced text⟩ of a definition:

If you do

\newcommand\foobar{%
  This is the definition of foobar.
  \begin{comment} 
  This is a comment.
  \end{comment}
}

, you will have all the tokens that form the comment-environment within the ⟨replacement text⟩ of \foobar. Besides this, while gathering the ⟨replacement text⟩ for \foobar, the phrase \end{comment} will have been tokenized under normal catcode-régime while the mechanism underlying the comment-environment "expects" it to be tokenized under verbatim-catcode-régime and thus will be broken.

The only foolproof approach for commenting known to me is commenting in terms of endline-comments via %/via characters of category code 14(comment) as this is the only approach where characters from the input at the stage of tokenizing don't yield tokens at all.

Another thing that one can sometimes do is putting remarks and the like at the end of the code, i.e., behind the \bye- or \stop- or \end{document}- or \endinput-command.

If you wish to frequently switch between commenting and not commenting some lines of input, you can do that by starting these lines with a character which usually does not occur in your .tex-input-files and switching the catcode of that character between 14 (comment) and 9(ignored). You can type characters by means of ^^-notation.

E.g., I never use the "start-of-heading"-character whose code point's number is 1 both in ASCII and in UTF-8 while in (La)TeX under normal catcode-conditions one can type that character as ^^A.

E.g.,

\documentclass{article}

\edef\savedcatcode{\the\catcode`\^^A}%
\catcode`\^^A=14 % <- lines starting with ^^A are comments.
%\catcode`\^^A=9 % <- lines starting with ^^A are processed as it the ^^A wasn't there.

% Make sure that the things behind the ^^A, i.e., the things which either
% are to be kept or are to be commented out/are to be ignored, don't contain \input-
% directives or the like for reading and tokenizing input-files where the
% catcode of ^^A should not be changed.

\newcommand\mycommand{%
   This does always occur.\\
   ^^A 1 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 2 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 3 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 4 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 5 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   This also does always occur.
}%
\catcode`\^^A=\savedcatcode\relax

\begin{document}
\mycommand
\end{document}
This is a nice remark which does not get processed by LaTeX.
This is another nice remark which does not get processed by LaTeX.

yields:

enter image description here

, while

\documentclass{article}

\edef\savedcatcode{\the\catcode`\^^A}%
%\catcode`\^^A=14 % <-lines starting with ^^A are comments.
\catcode`\^^A=9 % <- lines starting with ^^A are processed as it the ^^A wasn't there.

% Make sure that the things behind the ^^A, i.e., the things which either
% are to be kept or are to be commented out/are to be ignored, don't contain \input-
% directives or the like for reading and tokenizing input-files where the
% catcode of ^^A should not be changed.

\newcommand\mycommand{%
   This does always occur.\\
   ^^A 1 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 2 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 3 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 4 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 5 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   This also does always occur.
}%
\catcode`\^^A=\savedcatcode\relax

\begin{document}
\mycommand
\end{document}
This is a nice remark which does not get processed by LaTeX.
This is another nice remark which does not get processed by LaTeX.

yields:

enter image description here

You can put such catcode-assignments into macros if you wish so that you can switch between commenting and keeping things by choosing the appropriate macro-definition in the preamble or by setting/checking an \if...-switch or whatever:

\documentclass{article}

\newif\ifKeepTheThings
\KeepTheThingstrue
%\KeepTheThingsfalse

%---\SetTheCatcodeOfTheBeginHeaderChar / \RestoreTheCatcodeOfTheBeginHeaderChar
\newcommand\SetTheCatcodeOfTheBeginHeaderChar{%
  \edef\SavedCatcodeOfTheBeginHeaderChar{\the\catcode`\^^A}%
  \catcode`\^^A=\ifKeepTheThings9\else14\fi\relax
}%
\newcommand\RestoreTheCatcodeOfTheBeginHeaderChar{%
   \catcode`\^^A=\SavedCatcodeOfTheBeginHeaderChar\relax
}%
\newcommand\SavedCatcodeOfTheBeginHeaderChar{}%
%
% Now you can use \SetTheCatcodeOfTheBeginHeaderChar and
% \RestoreTheCatcodeOfTheBeginHeaderChar throughout the document.
%
% The behavior of all instances of \SetTheCatcodeOfTheBeginHeaderChar and thus
% commenting/keeping can be switched by setting the if-switch \ifKeepThings 
% in the preamble.
%
% Make sure that the things behind the ^^A, i.e., the things which either
% are to be kept or are to be commented out/are to be ignored, don't contain \input-
% directives or the like for reading and tokenizing input-files where the
% catcode of ^^A should not be changed.
%-------------------------------------------------------------------------------

\SetTheCatcodeOfTheBeginHeaderChar
\newcommand\mycommand{%
   This does always occur.\\
   ^^A 1 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 2 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 3 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 4 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   ^^A 5 This does only occur when \texttt{\string^\string^A}'s catcode is not 14.\\
   This also does always occur.
}%
\RestoreTheCatcodeOfTheBeginHeaderChar

\begin{document}
\mycommand
\end{document}
This is a nice remark which does not get processed by LaTeX.
This is another nice remark which does not get processed by LaTeX.

By the way:

I hope that nobody will introduce another category-code or whatever for inline-comments: This would require (La)TeX to care about matching inline-comment-characters and the like and this would provide another area where things could get messy. ;-)

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