# Align & match length of \rightarrow and \xrightarrow

In the following example, I want to make \rightarrow be the same length as the \xrightarrow, while aligning the two.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$$\begin{split} A &\xrightarrow{\mathrm{closed\, loop}} B \\ C &\rightarrow D \end{split}$$
\end{document}


I have found that I can change the length of \rightarrow, using calc's \widthof to get the length right:

\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}
\begin{document}
$$\begin{split} A &\xrightarrow{\mathrm{closed\, loop}} B \\ C &\parbox{\widthof{\xrightarrow{\mathrm{closed\, loop}}}}{\rightarrowfill} D \end{split}$$
\end{document}


But I then lose the alignment of the two arrows (and I am not happy about using $...$ while already in an equation. Is there a way to align these two arrows? Is there a better way to get two arrows of the same length?

## 2 Answers

You only need a \hphantom.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$$\begin{split} A &\xrightarrow{\mathrm{closed\, loop}} B \\ C &\xrightarrow{\hphantom{\mathrm{closed\, loop}}} D \end{split}$$
\end{document}


• That's exactly what I wanted! I couldn't figure out how to have \xrightarrow not print out the text for the second line, which is why I started messing with \rightarrow. Thank you! – LSchoon Jun 13 at 10:13

This answer is actually the same with @Schrödinger's. I add two commands to make it conveniant to use.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\firstarrow}[2][default]{
\expandafter\gdef\csname arrow@content@#1\endcsname{#2}
\xrightarrow{\@nameuse{arrow@content@#1}}
}
\newcommand{\alignarrow}[1][default]{\xrightarrow{\hphantom{\@nameuse{arrow@content@#1}}}}
\makeatother

\begin{document}
\begin{split} A &\firstarrow{\mathrm{closed\, loop}} B \\ A &\alignarrow B\\ C &\firstarrow[test]{a_c} D\\ A &\alignarrow B\\ C &\alignarrow[test] D\\ \end{split}
\end{document}