xymatrix is broken when inside section

If I use xymatrix inside of a section command, it doesn't compile.

Example:

\documentclass{article}
\usepackage[all,cmtip]{xy}
\begin{document}

\section{$\xymatrix{A\ar[r]&B}$}

\end{document}


Compilation error: Argument of doSpecialRotate@@ has an extra {.

I found a proposed solution at the end of this discussion. Problem is, I have found it to collapse when I use either the bookmark package or the hyperref package. Now, the bookmark package I can dispose of, but the hyperref package is vital for me. Here's an example:

\documentclass[a4paper,11pt]{report}
\usepackage[utf8]{inputenc}
\usepackage[all,cmtip,2cell]{xy}
\usepackage[pdftex]{hyperref}
\begin{document}

\DeclareRobustCommand{\ta}{\xymatrix{A\ar[r]&B}}
\section{A stepping stone: $\ta$}

\end{document}


Compilation error: Argument of \xP@rotate@ has an extra }. If you comment out the line concerning hyperref, it works correctly.

How can I use both an xymatrix command inside a \section and use the hyperref package?

-
You try to use mathematical markup in a section command, which will try to set up a bookmark with math content. As far as I know, this does not work. If it is possible, I would like to know how it works ;-) You could use \section{\unexpanded{A stepping stone: $\ta$}} to bypass the error message, however – Christian Hupfer May 13 '14 at 10:28
Just out of curiosity, why not just write $A\to B$ in the title instead, why the need for \xymatrix? – daleif May 13 '14 at 10:31
@daleif: a very reasonable question. The thing is I don't want to use just a plain arrow, I want a more fancy arrow that I only know how to render with xymatrix. – lentic catachresis May 13 '14 at 10:35
@ChristianHupfer: it works on the MWE I posted above (the second one). On my actual document, it works for the first compilation but fails on the second one, something toc-related. – lentic catachresis May 13 '14 at 10:41
I'd still just use \longrightarrow as @egreg shows below, the A to B does not stand on the same line as the rest of the text, which looks bad in my book. – daleif May 13 '14 at 10:41

You can't have typesetting instructions in the bookmarks; moreover you need to protect the fragile commands in \xymatrix.

\documentclass[a4paper,11pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}

\usepackage[all,cmtip,2cell]{xy}
\usepackage[unicode]{hyperref}
\usepackage{bookmark}

\newcommand{\protectedtexorpdfstring}[2]{%
\texorpdfstring{\unexpanded{\unexpanded{#1}}}{#2}%
}

\begin{document}

\section{A stepping stone: \protectedtexorpdfstring{\xymatrix{A\ar[r]&B}}{A\textrightarrow B}}

\end{document}


In the second argument of \protectedtexorpdfstring (choose a shorter name, if you wish) you put an approximate representation of the diagram or anything you prefer.

If you plan to also have a table of contents, there's something more to be done; the problem is that when the .toc file is read in, the category code of @ is 11, which confuses xypic.

The \txymatrix command has two arguments: the contents of the \xymatrix and the alternative text for the bookmark.

\documentclass[a4paper,11pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}

\usepackage[all,cmtip,2cell]{xy}
\usepackage[unicode]{hyperref}
\usepackage{bookmark}

\newcommand{\protectedtexorpdfstring}[2]{%
\texorpdfstring{\unexpanded{\unexpanded{#1}}}{#2}%
}

\newcommand{\txymatrix}[2]{%
\protectedtexorpdfstring
{$\catcode\@=12 \scantokens{\xymatrix{#1}}$}
{#2}%
}

\begin{document}

\tableofcontents

\section{A stepping stone: \txymatrix{A\ar[r]&B}{A\textrightarrow B}}

\section{A stepping stone: \txymatrix{K\Omega \ar@{~}[r]^-\simeq & KqB^b}{KOmega}}

\end{document}


-
Thanks! However, when I use the decorated arrow I wanted to use, it shows up very badly in the TOC. \section{A stepping stone: \protectedtexorpdfstring{\xymatrix{K\Omega \ar@{~}[r]^-\simeq & KqB^b}}{KOmega}}. That is, passing the \tableofcontents command. How can I fix this? – lentic catachresis May 13 '14 at 10:52
@egreg: Good answer! Is the slight downshift of the typeset \xymatrix command in the section head or in TOC reversable? – Christian Hupfer May 13 '14 at 11:09
@BrunoStonek Fixed. – egreg May 13 '14 at 11:24
@ChristianHupfer Enclosing \xymatrix between \$` symbols does it. – egreg May 13 '14 at 11:25
Wonderful! Thank you very much! – lentic catachresis May 13 '14 at 12:07