0

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{equation}
\begin{split}
A &\xrightarrow{\mathrm{closed\, loop}} B \\
C &\rightarrow D
\end{split}
\end{equation}
\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{equation}
\begin{split}
A &\xrightarrow{\mathrm{closed\, loop}} B \\
C &\parbox{\widthof{$\xrightarrow{\mathrm{closed\, loop}}$}}{\rightarrowfill} D
\end{split}
\end{equation}
\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?

LSchoon
  • 338

2 Answers2

3

You only need a \hphantom.

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

enter image description here

  • 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 '20 at 10:13
0

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{equation} \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{equation} \end{document}

enter image description here

ZhiyuanLck
  • 4,516