Thursday, August 27, 2009

How to highlight text in LaTeX

Add the following to your preamble:
\usepackage{color}
\newcommand{\hilight}[1]{\colorbox{yellow}{#1}}
To highlight text in the body of your document, use
\hilight{this is some highlighted text}
Voila!

Of course, you can change the color from yellow to any of the supported colors. For a list of those, check: http://en.wikibooks.org/wiki/LaTeX/Colors

UPDATE:

There is a problem with the previous method is that the color box does not wrap properly. Here's a much easier and robust way of doing this

\usepackage{soul}
To highlight text in the body of your document, use
\hl{this is some highlighted text}

Cite as:
Saad, T. "How to highlight text in LaTeX". Weblog entry from Please Make A Note. http://pleasemakeanote.blogspot.com/2009/08/how-to-highlight-text-in-latex.html

21 comments:

  1. That doesn't quite work for me - the yellow box can't follow line wraps so the highlighted text goes off the edge of the page...

    ReplyDelete
  2. Hi Charlie, I have updated the post with a more robust method.

    ReplyDelete
  3. Hi, just wante to point out that sometimes normal use of the soul package causes \hl to create underlines. this can be fixed by

    \usepackage{color,soul}

    ReplyDelete
    Replies
    1. Thanks for the post ! works for me
      using windows 8.1, and Ubuntu 13.10

      Delete
  4. try \hl{\emph{some text}} to see how badly it breaks.

    In this case, \hilight command works much better!

    ReplyDelete
  5. This whole information is absolutely interesting. i like this blog because is easily understandable, and that is invaluable to the readers. Beyond all doubt La Tex will be very useful for everybody who want to prove it.
    buy viagra

    ReplyDelete
  6. whenever there is {} within the hilighted region {}, I got error messages using soul.

    ReplyDelete
  7. thanks for this info I need it for my blog usagamezone.blogspot.com

    ReplyDelete
  8. is there a way to highlight a letters in html ? please post a tutorial about how to do that easily.

    ReplyDelete
  9. Package soul works fine for me. I'm able to highlight text within LaTeX easily without undesired behaviour.

    However, is there an option to highlight text in one document using soul with different colours? For example line 1 with colour yellow, line 2 with colour magenta?

    Thanks for responses.

    ReplyDelete
  10. Package soul also works fine for me. Trouble with color package.

    ReplyDelete
  11. Awesome thanks!

    ReplyDelete
  12. I do not understand what is \usepackage{color}
    \newcommand{\hilight}[1]{\colorbox{yellow}{#1}}????? please help me!! I think that it is a little bit confused!!

    ReplyDelete
  13. @yNot and @Silencer, your combined solution did it for me, thanks!

    ReplyDelete
  14. I got error messages if I use multibyte languages in the \hl{...}.

    ReplyDelete
  15. If by chance you're using the beamer document class for presentations:
    \colorbox{yellow}{your text}

    ReplyDelete
  16. ichbinrene thanks dude, i was using "soul" but your post it's useful.

    ReplyDelete
  17. It works very well. Btw, how to change the color of highlight part?

    ReplyDelete
  18. To fix the emph break, dont include the initial { after emph. Then end the \hl command at the end of the text to be italicized. The ending } will end the emph as well as the \hl command. Start the \hl command again after the italicized part.

    ReplyDelete