# Definition dependent on font size

I would like to define an arrow similar to here. The definition should scale with the font size. At the moment, my definition looks like:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathmorphing,shapes}

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

\begin{document}

$A\xrsquigarrow{f}B$

\end{document}

How does the definition have to look like in order to get dependent on the font size (from the document class). Idea: All scales should be related to the current font size, e.g. here 12pt.

-
Use em and ex units, which depend on the current font size, instead of fixed units like mm and pt. – Jake Jan 27 at 15:03
For instance 0.135ex for 0.7pt and 0.34em for 1.2mm. Are you sure that \thesarrow shouldn't be thesarrow (without the backslash)? – egreg Jan 27 at 15:33
Slightly related: Which measurement units should one use in LaTeX? – Qrrbrbirlbel Jan 27 at 23:51

Define, as already suggested, the macro with "relative units":

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathmorphing,shapes}

\newcounter{sarrow}
\newcommand\xrsquigarrow[1]{\mathrel{%
\begin{tikzpicture}[baseline={($(current bounding box.south)+(0,-0.5ex)$)}]
\node[inner sep=.5ex] (\thesarrow) {$\scriptstyle #1$};
\draw[<-,decorate,
decoration={snake,amplitude=0.135ex,segment length=0.34em,pre=lineto,pre length=0.4em}]
(\thesarrow.south east) -- (\thesarrow.south west);
\end{tikzpicture}
}}

\begin{document}

$A\xrsquigarrow{f}B$

{\large$A\xrsquigarrow{f}B$\par}

{\Large$A\xrsquigarrow{f}B$\par}

{\LARGE$A\xrsquigarrow{f}B$\par}

{\small$A\xrsquigarrow{f}B$\par}

{\footnotesize$A\xrsquigarrow{f}B$\par}

\end{document}

In order to get automatic scaling for subscripts and superscripts (or \scriptstyle declarations), one has to work harder:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathmorphing,shapes}

\makeatletter
\newcommand\xrsquigarrow[1]{\mathrel{\mathchoice
{\hbox{\fontsize{\tf@size}{\tf@size}\selectfont\@xrsquigarrow\scriptstyle{#1}}}
{\hbox{\fontsize{\tf@size}{\tf@size}\selectfont\@xrsquigarrow\scriptstyle{#1}}}
{\hbox{\fontsize{\sf@size}{\sf@size}\selectfont\@xrsquigarrow\scriptscriptstyle{#1}}}
{\hbox{\fontsize{\ssf@size}{\ssf@size}\selectfont\@xrsquigarrow\scriptscriptstyle{#1}}}
}}
\newcommand\@xrsquigarrow[2]{%
\begin{tikzpicture}[baseline={($(current bounding box.south)+(0,-0.5ex)$)}]
\node[inner sep=.5ex] (A) {$#1#2$};
\draw[<-,decorate,
decoration={snake,amplitude=0.135ex,segment length=0.34em,pre=lineto,pre length=0.4em}]
(A.south east) -- (A.south west);
\end{tikzpicture}%
}

\begin{document}

$A\xrsquigarrow{f}B_{A\xrsquigarrow{f}B}$

{\large$A\xrsquigarrow{f}B$\par}

{\Large$A\xrsquigarrow{f}B$\par}

{\LARGE$A\xrsquigarrow{f}B$\par}

{\small$A\xrsquigarrow{f}B$\par}

{\footnotesize$A\xrsquigarrow{f}B$\par}

\end{document}

Beware that this is slow, because for each instance of \xrsquigarrow all the four variants need to be typeset.

Note. I've removed \thesarrow that does nothing more than provide a node name.

\mathchoice requires four arguments: what's to be typeset in display, text, first level sub/superscript and second level sub/superscript styles respectively. All four text will be typeset and then TeX will decide which one to use. So we set four boxes in which the font is chosen to be of size \tf@size for display and text styles, \sf@size and \ssf@size for the other two styles (these sizes are automatically computed by LaTeX when a formula is being typeset).

The box then contains \@xrsquigarrow which has two arguments: the style to be applied for the label and the label itself. This style should be \scriptstyle when the arrow is in display or text styles, \scriptscriptstyle in the other cases.

The definition of \@xrsquigarrow, apart from the additional parameter, is just the same as the one proposed before.

-
This is really great but this way it still does not scale with the size inside the math-environment, e.g. `$\scriptscriptstyle A\xrsquigarrow{f}B$. That is what I would intuitively think of! – strpeter Feb 11 at 16:11
@strpeter Your wish is my command. ;-) – egreg Feb 11 at 16:33
Excellent command, even if I do not fully understand why you wrote twice the same line in \newcommand\xrsquigarrow and why in general the definition for every single font size is required?! Could you explain your final version of the command? – strpeter Feb 12 at 0:51
@strpeter I've added a bit of explanation – egreg Feb 12 at 1:00
Why did you make the \@xsquigarrow command take two arguments when you just concatenate them? Is there any profound reason not to use a singel argument of {\scriptstyle{#1}} (and similar for scriptscriptstyle)? – Johan_E Feb 12 at 3:35