# Babel and listings cause problem with hard spaces in sectioning headers in classicthesis

## Problematic MWE:

\documentclass[headinclude,footinclude]{scrbook}%\documentclass[headinclude,footinclude,a4paper]{scrbook}
\usepackage{classicthesis}%\usepackage[style=arsclassica]{classicthesis}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}%\usepackage[ansinew]{inputenc}
%\usepackage[T1]{fontenc}

\usepackage{listings}%\usepackage{listingsutf8}
\lstset{breaklines=true}

\begin{document}

%\tableofcontents

\chapter{Exemplary~chapter~title here}
\section{Exemplary~section~title here}

%\lstinputlisting{a.py}
\begin{lstlisting}
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
a = 1;
\end{lstlisting}

\end{document}


Problems (highlighted in red in the images below):

1. The hard spaces (tildes) used in chapter and section commands are shown wrongly in page headers – sometimes as parts of the code from the problem-causing listing ended by a ‘)’ bracket (in this case as: ‘␣␣␣␣␣␣a)’) and sometimes just as the ‘)’ bracket.
2. One of the line breaks in lstlisting or lstinputlisting listing is lost. Shortly after, a critical listing length is reached that causes the header problems to occur.

Observations:

1. If the listing is shortened by removing a sufficient number of lines, the header problem on the last page covering the listing eventually disappears (previous pages seems to be still affected though).
2. Removing \usepackage[english]{babel} seems to prevent both problems from occuring.
3. Removing \usepackage{classicthesis} removes the problems as well.
4. This seems to be unrelated to the \lccode\~=0\relax bug in listings package (e. g., Textcase, Listings, and Tilde).

## Approximation of the desired result:

• Does the order in which you call the packages change anything? Call listings and babel before ct. What happens when you remove the tildes? – PhilipPirrip Oct 10 '18 at 11:55
• @PhilipPirrip Problem persists for all 6 possible orders of these 3 packages. Without any tilde in both chapter and section titles, the output is correct (also for Problem 2, i.e., missing line break in listing), but just a single tilde makes both problems occur again. Also, putting tilde only in the section title moves the listing breaking problem to the beginning to the next page (relatively to the pictures above). – Peter Oct 10 '18 at 13:30

I'm not able to tell the exact cause of the problem but it's apparently a weird interaction between the listings, babel and textcase packages (where the latter is loaded by classicthesis). There seem to be four conditions required to trigger the problem:

1. The ~ command must be used in the text that goes into the heading. If you use normal spaces here, the output is fine.
2. A lstlisting must occur on the page and span over to the next page. The headings look normal if you just use plain dummy text.
3. The babel package must be loaded, such that the normal definition of the ~ command (\nobreakspace {}) is replaced by \active@prefix ~\active@char~ which then somehow triggers the problem.
4. And finally, a \lowercase must be involved. classicthesis uses textcase's \MakeTextLowercase command in the headings. This uses TeX's \lowercase primitive on ~ and thus leading to the strange behavior. You can see the same behavior if you redefine \MakeTextLowercase with \def\MakeTextLowercase#1{\lowercase{#1}}, but not with a simple \def\MakeTextLowercase#1{#1}.

Putting all that together, the listings bug you mentioned in your question actually does seem to be related here. At least that's what I think, because it somehow manipulates the lowercase code of ~. So my suggested solution is to drop babel's ~ implementation and use the original one:

\let\origtilde=~
\usepackage[english]{babel}
\let~=\origtilde
`