Custom-length arrows, text over and under
Here is a \xxrightarrow
that has a first mandatory argument, the desired subscript to which the arrow should adapt (see the examples in the test document
below).
\documentclass[a4paper]{article}
\usepackage{amsmath,mathtools}
\makeatletter
\newlength\min@xx
\newcommand*\xxrightarrow[1]{\begingroup
\settowidth\min@xx{$\m@th\scriptstyle#1$}
\@xxrightarrow}
\newcommand*\@xxrightarrow[2][]{
\sbox8{$\m@th\scriptstyle#1$} % subscript
\ifdim\wd8>\min@xx \min@xx=\wd8 \fi
\sbox8{$\m@th\scriptstyle#2$} % superscript
\ifdim\wd8>\min@xx \min@xx=\wd8 \fi
\xrightarrow[{\mathmakebox[\min@xx]{\scriptstyle#1}}]
{\mathmakebox[\min@xx]{\scriptstyle#2}}
\endgroup}
\makeatother
\begin{document}
$A\xxrightarrow{1000}{1}A$
$A\xxrightarrow{1000}{1000}A\xrightarrow{1000}A$
$A\xxrightarrow{1000}[1]{1}A\xrightarrow[1]{1}A$
\end{document}
So you call it as
\xxrightarrow{<sample>}[<below>]{<above>}
where <below>
is optional.
The syntax that you have written means that the arrow has to be called with two arguments. So even if you don't have anything below the arrow, you still need to say \myrightarrow{}{above}
. To get an optional argument, define the command as:
\newcommand*{\myrightarrow}[2][]{\xrightarrow[#1]{\mathmakebox[\arrow]{#2}}}
However, this treats the two arguments differently. To see this, try putting a really long text in the upper and lower arguments. The arrow will stretch for the lower one but not for the upper one. To ensure equal treatment, we need to put both arguments in a \mathmakebox
. However, for spacing reasons we should only put the lower one in a box if it is defined. So we test for that. Here's one way to do it, probably not the most elegant (but then I'm not know for the elegance of my code!).
\def\empty{}
\newcommand*{\myrightarrow}[2][]{%
\def\temp{#1}%
\ifx\temp\empty
\def\mycmd{\xrightarrow}%
\else
\def\mycmd{\xrightarrow[{\mathmakebox[\arrow]{#1}}]}%
\fi
\mycmd{\mathmakebox[\arrow]{#2}}%
}
What this does is the following: the #1
is a placeholder for our first argument. We want to test if this is empty, which we do by defining a temporary macro (\temp
) to hold it and testing that against a pre-defined \empty
macro (note to TeXperts: I'm deliberately avoiding @
s). If it is empty, we define a temporary command to be just \xrightarrow
. If it is not, we add in the lower part with the surrounding \mathmakebox
. Then whatever that was set to, we execute it together with the command for the upper code.