# Prevent hyphenation in \texttt words

## 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!

-
why can't you use \verb|...| or \lstinline|...| ? – Herbert Jan 25 '11 at 9:33
@Herbert: There are hundreds, if not thousands of instances. I would prefer not to re-edit them all. – Dave Jarvis Jan 25 '11 at 9:34
@Dave: ok, but it is only a search and replace which can be done in one run over all files with eg Perl – Herbert Jan 25 '11 at 9:36
@Herbert: The document is being edited with LyX. As such, I would prefer keeping everything as \texttt, rather than adding more evil red text. But that is a possibility. – Dave Jarvis Jan 25 '11 at 9:38
@Dave: did you load the package hyphenat? If yes then comment it out – Herbert Jan 25 '11 at 9:52

\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...

-
LyX automatically escapes the underscores (\\_). And thank you. This works flawlessly. – Dave Jarvis Jan 25 '11 at 17:59
@Dave Aha! That's clever of LyX. – Seamus Jan 26 '11 at 8:33

The problem is not the texttt as is evident from the short code below!

\documentclass{article}
\usepackage{lipsum}
\def\printhyphens{\everypar{\setbox0\lastbox \setbox1\hbox{\strut}\vbox\bgroup
\everypar{\setbox0\lastbox \nobreak\hskip0pt\relax}\dimen0=\hsize
\hsize=0pt \hfuzz\maxdimen \def\par{\endgraf \hsize=\dimen0\getlastline
\egroup\endgraf}}\offinterlineskip\breakafterword}

\def\breakafterword{\catcode\^^M\active\catcode\ \active}
{\breakafterword\gdef^^M{\par}\global\let ^^M}

\def\getlastline{\setbox0\lastbox\ifvoid0\let\next\nomorelines
\else\unskip\unpenalty\setbox1\hbox{\unhbox0\strut\discretionary{}{}{}%
\unhbox1}\let\next\getlastline\fi\next}

\def\nomorelines{\unhbox1}
\begin{document}

\begingroup

\printhyphens
photograph photography photographer
photographical photographically
hypersupersuperdupercali%

\texttt{fragilisticexpihalidocious}

\texttt{photographer}
\end{document}


If we can see a minimal may be someone can find a solution!

-
The problem is breaking at underscores. Underscores cause an error on my system. So we need to know what @Dave Jarvis is doing with underscores to see the problem. – Seamus Jan 25 '11 at 13:49
@Seamus it appears as the most likely cause for this. – Yiannis Lazarides Jan 25 '11 at 14:41
Thank you for taking the time to look into this. – Dave Jarvis Jan 25 '11 at 18:38

It might help to put

\texttt{\hyphenchar\font=-1 }


early in your document. But I wonder why it is needed: When I try, the hyphenchar of the texttt font is already set to -1, as evidenced by running

\texttt{\showthe\hyphenchar\font}


But maybe you are using a different fixed-width font?

-
 Thank you. LyX defines a "TypeWriter" font, which marks-up the text using \texttt'. – Dave Jarvis Jan 25 '11 at 17:38