13

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?

1
  • 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. Jan 31, 2011 at 17:26

3 Answers 3

14

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.

4
  • 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, 2011 at 13:40
  • @Thomas: Yes, I've edited my answer to show how it is done. Jan 31, 2011 at 13:48
  • I'm not sure what's uglier, the resulting spaces or your answer, but thanks anyway ;-)
    – Thomas
    Jan 31, 2011 at 13:56
  • 2
    Maybe \sloppy or sloppypar environment could help. However, I don't like them.
    – Leo Liu
    Jan 31, 2011 at 17:25
16

You can redefine \_:

\renewcommand\_{\textunderscore\linebreak[1]}

or

\renewcommand\_{\textunderscore\allowbreak}
2
  • 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, 2011 at 13:39
  • The hyphenat package does basically the same: \DeclareRobustCommand{\_}{\ifmmode\nfss@text{\textunderscore}\else\BreakableUnderscore\fi}
    – ypid
    May 25, 2015 at 21:42
4

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

\usepackage{underscore}

I think it will resolve the problem.

1
  • 1
    This seems to fail when I have: \texttt{\detokenize{here_is_my_file.xml}} (the goal being avoiding having to escape every underscore) Sep 29, 2014 at 22:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .