Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In my LaTeX document, I have a few special terms that contain underscores, such as AAA_BBB_CCC. In one case, such a token is placed at the end of the line, and for some reason it reaches into the margin.

I like the fact that it the token is not divided into subtokens, but ideally, if it doesn't fit on the line, I'd like the whole term to go to the next line or in any case keep the right margin intact.

I'm currently writing the terms in the source like so:

bla bla bla My\_underscored\_term bla bla bla

Is there an alternative that will keep the margin intact?

share|improve this question
I once read in a LaTeX book the suggested solution to this kind of problems. It shocked my believe in LaTeX first, but now I agree: Rephrase your text! This way the long term is not at the end of a line. It is most the time much easier than to adjust/redefine things. –  Martin Scharrer Jan 31 '11 at 17:26
add comment

3 Answers

up vote 8 down vote accepted

I think this doesn't have much to do with the underscore, but more about having a long(ish) word, with no clear indication of how to hyphenate, near the end of a line. (La)TeX did what it did because it thought there wasn't really any good point where to break the line: keeping the word in the line makes it go into the margin, moving the word to the next line would require rather large spaces between the words in order to fill the line.

This is probably not the answer that you hoped for, but the most sensible solution is trying to reword a bit your paragraph (add or remove a few words here and there) so that this long non-hyphenated words do not end up near the end of a line.

You could also force LaTeX to choose the other suboptimal alternative (i.e. moving the whole word to the next line and fill the current line with long spaces) by explicitly adding a \linebreak, e.g.

... bla bla bla\linebreak My\_underscored\_term bla bla bla

The other alternative, as Leo Liu pointed out, is to redefine \_ so that it allows to break the long word at the underscores. This may or may not be acceptable to you.

share|improve this answer
Thanks a lot! If LaTeX has to choose between those two options, both of which are suboptimal, is there a way I could force it to go for the other option that will result in more (ugly) spaces in the previous line? –  Thomas Jan 31 '11 at 13:40
@Thomas: Yes, I've edited my answer to show how it is done. –  Juan A. Navarro Jan 31 '11 at 13:48
I'm not sure what's uglier, the resulting spaces or your answer, but thanks anyway ;-) –  Thomas Jan 31 '11 at 13:56
Maybe \sloppy or sloppypar environment could help. However, I don't like them. –  Leo Liu Jan 31 '11 at 17:25
add comment

You can redefine \_:



share|improve this answer
Thank you very much, but as I stated in the question, this is exactly what I'd like to avoid. But it's certainly a good answer for others with a similar problem. –  Thomas Jan 31 '11 at 13:39
add comment

You can simply include the following line in your code:-


I think it will resolve the problem.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.