# \selectlanguage pushes section to new page

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

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.

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}
\selectlanguage{ngerman}


which works for the particular situation.

## Output without the four above lines

Note the correct English hyphenation of “representation”.

## Output with the four lines above

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

• 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. Jan 14, 2021 at 9:26

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. Jan 14, 2021 at 9:37