views:

1145

answers:

1

Hi there!

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.

+7  A: 

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
Quick question: Is it possible to get LaTeX to break the line early instead of overrunning the margin?
Anon.
@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
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
@Will: oh yes I forgot about `sloppypar`. `\sloppy`, on the other hand, is deprecated and shouldn’t be used.
Konrad Rudolph
Erm, news to me about \sloppy. Any reference for that?
Will Robertson
@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
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
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.
BigMadWolf