# Define C-like multiline comment

I am aware of other comment options, but I want to define /* as opening for mulitiline comment and */ as closing.

Any ideas?

• this is so far from standard tex syntax that any solution is going to be very fragile and only work if the document obeys lots of extra conditions. – David Carlisle Jun 24 '19 at 19:46

This might break other things (David points out that the braces in a comment must be balanced, for example and you can't have a % preceding the closing */ on the same line; also, other uses of / inside an argument will be trashed, for example a slash as part of an \includegraphics subdirectory filename).

This approach makes / active, which means it is now a macro name, and will absorb space after it, so if you use a / by itself, you may need to add {} following it, so that subsequent (desired) space is not suppressed.

I concur with David's strong recommendation NOT to use this approach, because of the gotchas!

\documentclass{article}
\let\svslash/
\catcode/=\active
\makeatletter
\def/{\@ifnextchar*{\ccomment}{\svslash}}
\makeatother
\long\def\ccomment*#1*/{}
\begin{document}
Here is/* a comment that I want
to go on

and on

and on*/ something that I am testing.  But a normal /{} passes through.
\end{document}


• but you can't comment out } or a % on the last line or ..... – David Carlisle Jun 24 '19 at 19:51
• \includegraphics{images/hmm} might be interesting as well. (note these are not criticsms of your answer, just trying to persuade the OP not to use this syntax:-) – David Carlisle Jun 24 '19 at 19:54

One can setup a parser with pgf's parser module such that it gobbles any token, the only problem is we have to make / active, so it can't be used in filenames, TikZ or pgf options, etc.

Also it changes the catcode of % locally, such that they can appear inside of a C-comment, though this will fail if the input in already tokenized (so if it is already part of a macro).

# Don't use this!

\documentclass[]{article}

\usepackage{pgf}
\usepgfmodule{parser}
\pgfparserdef{ccomment}{initial}*{\pgfparserswitch{star}}
\pgfparserdefunknown{ccomment}{star}{\pgfparserswitch{initial}}
\pgfparserdeffinal{ccomment}{\endgroup}
% we have to put this here, as the / would otherwise not work in the options
\pgfparserset{ccomment/silent=true}
\let\svslash/
\catcode\/=\active
\makeatletter
\def/{\@ifnextchar*{\@firstoftwo\ccomment}{\svslash}}
\makeatother
\pgfparserdef{ccomment}{star}/{\pgfparserswitch{final}}
\newcommand*\ccomment
{%
\begingroup
\catcode\%=12
\catcode\*=12 % just to make sure * has the correct meaning
\pgfparserparse{ccomment}%
}

\begin{document}
some /*comment that gobbles} %unbalanced{tokenlists*/ but a normal one /\ slips
through
\end{document}