LaTeX forum ⇒ Text Formattinginline comment and uncomment

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

inline comment and uncomment

Postby gaudetteje » Sun Apr 04, 2010 5:00 pm


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.

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




User avatar
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am
Location: Amherst, MA

Postby frabjous » Sun Apr 04, 2010 5:14 pm

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

  1. \documentclass{article}
  3. \newcommand{\ignore}[1]{}
  5. \begin{document}
  7. This is my first sentence.\ignore{This is a hidden comment.} This second sentence should display.
  9. \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.

Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Postby josephwright » Sun Apr 04, 2010 5:29 pm

You could use an active character (risky and untested):
  1. \catcode`\|=\active
  2. \def|#1|{}
  3. 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

Posts: 2
Joined: Sun Apr 04, 2010 3:01 pm

Postby gaudetteje » Mon Apr 05, 2010 3:01 am

Thanks for the help! Both appear to be equally valid. I think the "best" one comes down to personal preference.

Posts: 1
Joined: Wed Mar 09, 2011 6:58 pm

Postby aaronmswan » Wed Mar 09, 2011 7:08 pm

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.

  1. \documentclass{article}
  3. \newcommand{\ignore}[2]{\hspace{0in}#2}
  5. \begin{document}
  7. Notice there is a space before \ignore{This is a hidden comment.} and after this comment.
  9. \end{document}

Posts: 1
Joined: Mon Jul 15, 2013 4:10 pm

Postby stoiczek » Mon Jul 15, 2013 4:14 pm

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:

  1. This is my first sentence.
  2. %This is a hidden comment.
  3. This second sentence should display

should do the trick.

Return to “Text Formatting”

Who is online

Users browsing this forum: MSNbot Media and 4 guests