[Tex/LaTex] way to toggle comments

comments

I'm looking for a way to turn toggle the "comment" status of certain comments within a latex document. The idea is that throughout the document, including the preamble, there are specific commented-out lines that I can effectively toggle on and off, from comment to printed line. The following MWE shows my attempt to instigate this using a command, but unfortunately it just prints the % character:

% Compiles with XeLaTeX

\documentclass{memoir}

\usepackage{polyglossia}
\setmainlanguage{english}


\newcommand{\tcom}{\%} % I was hoping changing this as needed would work.

\tcom comment in preamble that can be toggled.

\begin{document}

% comment that should remain hidden.
\tcom comment that can be toggled.

\end{document}

Best Answer

Since you're using XeLaTeX you have thousands of characters you don't use in your document. Choose one that you can easily type (maybe with a shorthand in your editor). Here I use TIBETAN SYLLABLE OM

\documentclass{memoir}

\usepackage{polyglossia}
\setmainlanguage{english}

\newif\ifshowcomments
%\showcommentstrue % uncomment to show the comments

\ifshowcomments
  \catcode`ༀ=9 % ignored
\else
  \catcode`ༀ=14 % comments
\fi

ༀ \usepackage{kantlipsum} %comment in preamble that can be toggled.

\begin{document}

Some text

% comment that should remain hidden.
ༀ comment that can be toggled.

\end{document}

If you uncomment the line \showcommentstrue the chosen character will be ignored, otherwise it will act exactly like %.

Related Question