7

The following code

\documentclass{article}

\usepackage{kantlipsum}
\usepackage[ngerman,english]{babel}

\begin{document}

\section{Foo}
\kant[1]
\begin{itemize}
\item
\item
\item
\item
\item
\end{itemize}
\kant[2]
\begin{itemize}
\item
\item
\item
\item
\end{itemize}

% \selectlanguage{ngerman}
\section{Baz}
\kant[1]

\end{document}

produces the output

enter image description here

When uncommenting the \selectlanguage instruction (and also using \begin{otherlanguage}) the second section is pushed to the second page. I noticed this in a document with itemizes in it, like I've shown here, and I did not manage to reproduce the problem by using text only.

My (wild) guess is that the whatsit node introduced by the \write directive issued by \selectlanguage interferes with the vertical glue introduced by itemize and \section. In fact, writing

\makeatletter\write\@auxout{\relax}\makeatother

instead of \selectlanguage leads to the same problem.

Is my guess right? And more importantly, can I do something against it? In the real use case the section heading involves a \...name which changes with the language. As I workaround I could use \foreignlanguage in the section title but I'd like to know if there are better ways.

8

Your guess is right. At the end of \end{itemize}, an instruction

\addvspace{<skip 1>}

is issued and \section issues

\addvspace{<skip 2>}

so, in normal situations, only the greater skip results in the output. However, as you observe, the \write instruction that's part of \selectlanguage makes it impossible for the second \addvspace to know the amount of <skip 1>, because the last item in the vertical list is no longer a vertical skip.

You can manually fix this with

\setlength{\skip0}{\lastskip}
\addvspace{-\skip0}
\selectlanguage{ngerman}
\addvspace{\skip0}

which works for the particular situation.

Output without the four above lines

Note the correct English hyphenation of “representation”.

enter image description here

Output with the four lines above

Note the incorrect English hyphenation of “representation” (due to using German hyphenation rules).

enter image description here

  • Thanks. Luckily the "particular situation" is very close to what I'll always have (it's for a multi-lingual exercise sheet, and this combination itemize/section is built in) so I can adapt your solution to my case. – campa Jan 14 at 9:26
6

hyperref tries quite hard to avoid such side effects. You could steal its commands:

\documentclass{article}

\usepackage{kantlipsum}
\usepackage[ngerman,english]{babel}
\usepackage{hyperref}
\begin{document}
\section{Foo}
\kant[1]
\begin{itemize}
\item
\item
\item
\item
\item
\end{itemize}
\kant[2]
\begin{itemize}
\item
\item
\item
\item
\end{itemize}


\makeatletter
\Hy@SaveLastskip
\selectlanguage{english}
\Hy@RestoreLastskip
\makeatother


\section{Baz}

\kant[1]

\end{document}

(It is not perfect, in some cases it then suppress a break point).

  • Wow, that's also a great answer. I'll try it out. – campa Jan 14 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.