6

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 ?

brunoh
  • 723

2 Answers2

6

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}
David Carlisle
  • 757,742
  • @David_Carlisle Clear & precise answer ! Very useful for me because I also learn the mechanism of getting rid of shorthands in babel locally or globally. Thank you very much. – brunoh Apr 30 '15 at 15:57
4

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[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 }
egreg
  • 1,121,712
  • Very useful answer ! After the one of David Carlisle, I was using the option \shorthandoff as a global option parameter of the babel package, but I think yours is more precise and allow fine tuning with less side effects. Thank you very much. – brunoh Apr 30 '15 at 18:19
  • I tested it but unfortunately a construction like A_{\varphi^{-1}(q)} \ar@{}[r]^(.25){}="a"^(.75){}="b" \ar "a";"b"^{\varphi_{q}} & B_{q} inside a xymatrix bracket does not seem to work anymore : the ; character does not seem to be handled well anymore ... Do you know what is happening ? – brunoh Apr 30 '15 at 18:27
  • @brunoh Here's what I get from \xymatrix{ A_{\varphi^{-1}(q)} \ar@{}[r]^(.25){}="a"^(.75){}="b" \ar "a";"b"^{\varphi_{q}} & B_{q} }: http://i.stack.imgur.com/KTWQL.png – egreg Apr 30 '15 at 21:07
  • Yes, you are right. In my main file, I used \preto\xy{\shorthandoff{!?;:}} before the declaration of the xy package. If I put it after, everything works fine ! Thank you again I will choose your answer as the preferred one but I also like the one of David Carlisle ... – brunoh Apr 30 '15 at 21:59
  • @brunoh I get no such warning. – egreg Apr 30 '15 at 22:00
  • I forgot to delete the previous \preto instruction before the package's call. My bad. Sorry for the inconvenience, and I edited my previous comment. – brunoh Apr 30 '15 at 22:02
  • I ran into similar problem with \usepackage[slovak]{babel}. Here I had to use \shorthandoff*{^}. The same is probably true with other languages having ^ among shorthands. Thanks to David Carlisle for his advice in chat. – Martin Nov 20 '15 at 14:38
  • @Martin You're right, I added also = which is a shorthand in babel-turkish. – egreg Jul 14 '22 at 13:10
  • This is an old question, but I just solved a similar problem to that of @egreg with a construction like \xymatrix{s\ar@/_3mm/[r]_{t}="t"&t} it was originally more complicated but the problem is the quotation character, solved it by adding \shorthandoff{"} – MetalMathician Jul 14 '22 at 13:12
  • 1
    @MetalMathician Added also " – egreg Jul 14 '22 at 13:21
  • I get errors such as Package babel Error: I can't switch '^' on or off--not a shorthand.for all characters except for ". Other people might have to find which characters are and aren't shorthands in their particular language. I am using the spanish option for babel. – MetalMathician Jul 15 '22 at 05:12
  • Alright, a partial list of shorthands for different languages can be found in the documentation. See page 14. https://www.ctan.org/pkg/babel – MetalMathician Jul 15 '22 at 05:26