Q:

xypic error at macro @!

Here is an example of the problem I encounter with xypic
\documentclass[10pt,a4paper,french,oneside]{amsbook}
\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage[applemac]{inputenc}
\usepackage[all,cmtip]{xy}
\begin{document}
\[
\xymatrix@!{
P\ar[r]^{p_{2}}\ar[d]_{p_{1}}&Y\ar[d]^g\\
X\ar[r]^f&Z}
\]
\end{document}

And below is the error I got when I typeset
./essai.tex:16: Xy-pic error: <addop> or <direction> or one of    1RCMLHW*! expect
ed.
\xyerror@ ...#2}\fi \errmessage {Xy-pic error: #1}
                                              }
l.16     \xymatrix@!
                {

I strongly suspect it comes from babel and the french option messing up stuff, since the character ! does not seem to be recognized (@R works fine for instance). What can I do about that ?

A:

You can locally get ! back with \shorthandoff{!} :
\documentclass[10pt,a4paper,french,oneside]{amsbook}
\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage[applemac]{inputenc}
\usepackage[all,cmtip]{xy}
\begin{document}
\[\shorthandoff{!}
\xymatrix@!{
P\ar[r]^{p_{2}}\ar[d]_{p_{1}}&Y\ar[d]^g\\
X\ar[r]^f&Z}
\]
\end{document}

A:

Solving the problem for ! is not sufficient, as also ?;: are used in Xy-pic syntax.
You can globally solve it by disabling shorthands in all Xy-pic constructions:
\documentclass[10pt,a4paper,french,oneside]{amsbook}
\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage[applemac]{inputenc}
\usepackage[all,cmtip]{xy}
\usepackage{etoolbox}

\preto\xy{\shorthandoff{!?;:}}

\begin{document}
\[
\xymatrix@!{
P\ar[r]^{p_{2}}\ar[d]_{p_{1}}&Y\ar[d]^g\\
X\ar[r]^f&Z}
\]
\end{document}

Without etoolbox it would be, instead of \preto\xy{...},
\toks0=\expandafter{\xy}
\edef\xy{\noexpand\shorthandoff{!?;:}\the\toks0 }

