\newcommand{\code}[1]{\texttt{#1}}
When I put a hyphenated word inside \code, like so:
\code{long-and-hyphenated}
LaTeX acts like it's contained in an \mbox. It won't break the line
after any of the hyphens, even if it means setting the text two full
inches outside the right margin! How can I convince LaTeX it's OK to
break the line after a hyphen inside \code? Thanks in advance.
: \code{long-and-hyphenated}
This problem was discussed in the German TeX newsgroup a few days
ago. The problem is that LaTeX loads the tt-font with disabled
hyphenation. The solution might be to change this behaviour in the
fd file, but I would prefer the following solution posted by
B. Raichle because you don't have to hack in the fd
file (where you would have to think about copyright, too). Just
write in the preamble:
\newcommand{\origttfamily}{}\let\origttfamily=\ttfamily
\DeclareRobustCommand{\ttfamily}{\origttfamily\hyphenchar\font=`\-}
Bye
Klaus
--
Klaus Hoeppner uph...@zx2.hrz.uni-dortmund.de
Universitaet Dortmund
Exp. Physik 1
D-44221 Dortmund
> : It won't break the line
> : after any of the hyphens, even if it means setting the text two full
> : inches outside the right margin!
>
> Just write in the preamble:
>
> \newcommand{\origttfamily}{}\let\origttfamily=\ttfamily
> \DeclareRobustCommand{\ttfamily}{\origttfamily\hyphenchar\font=`\-}
When I put these lines in the preamble, LaTeX complains:
! TeX capacity exceeded, sorry [input stack size=300].
\ttfamily ->\origttfamily
\hyphenchar \font =`\-
the first time I use the tt font.
So sorry!
I just tried it AFTER posting this article and got the same error.
The definition seems to lead to a infinite recursion...
The following solution is a little bit more complicated but worked well
for me (I just copied the definition of \ttfamily from latex.ltx):
\makeatletter %only in the preamble
\DeclareRobustCommand\origttfamily
{\not@math@alphabet\ttfamily\mathtt
\fontfamily\ttdefault\selectfont\hyphenchar\font=-1}
\DeclareTextFontCommand{\texttorig}{\origttfamily}
\DeclareRobustCommand\ttfamily
{\not@math@alphabet\ttfamily\mathtt
\fontfamily\ttdefault\selectfont\hyphenchar\font=`\-}
\makeatother %only in the preamble
Now words in \texttt{} should be hyphenated and words in \textttorig{}
not.
You don't need the makeatletter and makeatother, resp., if you place the
definition in a style file.
Hope it helps
bye
This is because (the original) \ttfamily is also a robust command, so
that both \origttfamily and \ttfamily are really defined as
{\protect\ttfamily }
(I put that in braces to emphasise what's going on... there's an
honest-to-goodness space at the end of that control sequence.)
The following _will_ work:
\expandafter\let\expandafter\origttfamily\csname ttfamily \endcsname
\DeclareRobustCommand{\ttfamily}{\origttfamily\hyphenchar\font=`\-}
I hate that code, but don't have anything better...
--
Robin (Campaign for Real Radio 3) Fairbairns r...@cl.cam.ac.uk
U of Cambridge Computer Lab, Pembroke St, Cambridge CB2 3QG, UK
Home page: http://www.cl.cam.ac.uk/users/rf/robin.html