[Tex/LaTex] Custom-length arrows, text over and under

macros

Expanding on this question I'd like to know if it's possible to modify this macro to allow for text under the arrow as well, as the \xrightarrow[below]{under}. I don't have any experience with macros but using the answer to the aforementioned question I tried this to no effect

\newlength{\arrow}
\settowidth{\arrow}{\scriptsize$1000$}
\newcommand*{\myrightarrow}[2]{\xrightarrow[#1]{\mathmakebox[\arrow]{#2}}}

What would be the right way to do this?

Best Answer

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.

Related Question