4

In the following document

\documentclass{article}
%\usepackage[german]{babel} % also try english, ngerman and dutch
\usepackage{listings}
\begin{document}
\thispagestyle{empty}
\begin{minipage}{1em}

und/oder % and/or on german; shouldn't be hyphenated

\medskip
n und/oder % same es above, but not the first word in paragraph

\medskip
und/""oder % "" is a shorthand in some languages, provided by babel. It allows a linebreak without hyphen
\end{minipage}
\end{document}

I get problems with the hyphenation. As soon as both packages are loaded—the order doesn't matter—the / seems to be treated as ordinary letter, at least if certain languages are activated (e.g. german, ngerman or dutch). Only loading these languages, but not activate them, has no effect. Normally, the whole term und/oder shouldn't be hyphenated. Interestingly, even pdflatex and lualatex produce different results: lualatex hyphenates the first word of a paragraph (compare the first and second row in the images).

Does anyone know what's the reason for this? I haven't noticed this behaviour before and I'm using both packages in nearly every document. Results of several combinations below.

0

1 Answer 1

4

Here's the problem in the .dtx file:

   5746 % \begin{macro}{\lst@RestoreCatcodes}
   5747 % To load the kernel, we will change some catcodes and lccodes. We restore them
   5748 % at the end of package loading. \lsthelper{Dr.~Jobst~Hoffmann}{2000/11/17}
   5749 % {incompatibility with typehtml package} reported an incompatibility with the
   5750 % \packagename{typehtml} package, which is resolved by |\lccode`\/`\/| below.
   5751 %    \begin{macrocode}
   5752 \def\lst@RestoreCatcodes#1{%
   5753     \ifx\relax#1\else
   5754         \noexpand\catcode`\noexpand#1\the\catcode`#1\relax
   5755         \expandafter\lst@RestoreCatcodes
   5756     \fi}
   5757 \edef\lst@RestoreCatcodes{%
   5758     \noexpand\lccode`\noexpand\/`\noexpand\/%
   5759     \lst@RestoreCatcodes\"\^^I\^^M\~\^^@\relax
   5760     \catcode12\active}
   5761 %    \end{macrocode}

Setting

\lccode`\/`\/

may perhaps solve the problem with typehtml, but creates the problem with hyphenation, which is much worse.

As far as hyphenation is concerned, TeX considers a word as a sequence of characters of category code 11 or 12 having nonzero \lccode. So a zero \lccode stops hyphenation. On the other hand, if / is assigned code 47, it is considered and hyphenation patterns can be found. Since

und/oder

is an eight character “word” and hyphenation between un and d is allowed, as well as between o and der, those hyphenation points are used.

You solve the problem by saying

\documentclass{article}
\usepackage[ngerman]{babel} % also try english, ngerman and dutch
\usepackage{listings}
\usepackage{etoolbox}
\makeatletter
\lccode`\/=0
\patchcmd{\lst@RestoreCatcodes}{\lccode `\/`\/}{}{}{}
\makeatother

\begin{document}
\thispagestyle{empty}
\begin{minipage}{1em}
und/oder % and/or on german; shouldn't be hyphenated

\medskip
n und/oder % same es above, but not the first word in paragraph

\medskip
und/""oder % "" is a shorthand in some languages, provided by babel. It allows a linebreak without hyphen
\end{minipage}

\makeatletter

\end{document}

This zaps the offending setting from the relevant commands.

3
  • Thanks, your patch works! Could you shortly explain, why the behaviour only occurs if babel is loaded? If listings changes the catcode and / is now treated as letter, why are there no effects without babel? Mar 6, 2014 at 19:53
  • @ThorstenKück Without babel, the hyphenation patterns for English are used, which happen not to allow any hyphenation.
    – egreg
    Mar 6, 2014 at 20:44
  • Oh, of course, that makes sense. Mar 6, 2014 at 21:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .