GeneralSelective commenting out

LaTeX specific issues not fitting into one of the other forums of this category.
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Selective commenting out

Post by ptrcao »

I know about multi-line commenting out with verbatim package and the % sign. That's not what I'm asking in this question.

I want the ability to comment using % in a way that doesn't end up commenting out the entire line.

It is possible in html to do this with

Code: Select all

Alice in Wonderland <!-- by Lewis Carroll --> was the book this movie was based on.
However, within the context of LaTeX, the % symbol is inflexible. Everything that occurs after it on a line gets commented out. I want the ability to set an end boundary to my comment so I can selectively comment out parts of the line, without being forced to comment out everything after the % symbol.
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Selective commenting out

Post by 5gon12eder »

I know this is not exactly what you asked for but I'm afraid that there simply is no built in LaTeX feature for inline comments. However, there is a workaround that allows you to do something similar:

Code: Select all

In this work we \iffalse or me, who actually did all the work \fi could show something interesting.
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Selective commenting out

Post by ptrcao »

5gon12eder wrote:I know this is not exactly what you asked for but I'm afraid that there simply is no built in LaTeX feature for inline comments. However, there is a workaround that allows you to do something similar:

Code: Select all

In this work we \iffalse or me, who actually did all the work \fi could show something interesting.
I appreciate the suggestion...it might be a bit cumbersome for my purposes I'm afraid. I use comments a lot, and that would be too much effort and clutter... You're fairly confident no such commenting feature exists then? If you've used LaTeX regularly for a long time now, chances are your answer is definitive; but if you're only a light user there's still a chance such a feature might've eluded you. It was kind of you; I will stay open to any suggestions from yourself or anyone. Anybody else got ideas?
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Selective commenting out

Post by ptrcao »

Solution still outstanding but in the meantime a workaround:
http://tooloftrade.blogspot.com/2011/01 ... latex.html

In the workaround it is suggested that a macro be created which does nothing.

Code: Select all

\newcommand{\comment}[1]{}
Still doesn't look great to be honest, as in it detracts from readability:

Code: Select all

....although they may be observed at higher concentrations inside hosts~\cite{thompson_etal_2004}\comment{p 404}.  Their geographical distribution correlates with salinity and temperatures...
And care has to be taken re whitespaces.

Anyhow my question is in the context of the macro definition above what does the "[1]" do?
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Selective commenting out

Post by Juanjo »

The [1] in the definition of \comment means that this macro has one mandatory argument. When you write \comment{some text here}, LaTeX takes «some text here» as the argument of \comment. Since \comment does nothing with its argument, «some text here» is gobbled, it simply disappears.

I think a better definition could be

Code: Select all

\newcommand{\comment}[1]{\ignorespaces}
In this way, you have to worry about the leading spaces, not the trailing ones. Let me propose an even simpler solution. Choose a character in the keyboard you never use in your tex files. For example, if you are a Mac user, it could be the Apple logo (). Then use the xparse package to define the macro \ as in the code below, so that \ behaves as \comment, but: (a) its name is shorter, (b) the argument of \ is delimited by the macro itself and the own  character. Check the example:

Code: Select all

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{xparse}

\NewDocumentCommand\{+u{}}{\ignorespaces}

\begin{document}

It is quite easy now \ thanks to the help of LaTeX Community  to introduce
inline comments, which can even include \ this line is included in the inline comment

this line too

and even this one up to here  blank lines in the source file.

\end{document}
After typesetting, you should get a document with two lines:
«It is quite easy now to introduce inline comments, which can even include
blank lines in the source file.»

Imagine you choose | instead of . The above code will yield an error, since \| already exists. You may replace \NewDocumentCommand by \DeclareDocumentCommand to force LaTeX to accept your definition... or you may choose a different character.

The xparse package is worth knowing. It will be in the core of a future new release of LaTeX. See here a brief introduction.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Selective commenting out

Post by ptrcao »

Juanjo wrote:Choose a character in the keyboard you never use in your tex files. For example, if you are a Mac user, it could be the Apple logo ().
How would I key in windows/apple logo exactly? I'm in Windows now, but when I try to enter window logo in the definition, it just triggers my start menu! :?
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Re: Selective commenting out

Post by Juanjo »

I am afraid there is no Apple logo in a PC keyboard ;) . Just replace it with a different character, anyone you like, but not precisely the Windows logo :D
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Re: Selective commenting out

Post by ptrcao »

I'm finding it tricky to avoid conflicting with existing commands and uses. Is there a way to use two characters, like \## and ##?
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Selective commenting out

Post by Juanjo »

Without seeing your tests and correponding log files, I can't figure out what problems are you facing. I can use many unused characters, as ƒ, ¥, ∫, ®, etc. Anyway, you can also use one, two or more letters. For example, define

Code: Select all

\NewDocumentCommand\ZZ{+u{ZZ}}{\ignorespaces}
and then write

Code: Select all

It is quite easy now \ZZ thanks to the help of LaTeX Community ZZ to introduce...
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Selective commenting out

Post by ptrcao »

My keybord is rather limited, it may have to do with geographical differences in our keyboard layout. The only currency key appearing on my keyboard is $. I think I have an American style keyboard layout. I use Latex for versatile range of disciplines (and I'm a prolific user of packages for a high degree of customisation), so I can't rule out for example that I won't use ZZ in math as the product of two Zs. I do typset algebra so any combination of letters is risky.

I was going to use \# but obviously that's already reserved. So I thought to use \## but two successive symbols aren't interpreted properly by the macro...

I want to stick with symbols, but symbols that I don't use. Is the tilde symbol reserved for anything? Since when I typeset in math I use \sim and the like, I suppose the ~ symbol is disposable...

Edit: On second thought, no it isn't! ~ has a special use to mean a non-breaking space, for figure labeling and in citations. See what I mean? Everything is already reserved for other use...
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)
Post Reply