12

TL;DR see below at "core problem"


Problem

I'm trying to solve a conflict between the packages tipa (Fonts and macros for IPA phonetics characters) and babel with the ngerman option. The problem is the " character, which both packages make use of. I've gotten pretty far, here's what I've got:

Using just tipa

\documentclass{article}
\usepackage{tipa}
\usepackage[T1]{fontenc}
\begin{document}
  \textipa{" ""}
\end{document}

produces the desired

ˈ ˌ

(primary and secondary stress)

Using just babel

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\begin{document}
  "a "s "‘ "’ "< ">
\end{document}

produces the desired

ä ß „ “ « »

(result of babel shorthands specific to ngerman, i.e. english wouldn't produce this output)

Using babel and tipa

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{tipa}
\usepackage[T1]{fontenc}
\begin{document}
  \textipa{"x""x"a"s}
\end{document}

produces the not desired

ˈxxäß

Problem: the secondary stress is broken and a primary stress (") a) before a vowel results in a diaeresis or trema (= two dots, "umlaut") on the vowel and b) before an s results in a sharp s (ß).

My approach

Since in \textipa, " should be picked up by tipa, I used babel's \shorthandoff{"}:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{tipa}
\usepackage[T1]{fontenc}
\begin{document}
  \shorthandoff{"}
  \textipa{"a""s}
\end{document}

produces the desired

ˈxˌxˈaˈs

Problem solved? Not quite. To limit the scope of \shorthandoff{"} and to save typing, I put it in a macro \myipa, which doesn't work:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{tipa}
\usepackage[T1]{fontenc}
  \newcommand{\myipa}[1]{\shorthandoff{"}\textipa{#1}}
\begin{document}
  \myipa{"x""x"a"s}
\end{document}

produces the not desired

ˈxxäß

Putting the \shorthandoff{"} in \textipa's argument doesn't make a difference (\newcommand{\myipa}[1]{\textipa{\shorthandoff{"}#1}}).

Generally, putting \shorthandoff{"} in a macro works:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
  \newcommand{\shoff}{{\shorthandoff{"} "a}}
\begin{document}
  "a \shoff{} "a
\end{document}

produces the desired

ä "a ä


TL;DR – the core problem

babel's \shorthandoff{"} doesn't seem to have an effect on input characters called outside of its direct range:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
  \newcommand{\shoff}[1]{{\shorthandoff{"} 2"a #1}}
\begin{document}
  1"a \shoff{3"a} 4"a
\end{document}

produces

1ä 2"a 3ä 4ä

i.e. "a called in the same macro as \shorthandoff{"} produces "a (2), but "a called as an argument to a macro containing \shorthandoff{"} procudes ä (3).

How can I fix this macro to succeed in this last step of getting tipa and babel/ngerman to be friends?

lockstep
  • 250,273
doncherry
  • 54,637
  • Just out of curiosity: what's "TL;DR"? – egreg Sep 30 '11 at 08:51
  • It stands for "too long, didn't read", the comma probably has turned into a semicolon because it's all-caps? I think it started out as a comment to posts perceived as too long, and has since become kind of a synonym for "summary" or "short version", for those people who don't want to read the whole thing and would otherwise add "TL;DR" as a comment. – doncherry Sep 30 '11 at 12:43

2 Answers2

8

You have to defer gathering the argument:

\newcommand{\myipa}{\begingroup\shorthandoff{"}\myipaI}
\newcommand{\myipaI}[1]{\tipaencoding #1\endgroup}

This will typeset correctly the bit \myipa{"x""x"a"s}. Of course such a command can't go in the argument of other commands. Should you need it into a section title, say

\shorthandoff{"}
\section{\textipa{"x""x"a"s}}
\shorthandon{"}

Here \myipa would not work if you need to typeset the table of contents. It's always the same problem: when some characters are gathered as arguments, their category code is fixed.

This however suggests a different approach:

\newcommand{\myipa}[1]{{\shorthandoff{"}\scantokens{\tipaencoding#1\endinput}}}

Or \DeclareRobustCommand, if you need it frequently in moving arguments; it's always possible to precede \myipa with \protect, for one or two cases.

egreg
  • 1,121,712
  • \DeclareRobustCommand{\myipa}[1]{{\shorthandoff{"}\scantokens{\tipaencoding#1\endinput}}} works in all cases, thanks a lot! – doncherry Oct 01 '11 at 11:52
0

I had the same problem with babel's spanish option. A simple workaround is to instruct babel to load two languages and set the language to english whenever IPA is called for:

\documentclass{minimal}

\usepackage[ngerman,english]{babel} % or [spanish,english], or whatever
\usepackage{tipa}

\newcommand{\myipa}[1]{\begin{otherlanguage}{english}\textipa{#1}\end{otherlanguage}}

\begin{document}
\selectlanguage{ngerman}
% ... stuff in German ...
\myipa{"x""x"a"s} % should be 'x,x'a's
% ... more stuff in German ...
\end{document}

This approach should also take care of other potential clashes not to do with the " character.

Kahovius
  • 141