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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityFollowing 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
.
\begin{comment}
environment, either from the verbatim
package or comments
package.
A simple solution I use is
\newcommand{\mycomment}[1]{}
Which just defines a command that does nothing with the input (effectively commenting it out!)
Sample use:
\mycomment{
This line of text won't show
This one won't either
}
Edit: Replaced comment
with mycomment
since the former conflicts with the semi-popular comment
package. This way, if you reuse commented blocks in a project that uses the comment
package, nothing will break.
section*{}
(and not displaying it, why woudn't \renewcommand{\section}[1]{}
work ? Is there a work around? Thanks.
section*
blocks, you can do (on separate lines): \usepackage{suffix}
, \WithSuffix\newcommand\section*[1]{}
\iffalse ... \fi
suggestion.
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}
\*
and requires a space after the closing delimiter but, \def\*#1*\ {}
works I think.
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:
Gives this output:
\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}
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.
2 /* HIDDEN HIDDEN HIDDEN */ 3
Short: put \directlua{--
before the block and a single }
after the block when compiling with Lua(La)TeX.
Long: this is a "bug" of LuaTeX turned into a feature, or what make programmers happy. Putting a double hyphen in front of a text in Lua means a single-line comment; however, since TeX strips newlines from Lua code, this appears as a single-line chunk to Lua and so all the chunk gets commented.
Not tested, but I think you could alias that as
\newcommand{\multlinecomment}[1]{\directlua{-- #1}}
and then enclose your comment as in
\multlinecomment{
My multiline
comment
with Lorem ipsum.
}
This works for me and I think it is very simple and good enough. Highlight the text that you want to comment using your mouse or keyboard. Once highlighted use Ctrl+t
This will comment the selection. You can uncomment the selection by using same steps on commented text.
Hope it helps :)
Ctrl+/
as mentioned in one of the questions comments.
\usepackage{verbatim}
Ctrl + /
(command + /
on a Mac) to toggle commenting for selected lines. (Comment/ uncomment selected text).