[Tex/LaTex] How to create a squiggle arrow with some text on it in TikZ

arrowstikz-pgf

I need to define a new command to create a squiggle arrow with some text on it. Something similar to what \xrightarrow command produces but with wiggly arrows as in \rightsquigarrow. The length of the arrow should automatically be adjusted to fit the text above it and I do not know how to handle this part in TikZ. Any ideas would be much appreciated.

Best Answer

As percusse mentions in his comment, you can use a node and a decorated arrow; something like this:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,shapes}

\newcounter{sarrow}
\newcommand\xrsquigarrow[1]{%
\stepcounter{sarrow}%
\begin{tikzpicture}[decoration=snake]
\node (\thesarrow) {\strut#1};
\draw[->,decorate] (\thesarrow.south west) -- (\thesarrow.south east);
\end{tikzpicture}%
}

\begin{document}

\xrsquigarrow{text}\quad\xrsquigarrow{longer text}

\end{document}

enter image description here

The following code contains a new version of \xrsquigarrow using the zigzag decoration and \mathrel to be used in math-mode; it shows a comparison between \xrightarrow, \rightsquigarrow and \xrsquigarrow (some fine-tuning is still required, but I leave that to you):

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathmorphing,shapes}

\newcounter{sarrow}
\newcommand\xrsquigarrow[1]{%
\stepcounter{sarrow}%
\mathrel{\begin{tikzpicture}[baseline= {( $ (current bounding box.south) + (0,-0.5ex) $ )}]
\node[inner sep=.5ex] (\thesarrow) {$\scriptstyle #1$};
\path[draw,<-,decorate,
  decoration={zigzag,amplitude=0.7pt,segment length=1.2mm,pre=lineto,pre length=4pt}] 
    (\thesarrow.south east) -- (\thesarrow.south west);
\end{tikzpicture}}%
}

\begin{document}

\[ 
A\xrightarrow{f} B\quad A\rightsquigarrow B\quad A\xrsquigarrow{f}B\quad A\xrsquigarrow{(f\circ g)\circ h}B
\]

\end{document}

enter image description here

This definition does not cover the case in which the arrow is to be used in super/sub-scripts.