Break or wrap a long word containing underscores in Latex

My situation is that I have many long words that containing underscores such as “how\_are\_you\_doing_?”, and they don’t break at the end of a line. Therefore, a line that contains these words sometimes is stretched over the page margin.

To fix this problem, we need to redefine the underscore command. Here is how I do it. I put the following Latex code after the \usepackage{} at the beginning of a Latex document.

%Redefine the underscore command
\let\underscore\_
\newcommand{\myunderscore}{\renewcommand{\_}{\underscore\hspace{0pt}}}

%Issue the changed underscore command to the whole document.
\myunderscore

Credit to the original post at http://newsgroups.derkeiler.com/Archive/Comp/comp.text.tex/2007-03/msg00647.html

Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

One Response to Break or wrap a long word containing underscores in Latex

  1. Franco says:

    thank you for this. really saved me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s