2

Why do secondary babel languages make their shorthand characters active? Is there a way to make them only active for their language, instead of making them active characters that output themselves, to avoid conflicts?

In the following MWE, adding german as a secondary language (without activating it) breaks the document, because shortcut character " is made active and because my definition of \code is a bad idea (but hard to fix*). I can use shorthands=off, but then shortcuts aren't available in German.

\documentclass[varwidth,border=1pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage[ngerman,main=english]{babel}
\usepackage{listings}
\newcommand{\code}[1]{%
    \lstinline[basicstyle=\ttfamily]^#1^}

\begin{document}
The " character is active: \verb!\the\catcode`"! = \the\catcode`" .
But shortcuts are not actually active: "s.
\\

\shorthandoff{"}
I must disable shortcuts to ensure that " is not active: \verb!\the\catcode`"! =
\the\catcode`" .
So I can use the \verb!\code! command: \code{"bar" + str1}.
\\

\shorthandon{"}
\begin{otherlanguage}{ngerman}
Text with German shortcuts: "s.
\end{otherlanguage}
\shorthandoff{"}
\end{document}

*IIUC fixing the abuse would force me to change the syntax of \code, and that would require changing tens of pages using \code.

1 Answer 1

3

Your definition of \code is wrong: just avoid grabbing the argument. This way you will have available the full power of \lstinline, so typing either \code{<tokens>} or \code|<tokens>| (where | is any character not appearing in <tokens>).

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage[ngerman,main=english]{babel}
\usepackage{listings}
\newcommand{\code}{\lstinline[basicstyle=\ttfamily]}

\begin{document}
The " character is active: \verb!\the\catcode`"! = \the\catcode`" .
But shortcuts are not actually active: "s.

So I can use the \verb!\code! command: \code{"bar" + str1}.

\begin{otherlanguage}{ngerman}
Text with German shortcuts: "s.
\end{otherlanguage}

\end{document}

enter image description here

1
  • Thanks! \lstinline{...} is documented as experimental, but this seems to work. I guess there's no straightforward way to automatically make shorthand characters inactive as I asked (short of trying to wrap the otherlanguage environment)? Aug 13, 2017 at 18:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .