7

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}
DavidR
  • 1,107
  • No, not this way –  Jun 19 '15 at 20:02
  • 1
    Maybe you can achieve what you are looking for by introducing feature switches with \newif? After \newif\iffoo you can use \iffoo .. \else ..\fi and set the switch to true (\footrue) or false (\foofalse). – Christian Lindig Jun 19 '15 at 20:05
  • See the comment package; and perhaps the topics conditional compilation and editorial. – jon Jun 19 '15 at 20:05
  • @ChristianLindig This is not something I currently understand, but I will look into it. Thanks – DavidR Jun 19 '15 at 20:07
  • @ChristianLindig: This works, the drawback is, that this must be written right at the place where toggling should occur –  Jun 19 '15 at 20:10
  • @jon thanks, there looks to be the start of a solution here. – DavidR Jun 19 '15 at 20:10
  • @ChristianHupfer I take it that the idea is to put the comment before \else and then nothing after \else. Then the comment appears with \footrue but not \foofalse. This worked for me, but when I used the toggle before \begindocument I got an error "missing \begindocument, – DavidR Jun 19 '15 at 20:17
  • @DavidRowthorn: Of course, because you commented \begin{document} ;-) –  Jun 19 '15 at 20:32
  • @ChristianHupfer Aha! actually I had put my test example of a comment in the preamble with text. LaTeX didn't like that. When I made the comment a valid preamble command, it worked! thank you – DavidR Jun 19 '15 at 20:37
  • 1
    @DavidRowthorn: Yes -- if you use a real comment (indicating some explanation to some code, you don't use macros, just ordinary text) -- this is no problem in the document environment, but in the preamble, this text means typesetting as long as the % is removed –  Jun 19 '15 at 20:39

2 Answers2

9

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 %.

egreg
  • 1,121,712
  • And thanks for introducing me to the kantlipsum package. I wonder if there's a hegellipsum on the horizon – DavidR Jun 19 '15 at 21:22
0

If there is always a blank line after then you could do (untested)

\def\tcom#1\par{}

Eats everything until the next paragraph

Then the toggle would just be a redefinition of the macro

daleif
  • 54,450