3

I have used TikZ to define an extensible arrow interrupted by symbols similar to \xrightarrow.

\documentclass{article}   
\usepackage{tikz}

\newcommand{\oprightarrow}[1]{%
\mathbin{%
\tikz[baseline=-3pt]{%
\node[text depth=0,inner sep=1.5pt] (n) {\ensuremath{\scriptstyle #1}};%
\draw (n.west) -- +(-.7em,0);%
\draw[->] (n.east) -- +(.7em,0);%
}}}


\begin{document}

\begin{figure}
   \caption{$P\oprightarrow{\varphi}Q$}
\end{figure}

$P\oprightarrow{\varphi} Q$

\end{document}

While this works fine in normal situations, I encounter an error while using it in a figure's caption:

./test_mwe.tex:16: Undefined control sequence.
\tikz@deactivatthings ->\def ;
                          {\tikz@nonactivesemicolon }\def :{\tikz@nonact...
l.16    \caption{$P\oprightarrow{\varphi}Q$}

./test_mwe.tex:16: Undefined control sequence.
\tikz@deactivatthings ...onactivesemicolon }\def :
                                              {\tikz@nonactivecolon }\de...
l.16    \caption{$P\oprightarrow{\varphi}Q$}

./test_mwe.tex:16: Undefined control sequence.
\tikz@deactivatthings ...kz@nonactivecolon }\def |
                                              {\tikz@nonactivebar }\def ...
l.16    \caption{$P\oprightarrow{\varphi}Q$}

./test_mwe.tex:16: Undefined control sequence.
\tikz@deactivatthings ...tikz@nonactivebar }\def !
                                              {\tikz@nonactiveexlmark }
l.16    \caption{$P\oprightarrow{\varphi}Q$}

I know that there can be trouble mixing babel and tikz, e.g. with french language making the colon and semi-colon become active characters. But this seems to be something different.

FWIW, the caption is typeset correctly.

Any ideas?

1 Answers1

10

The \tikz[...]{...} command is essentially equivalent to \begin{tikzpicture}[...]...\end{tikzpicture} and \begin is a fragile command. This makes also your \oprightarrow command into a fragile command that needs \protect in front of it when it appears in a moving argument, such as a caption text or a sectional title.

Thus

\caption{$P\protect\oprightarrow{\varphi}Q$}

would solve the issue, but it's better if you solve it completely by doing \DeclareRobustCommand instead of \newcommand:

\DeclareRobustCommand{\oprightarrow}[1]{%
  \mathrel{%
    \tikz[baseline=-3pt]{%
      \node[text depth=0,inner sep=1.5pt] (n) {$\scriptstyle #1$};%
      \draw (n.west) -- +(-.7em,0);%
      \draw[->] (n.east) -- +(.7em,0);%
    }%
  }%
}

Note that an arrow should be a relation symbol (hence \mathrel) and that \ensuremath is just making TeX do more work for nothing.

\documentclass{article}
\usepackage{tikz}

\DeclareRobustCommand{\oprightarrow}[1]{%
  \mathrel{%
    \tikz[baseline=-3pt]{%
      \node[text depth=0,inner sep=1.5pt] (n) {$\scriptstyle #1$};%
      \draw (n.west) -- +(-.7em,0);%
      \draw[->] (n.east) -- +(.7em,0);%
    }%
  }%
}


\begin{document}

\begin{figure}[htp]
   \caption{$P\oprightarrow{\varphi}Q$}
\end{figure}

$P\oprightarrow{\varphi} Q$

\end{document}

enter image description here

You can check that this works also with babel-french.

On the other hand, you can solve the same problem without TikZ:

\documentclass{article}

\DeclareRobustCommand{\oprightarrow}[1]{%
  \mathrel{-}
  \mathrel{\vcenter{\hbox{$\scriptstyle\,\mathstrut#1\,$}}}
  \rightarrow
}


\begin{document}

\begin{figure}[htp]
   \caption{$P\oprightarrow{\varphi}Q$}
\end{figure}

$P\oprightarrow{\varphi} Q$

\end{document}

enter image description here

egreg
  • 1,121,712