37

In my report, I'm writing some class names or variable names inside of a paragraph, and I want these names to be rendered in a monospace font.

Example:

This is my class name: \texttt{baseAdminConfiguration}.

Sometimes when the single word inside of the \texttt tag is rendered at the end of a line, the word does not go to the next line, and there is no break in it neither: the end of the word passes over the margin.

How should I handle such a case?

Cheers.

jersub
  • 472
  • 1
  • 4
  • 7

2 Answers2

44

This hasn’t got much to do with \texttt. The word is simply too long, and LaTeX doesn’t know how to hyphenate it. You can tell it how to do this manually, by declaring hyphenation rules:

\hyphenation{base-Admin-Configuration}

The \hyphenation command may take arbitrarily many words, separated by whitespace.

Alternatively, if this doesn’t the trick, you can introduce manual hypenation hints in the text:

This is a long text that uses the word \texttt{base\-Admin\-Configuration) …

Only the actual hyphenation will be displayed – unused so-called discretionary hyphens (\-) will not be displayed so you can freely sprinkle your text with them, if necessary.

[Read more about hyphenation in LaTeX]

To prevent LaTeX from overflowing lines in principle, the whole paragraph can be wrapped in a sloppypar environment (thanks to Will for pointing this out in the comments):

\begin{sloppypar}
  Some text …
\end{sloppypar}

This manipulates the parameters of the line-breaking algorithm (in particular, \tolerance). The downside: this can lead to very ugly spacing. Alternatively, \tolerance and other internal parameters can be manipulated directly – the TeX FAQ shows how.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Quick question: Is it possible to get LaTeX to break the line early instead of overrunning the margin? – Anon. Jan 21 '10 at 20:05
  • 1
    @Anon: well, you can always *force* a line break using `\\` or `\newline`. Or you might put the text in a `flushleft` environment – it won’t be justified then. But in general, no, not that I’m aware of. Perhaps it’s possible to tweak the internal parameters of the line-break algorithm. I don’t know. – Konrad Rudolph Jan 21 '10 at 20:08
  • 7
    The closest you can get is wrap the paragraph with `\begin{sloppypar}...\end{sloppypar}` to allow poorer linebreaks. (Or write `\sloppy` to activate it globally.) – Will Robertson Jan 22 '10 at 01:46
  • 1
    @Will: oh yes I forgot about `sloppypar`. `\sloppy`, on the other hand, is deprecated and shouldn’t be used. – Konrad Rudolph Jan 22 '10 at 10:05
  • Erm, news to me about \sloppy. Any reference for that? – Will Robertson Jan 22 '10 at 14:26
  • 1
    @Will: The source is l2tabu (section 1.8) from de.comp.text.tex which claims to echo a consensus among TeXperts. http://tug.ctan.org/tex-archive/info/l2tabu. But I won’t argue with the guy whose name appears in about every second result to LaTeX-related searches. – Konrad Rudolph Jan 22 '10 at 16:37
  • 1
    Ah, thanks! l2tabu warns against `\sloppy` in general and gives lots of better alternatives. But that isn't to say it's deprecated as such, just to be used with care. – Will Robertson Jan 23 '10 at 12:06
  • Thank you all for your great answers! Finally in my case I used manual hyphenation, as my class mane is only written once or twice in my report. I'm keeping in mind the \hyphenation and \sloppypar tags. – jersub Jan 23 '10 at 16:13
  • And how can I tell LaTeX not to use `-` in hyphenation? I have Perl module name, e.g. `XXX::YYY::ZZZ` and I want to break this name in that way: `XXX::-YYY::-ZZZ` but I don't want hyphen. – jesper Feb 27 '11 at 13:49
  • 1
    @jesper Never tried this but perhaps you can solve this by setting `\hyphenchar` to whitespace: ``\hyphenchar\font=`\ ``. – Konrad Rudolph Feb 27 '11 at 14:48
4

The solution is quite simple: use the url package and replace the texttt command with the path command.

I found out that here https://tex.stackexchange.com/questions/299/how-to-get-long-texttt-sections-to-break in the post of Will Robertson.

Cheers

Community
  • 1
  • 1
glarrain
  • 8,031
  • 7
  • 31
  • 44
  • This does not help, when we have long complex class names. It breaks pathes at the file separator only. And it also removes whitespaces, e.g. in a method’s parameter declaration. – fachexot Sep 21 '15 at 14:46