16

I want to place text over arrows. The obvious choice is to use the amsmath command \xrightarrow{#1} However, sometimes the text above may be a single digit:

%Example 1
\xrightarrow{1}

other times the text is slightly longer:

%Example 2
\xrightarrow{1000}

The problem I have is that the length of the arrow is scaled to the length of the text. So the arrow in example 1 is much shorter than the arrow in example 2.

Is there a way of making the arrows the same length? Should I use a different command altogether?

2 Answers 2

26

In such cases, the command \makebox comes handy. It puts the argument text into a box of customizable width. Even more useful in math mode is \mathmakebox provided by the mathtools package, because it's working better in math mode and respects the current math style.

So, we could start with \xrightarrow{\mathmakebox[3em]{1}}. But we can do better - let's write a macro for that, which produces an arrow of the same width like a text we specify. Here's such a complete example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\newlength{\arrow}
\settowidth{\arrow}{\scriptsize$1000$}
\newcommand*{\myrightarrow}[1]{\xrightarrow{\mathmakebox[\arrow]{#1}}}
\begin{document}
$\myrightarrow{1}$

$\myrightarrow{1000}$
\end{document}

alt text

2

The accepted answer could be improved further with the use of the xparse package. We could define a global minimum width for all xrightarrows while still allowing for the stretching of the arrow when necessary:

\documentclass{article}

\usepackage{mathtools}
\usepackage{xparse}

\newlength{\minarrow}
\settowidth{\minarrow}{\scriptsize$1000$}

\NewDocumentCommand{\myarrow}{sm}{
  \IfBooleanTF{#1}{
    \xrightarrow{#2}
  }{
    \xrightarrow{\mathmakebox[\minarrow]{#2}}
  }
}

\begin{document}
  \(\myarrow{1}\)
  \(\myarrow*{This is a sentence longer than the minimum length.}\)
\end{document}

We use the starred version of the command to indicate that this arrow is is allowed to stretch. Some might argue this is not an improvement, as now we might have overset arrows of different lengths, but in war and love everything is permitted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .