Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

LaTeX: hyphenation inside \texttt?

980 views
Skip to first unread message

Bob Glickstein

unread,
Mar 14, 1996, 3:00:00 AM3/14/96
to
This is a little bit strange: I'm writing a document which has this
definition in the preamble:

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

Klaus Hoeppner

unread,
Mar 15, 1996, 3:00:00 AM3/15/96
to
Bob Glickstein (bo...@zoger.ipost.com) wrote:
: This is a little bit strange: I'm writing a document which has this
: definition in the preamble:

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

Bob Glickstein

unread,
Mar 18, 1996, 3:00:00 AM3/18/96
to
Thanks so much for posting a solution! Unfortunately, it doesn't
quite work.

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

Klaus Hoeppner

unread,
Mar 19, 1996, 3:00:00 AM3/19/96
to
Bob Glickstein (bo...@zoger.ipost.com) wrote:
: Thanks so much for posting a solution! Unfortunately, it doesn't
: quite work.

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

Robin Fairbairns

unread,
Mar 22, 1996, 3:00:00 AM3/22/96
to
In article <m191gyz...@zoger.ipost.com>,

Bob Glickstein <bo...@ipost.com> wrote:
>Thanks so much for posting a solution! Unfortunately, it doesn't
>quite work.
>
>> : 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.

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

0 new messages