# Define a math symbol relative to font size

## Idea

All characters, including the new defined symbol (here an arrow), should scale related to the current font size, e.g. here 12pt.

## Implementation

I would like to define an arrow similar to here. The arrow should scale with the font size. The current MWE of my definition is:

\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)?

-
Use em and ex units, which depend on the current font size, instead of fixed units like mm and pt. –  Jake Jan 27 '13 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 '13 at 15:33
Slightly related: Which measurement units should one use in LaTeX? –  Qrrbrbirlbel Jan 27 '13 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 '13 at 16:11
@strpeter Your wish is my command. ;-) –  egreg Feb 11 '13 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 '13 at 0:51
@strpeter I've added a bit of explanation –  egreg Feb 12 '13 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 '13 at 3:35

As Jake pointed out in his comment, you should replace all fixed measures (i.e. measures in mm, pt, etc) to font-dependant measures:

• em, which is equal to the current font-size (i.e., 1em = 10pt if the current font is typeset in 10pt). This length was originally related to the width of the capital "M" in the current typeface and point size, as the capital "M" was cast the full-width of the square "blocks" ("em-quads") used in printing presses.

• en, which is equal to half an em. The name en refers to the fact that traditionally the width of a lowercase letter "n" was exactly half of that of the capital "M".

• ex, which is equal to the distance between the baseline and the mean line in the current typeface and point size. The name refers here to the fact that traditionally the lowercase "x" was exactly this height, as it had no overshoot (nor ascenders or descenders).

Finally, note that the relationship between these lengths and their traditional definition (height or width of certain letters) is not necessarily true anymore in modern typefaces.

-
I would just add, that in a drawing you should use only one of them. However, in general context, em should be used for horizontal distances and ex` for vertical distances. –  yo' Jan 27 '13 at 23:15