Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In LaTeX, % can be used for single-line comments. For multi-line comments, the following command is available in the verbatim package.

\begin{comment}
Commented code
\end{comment}

But is there a simple command like /* code */ in C?

share|improve this question
3  
Related/duplicate: Commenting out large sections –  Qrrbrbirlbel Dec 18 '12 at 7:22
2  
Also see: Block comments in TeX –  Aditya Dec 18 '12 at 8:03

4 Answers 4

No, but you can define something close:

\documentclass{article}
\begin{document}
\long\def\/*#1*/{}

AAA

\/* This is a test
    and this is another
*/

BBB
\end{document}
share|improve this answer
    
Can you make \* multiline comments *\ ? –  In PSTricks we trust Dec 17 '12 at 7:01
2  
@GarbageCollector You can but then it means changing the catcode of the backtick to make it active and would cause a lot of grief in other areas. Ah! did not see your edit ... changing catcode of slash will give you same headaches. –  Yiannis Lazarides Dec 17 '12 at 7:03
    
@YiannisLazarides: Of course, this definition cannot be nested. With LuaTeX it is possible to have multiline comments without any catcode jugglery. See Block comments in TeX for an example. –  Aditya Dec 18 '12 at 8:06
    
It redefines \* and requires a space after the closing delimiter but, \def\*#1*\ {} works I think. –  Scott H. Dec 22 '12 at 21:05

Following the C code paradigm, where one can use the preprocessor directives

#if 0
junk code
#endif

something similar can be done in TeX (and descendants):

\iffalse
I don't want this to happen
\fi

The commented parts can be easily activated by replacing \iffalse with \iftrue.

share|improve this answer
3  
See the TeX FAQ on why \iffalse can fail in unexpected ways. –  Aditya Dec 18 '12 at 8:04
    
@Aditya: thanks for the link, I wasn't aware of these problems! –  guillem Dec 18 '12 at 8:35

Here's a poor man's version of the answer linked by Aditya above. It doesn't require ConTeXt but it does need to be compiled with LuaTeX. With it you can use C style comments: /*comment*/. A potential downside is that it "works" even in a verbatim environment. It works in all situations I can think of.

This input:

enter image description here

Gives this output:

enter image description here

\documentclass{article}

\usepackage{luacode}

\begin{luacode*}

commenting = false
local gsub = string.gsub

local opencomm = "%/%*(.*)"
local closecomm = "(.-)%*%/"

function comment(s)
    if not commenting then
        s = gsub(s,opencomm,
                function(s1)
                commenting = true
                s1 =  gsub(s1,closecomm,function(s2) commenting = false return "" end,1)
                  if commenting then return "" else
                      return comment(s1)
                  end
              end
            )
    else
        s = gsub(s,"(.+)",
                function(s3)
                s3 =  gsub(s,closecomm,function(s4) commenting = false return "" end,1)
                    if commenting then return "%" else
                        return comment(s3)
                    end
                end
                )
    end
    return s
end

luatexbase.add_to_callback('process_input_buffer', comment, 'comment')
\end{luacode*}

\begin{document}

1 /*HIDDEN*/ 2

1 /*HIDDEN
HIDDEN*/ 2

1/*HIDDEN
HIDDEN
HIDDEN*/2

1 /*HIDDEN 
HIDDEN*/ 2 /*HIDDEN*/ 3

1 /*HIDDEN*/ 2 /*HIDDEN
HIDDEN*/ 3

\end{document}
share|improve this answer

I think the most straightforward way to do this is to use a TeX editor. TeXnicCenter for instance offers to comment and uncomment marked blocks by ctrl+q and ctrl+w. This simply adds a '%' at the respective lines. This further grays out commented section, which is not the case with scripted solutions.

share|improve this answer
1  
you can't have inline comments as such for example 2 /* HIDDEN HIDDEN HIDDEN */ 3 –  percusse Nov 27 '13 at 21:18
1  
This is true of course. A scripted version also lets hide/show comments by a simple compile flag, which can be great in drafting or reviewing work. –  Thomas Ortner Nov 27 '13 at 21:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.