[Tex/LaTex] Prevent hyphenation in \texttt words

hyphenationsymbolstypewriter

Background

The text contains programming variable names such as report_city and SUBREPORT_FILE. All such variables are marked up with \texttt.

Problem

Some variable names are hyphenated:

SUBREPORT_-
FILE

I would prefer that the variables are not subjected to hyphenation.

Solution

A few ideas:

  • Redefine \texttt to wrap an \mbox{...} command around the content. Best solution?
  • Use \uchyph0. Did not work as expected.
  • Define all the words using \hyphenation{...}. The command \hyphenation{SUBREPORT\_FILE} did not compile, and defining all the words would be a second last resort.
  • Edit the entire document and put all variables inside \mbox{...} commands. I would consider this a last resort.

Question

How would you prevent the texts from being hyphenated?

Thank you!

Best Answer

\let\temptt\texttt
\renewcommand\texttt[1]{\mbox{\temptt{#1}}}

I have this break because latex doesn't like underscores in text mode, but I presume you have a way round this...

Related Question