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:

, 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:

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. ;-)
<!--
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)<!-- ... -->
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.\newcommand{\ignore}[1]{}
. But beware -- this isn't foolproof, and it will almost certainly mess up the spacing of the output.