# Babel shorthands do not expand correctly after a temporary switch to other language. Bug?

I cannot better describe the problem than by the MWE below.

Steps for reproduction:

1. Create a bi-lingual article with Babel (here: american and ngerman)
2. Add the Babel shorthands for the German language to English (I do that often for my convience to enable "= for English, too.)
3. Use the shorthand in a sectioning command.
4. Switch language locally.
5. Use the shorthand in a sectioning command again.

Actual result: In step 3 the shorthand is correctly expanded in the heading and the TOC. In step 5 the shorthand is correctly expanded in the heading, but the TOC prints the shorthand literally.

Expected result: The shorthand should always be expanded correctly.

MWE:

\documentclass[american]{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[ngerman,american,main=american]{babel}

\useshorthands{"}
\addto\extrasamerican{\languageshorthands{american}\languageshorthands{ngerman}}

\begin{document}

\tableofcontents

\section{Lore"=ipsum}

The section title \verb#Lore"=ipsum# is correctly typeset in the TOC.
The shorthands work in the heading and in the TOC.

\begin{otherlanguage}{ngerman}
Ein Absatz in deutsch.
\end{otherlanguage}

\section{Lore"=ipsum}

After a temporary switch to a foreign language, the shorthands work only partially.
The section title \verb#Lore"=ipsum# is correctly typeset in the heading but not in the TOC.
Why?

\end{document}


## 2 Answers

No bug; you're stating \useshorthands{"} in the wrong place:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[ngerman,main=american]{babel}

\addto\extrasamerican{%
\languageshorthands{ngerman}%
\useshorthands{"}%
}

\begin{document}

\tableofcontents

\section{Lore"=ipsum}

The section title \verb#Lore"=ipsum# is correctly typeset in the TOC.
The shorthands work in the heading and in the TOC.

\begin{otherlanguage}{ngerman}
Ein Absatz in deutsch.
\end{otherlanguage}

\section{Lore"=ipsum}

After a temporary switch to a foreign language, the shorthands work only partially.
The section title \verb#Lore"=ipsum# is correctly typeset in the heading but not in the TOC.
Why?

\end{document}


What's the problem? Using \begin{otherlanguage}{ngerman} causes \babel@toc{ngerman}{} to be written in the .toc file; then \end{otherlanguage} causes the writing of \babel@toc{american}{}.

Now the sequence \babel@toc{ngerman}{}\babel@toc{american}{} causes \noextrasngerman to be evaluated, which contributes

\bbl@deactivate {"}\umlauthigh \bbl@nonfrenchspacing


and you see the problem. By adding \useshorthands{"} in \extrasamerican, the problem is solved.

• OK. I followed this answer by Ulrike Fischer where the \useshorthands{"} is outside \addto\extrasamerican. Also Ulrike Fischer adds \languageshorthands{american} explicitly which is omitted in your answer. Is \languageshorthands{american} required or not? – nagmat84 Jul 9 '19 at 10:37
• @nagmat84 \languageshorthands{american} does nothing, because no shorthand is defined to begin with. And unfortunately, the answer you cite is incorrect. – egreg Jul 9 '19 at 10:43

Use the starred version of \useshorthands, ie:

\useshorthands*{"}


From the manual: “User shorthands are not always alive, as they may be deactivated by languages (for example, if you use " for your user shorthands and switch from german to french, they stop working). Therefore, a starred version \useshorthands*{char} is provided, which makes sure shorthands are always activated.”