Text Formattinginline comment and uncomment

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
gaudetteje
Posts: 2
Joined: Sun Apr 04, 2010 3:01 pm

inline comment and uncomment

Post by gaudetteje »

Hi,

I have a paragraph of text and want to comment out only part of it. I could place a '%' to start the comment and then use a single <CR> after the end of the comment. Is there another way so that I don't need to place <CR>s after every comment? Below is an example of what I'm trying to do.

Code: Select all

This is my first sentence.  %This is a hidden comment.%  This second sentence should display.
This code should display:
This is my first sentence. This second sentence should display.
Thanks,

-J

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

inline comment and uncomment

Post by frabjous »

Best I can think of off the topic of my head would be to define a command that doesn't do anything, e.g.:

Code: Select all

\documentclass{article}

\newcommand{\ignore}[1]{}

\begin{document}

This is my first sentence.\ignore{This is a hidden comment.} This second sentence should display.

\end{document}
Though you'll need to be careful with spaces. (E.g., don't put a space both before and after it.)

I think there are some better options, perhaps through the comment package, but I've never been able to make that package work as I wanted.

Perhaps someone else will be able to offer better advice.
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

inline comment and uncomment

Post by josephwright »

You could use an active character (risky and untested):

Code: Select all

\catcode`\|=\active
\def|#1|{}
Some text.|Invisible| Visible.
This will go wrong if you don't properly close the line ending, but things will recover at the end of a paragraph.
Joseph Wright
gaudetteje
Posts: 2
Joined: Sun Apr 04, 2010 3:01 pm

Re: inline comment and uncomment

Post by gaudetteje »

Thanks for the help! Both appear to be equally valid. I think the "best" one comes down to personal preference.
aaronmswan
Posts: 1
Joined: Wed Mar 09, 2011 6:58 pm

inline comment and uncomment

Post by aaronmswan »

I'm somewhat new to Latex, but I think the following code makes a small improvement by ignoring white space after an inline command. It accepts the next non-whitespace character as a second argument and then displays it with no space in front of it.

Code: Select all

\documentclass{article}

\newcommand{\ignore}[2]{\hspace{0in}#2}

\begin{document}

Notice there is a space before \ignore{This is a hidden comment.} and after this comment.

\end{document}
stoiczek
Posts: 1
Joined: Mon Jul 15, 2013 4:10 pm

inline comment and uncomment

Post by stoiczek »

Hi all,

On a slight note - do you really need to use such a complex solution? I may be wrong, but as far as I know, to create a new paragraph, you need to put an empty line. Thus solution like this:

Code: Select all

This is my first sentence. 
%This is a hidden comment.
This second sentence should display
should do the trick.
Post Reply