20

The package babel with the ngerman option (and most other languages as well, but not english) provides the shorthand "|, which prevents a ligature at the given position (see p. 81 of the babel documentation, e.g. Auf"|lage will result in the word Auflage without an fl-ligature (as is desired because, in German, you don't want ligatures across morpheme borders).

However, when placed in a macro, "| doesn't work anymore. Why is this and how can I circumvent it?

\documentclass{article}

%\usepackage[T1]{fontenc}% doesn't help
\usepackage[ngerman]{babel}

\newcommand{\foo}{"|}

\begin{document}

ff

f"|f

f\foo f

\end{document}

output of above code

2
  • Let me note for searchability that this is also the case with environment definitions.
    – Raphael
    Nov 13, 2013 at 9:56
  • Note that context-sensitive ligature suppression is implemented by the selnolig package.
    – koppor
    Mar 13, 2018 at 6:25

1 Answer 1

22

It works if you switch the shorthand on before defining the command, or if you define the command after \begin{document} when shorthands are active.

As Joseph commented, it's recommendable to switch it off immediately afterwards, so that the active shorthand doesn't affect other commands.

\documentclass{article}
\usepackage[ngerman]{babel}
\shorthandon{"}
\newcommand{\foo}{"|}
\shorthandoff{"}
\begin{document}
ff

f"|f

f\foo f
\end{document}

example output

Alternatively, you could switch it on locally within the macro definition, similar as written here by egreg, and made robust, so it can be used within moving arguments, such as \chapter and \section headings and captions.

\DeclareRobustCommand{\foo}{\shorthandon{"}\scantokens{"|\endinput}}

However, it might be useful to have a part in the preamble where shorthands are active, as in the first solution, so you can use them in your commands such as in text arguments to macros.

3
  • 4
    I'd also switch the shorthand off after the definition, as other parts of the preamble might otherwise be affected.
    – Joseph Wright
    Nov 13, 2011 at 10:05
  • You should also switch it off using \shorthandoff{"} after the definition. Otherwise you may run into catcode problems at the preamble (e.g. loading other packages after the definition). That's the reason for babel does not active the shorthands while loading the language file but just after \begin{document}. Nov 13, 2011 at 10:06
  • Are those shorthands based on "real" latex commands? If so, what are they? I just need to use "- once in the preamble, so just using \whatever would be shorter than turning shorthands on and off…
    – Marian
    Jan 23, 2019 at 14:23

You must log in to answer this question.

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