4

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?

2

1 Answer 1

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

4
  • Thank you very much, not only for solving the main problem, but also for the remarks on the macro itself, especially the \mathrel (I actually hesitated) and the useless use of \ensuremath. Commented Aug 6, 2015 at 11:12
  • Just read about your \ensuremath-only-when-really-needed campaign :) Commented Aug 6, 2015 at 11:15
  • 1
    @PhilippImhof I added a different solution that has the advantage of using the standard arrow.
    – egreg
    Commented Aug 6, 2015 at 12:21
  • Amazing. Thank you for that solution. It is indeed much more elegant than mine. Commented Aug 6, 2015 at 13:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .