3

I'm looking for a way to nicely set code inside my document. I used \def\code#1{{\texttt{#1}}}, but it still allows the text to be hyphenated. To prevent that, I tried the advise from Prevent hyphenation and use line break in block of text and tried \def\code#1{{\mbox{\texttt{#1}}}}, but then the font size is lost when I write, for example, T_{\code{FOO}}. Unfortunately, the \nohyphens{...} solution clashed with some other package (as I understand it) and the \hyphenpenalty 10000 solution produced no changes.

While preparing the MWE I found that the hyphenation only happens after underscores.

It can't be that hard to get what I want, or is it?

Here is an MWE:

\documentclass{article}

\def\code#1{{\texttt{#1}}}
%\def\code#1{{\mbox{\texttt{#1}}}}

\usepackage[strings]{underscore}

\begin{document}

I really like to use \code{assert()}. Still,
\code{assertions_that_should_never_fail_even_if_they_stick_out()} 
should not be hyphenated. I know some long words: 
pneumonoultramicroscopicsilicovolcanoconiosis,
parastratiosphecomyia stratiosphecomyioides,
pseudopseudohypoparathyroidism,
floccinaucinihilipilification,
subdermatoglyphic,
squirrelled,
abstentious and 
rotavator.

Function \code{FOO} runs in time $T_{\code{FOO}}$, not $T_{FOO}$.

\end{document}
cxxl
  • 1,183
  • Try using verbatim text: \verb+Here is my code.+ Any character (except *) can be used to delimit the verbatim text, as long as it doesn't appear in the text itself, obviously. – ChrisS Apr 08 '14 at 10:26
  • I already tried that. I get LaTeX Error: \verb illegal in command argument. – cxxl Apr 08 '14 at 10:34
  • @egreg: of course text should not stick out, I just want no hyphenation in \code{} but warnings in the log. Your solution works fine! Thanks again. – cxxl Apr 08 '14 at 10:50
  • @egreg: I'm grateful for the solution, but now it takes always 5 cycles to compile the document (pdfTeX, Version 3.1415926-2.4-1.40.13 (MiKTeX 2.9 64-bit)). It used to be done in max. 3. Is there anything I can do about it? – cxxl Apr 08 '14 at 11:00

1 Answers1

4

I don't really understand why you'd want long blocks of text sticking out in the margin.

However, here's a way; I avoided underscore for a safer method accomplishing the same result.

\documentclass{article}
\usepackage[T1]{fontenc} % better underscore
\usepackage{amsmath}
\usepackage[htt]{hyphenat}
\newcommand\code[1]{\texttt{#1}}

%% This code substitutes underscore.sty
\begingroup\lccode`~=`_
\lowercase{\endgroup
  \protected\def~{\ifmmode\sb\else\textunderscore\fi}
}
\AtBeginDocument{\catcode`_=\active}
%% end

\begin{document}

I like to use \code{assert()}. Still,
\code{assertions_that_should_never_fail_even_if_they_stick_out()}
should not be hyphenated. I know some long words:
pneumonoultramicroscopicsilicovolcanoconiosis,
parastratiosphecomyia stratiosphecomyioides,
pseudopseudohypoparathyroidism,
floccinaucinihilipilification,
subdermatoglyphic,
squirrelled,
abstentious and
rotavator.

Function \code{FOO} runs in time $T_{\code{FOO}}$, not $T_{FOO}$.

\end{document}

enter image description here

Alternatively, use \textnhtt (again from hyphenat):

\documentclass{article}
\usepackage[T1]{fontenc} % better underscore
\usepackage{amsmath}
\usepackage{hyphenat}
\DeclareRobustCommand\code[1]{%
  \ifmmode
    \expandafter\texttt
  \else
    \expandafter\textnhtt
  \fi{#1}%
}

%% This code substitutes underscore.sty
\begingroup\lccode`~=`_
\lowercase{\endgroup
  \protected\def~{\ifmmode\sb\else\textunderscore\fi}
}
\AtBeginDocument{\catcode`_=\active}
%% end

\begin{document}

I like to use \code{assert()}. Still,
\code{assertions_that_should_never_fail_even_if_they_stick_out()}
should not be hyphenated. I know some long words:
pneumonoultramicroscopicsilicovolcanoconiosis,
parastratiosphecomyia stratiosphecomyioides,
pseudopseudohypoparathyroidism,
floccinaucinihilipilification,
subdermatoglyphic,
squirrelled,
abstentious and
rotavator.

Function \code{FOO} runs in time $T_{\code{FOO}}$, not $T_{FOO}$.

\end{document}

Note that with amsmath the subscript size is chosen correctly.

egreg
  • 1,121,712