# Undefined control sequence in Latex using programming code [duplicate]

Problem:

Getting the error message Undefined control sequence. (\end{document}..). This occurs when I include the following code that prints out formatted programming code. If I remove this code the issue goes away.

Minimal Working Example (MWE):

\documentclass{studentlitteratur}
\usepackage{lipsum}
\usepackage{listings}
\mdfdefinestyle{mdlistingstyle}{}
\mdtheorem[style=mdlistingstyle]{sourcecode}[chapter]{Kodexempel}
\begin{document}
\part{Title}
\begin{sourcecode}
\end{sourcecode}
\lipsum[1-3]
\begin{sourcecode}
\end{sourcecode}
\end{document}


.cls content available here: http://pastebin.com/w1HVnM9L

.sty content available here: http://pastebin.com/YGTedA7q

Error message:

./studentlitteratur.tex:13: Undefined control sequence.
<argument> \protect \bbl@restore@actives delz
i\protect \hspace  {0.5em} title
l.13 \end{document}


Desired outcome:

To remove the error message and achieve correct code.

## marked as duplicate by Phelype Oleinik, Stefan Pinnow, dexteritas, Bobyandbob, SebastianoJul 26 '18 at 19:31

• It is easier to help you if you add a minimal working example that takes the form \documentclass{...}\usepackage{....}\begin{document}...\end{document}. If possible, it should compile and have the minimum amount of code needed to illustrate your problem. This makes it much easier for people to troubleshoot your problem - and much more likely that they will! – Andrew Jul 22 '15 at 12:30
• First a small apology, I didn't notice \mdtheorem[style=mdlistingstyle,theoremseparator={}]{sourcecode}..., in the code you have posted. That does require the mdframed package though, which is not present anywhere I think. Perhaps in the class file. – Torbjørn T. Jul 22 '15 at 14:15
• Reduce the example. I can provoke the same error with \documentclass{studentlitteratur} \usepackage{lipsum} \newcommand{\figureversion}[1]{} \usepackage{listings} \mdfdefinestyle{mdlistingstyle}{} \mdtheorem[style=mdlistingstyle]{sourcecode}[chapter]{Kodexempel} \begin{document} \part{Title} \begin{sourcecode} \end{sourcecode} \lipsum[1-3] \begin{sourcecode} \end{sourcecode} \end{document}. I had to define \figureversion for some reason, and uncomment some font packages from the class file because I don't have them (MinionPro, MnSymbol, MyriadPro). (Doesn't help for the class file.) – Torbjørn T. Jul 22 '15 at 15:40
• @TorbjørnT. great work. Even minimalER: \documentclass{studentlitteratur}\usepackage{lipsum}\usepackage{listings}\begin{document}\part{Part}\lipsum\end{document} with the same caveat for fonts. So ultimately the problem has nothing to do with sourcecode. The definition of \part messes up something. – Paolo Brasolin Jul 22 '15 at 15:56
• listings seem to have some influence as well, after messing around with the class file, I can get to bitbucket.org/snippets/torbjornt/qRExe – Torbjørn T. Jul 22 '15 at 16:28

An MWE (thanks @TorbjørnT.) reproducing your error is:

\documentclass{memoir}
\usepackage{lipsum}
\usepackage[swedish]{babel}
\usepackage{listings}
\makeatletter
\def\partmark##1{\markboth{%
\partname~\thepart\hspace{0.5em} ##1}{}}%
}
\makeatother
\begin{document}
\part{Title}
\lipsum
\end{document}


You too could have extorted this from your custom document class by mutilation. In fact, the indented lines are taken nearly verbatim from it.

There are various ways to make this example compile:

• omitting babel
• omitting listings
• omitting the ~
• omitting \MakeLowercase

The exact same error can be produced in a different and simpler context with a shorter example that has exactly the same behaviour:

\documentclass{standalone}
\usepackage[english]{babel} % the language is irrelevant
\usepackage{listings}
\begin{document}
\MakeLowercase{A~Z}
\end{document}


A simple solution is replacing ~ with its synonymous \nobreakspace, as in:

\makepsmarks{headings}{%
\def\partmark##1{\markboth{%
\partname\nobreakspace \thepart\hspace{0.5em} ##1}{}}%
}


This will probably fix your document class.

A more elegant solution would require knowing exactly how listings and babel are interacting, i.e. studying their source code. I will not do that. I've skimmed the code of listings and there seem to be some comments related to the behaviour of the ~ character, in case you want to dive into it.