3

we know that, the \xrightarrow can give superscript (over the arrow) and subscript (under the arrow). for example

X\xrightarrow{f}Y

X\xrightarrow[g]{f}Y

but, I want to define a command such that it has the following style:

X\xra Y Equals to X\rightarrow{} Y
X\xra^{f}Y Equals to X\xrightarrow{f}Y
X\xra_{g}Y Equals to X\xrightarrow[g]Y

How to solve this?

  • 2
    use the xparse package to spot ^ and _ and then pass it on in the right manner to \xrightarrow – daleif Jan 6 '16 at 14:58
  • @daleif could you give some more detail – jiaopjie Jan 7 '16 at 5:27
2

Some checks for ^ and _ in either order:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\DeclareRobustCommand{\xra}{%
  \def\xra@upper{}%
  \def\xra@lower{}%
  \xra@checksup
}
\newcommand{\xra@checksup}{\@ifnextchar^{\xra@sup}{\xra@checksub}}
\newcommand{\xra@sup}[2]{% #1 is ^
  \def\xra@upper{#2}%
  \xra@checksub
}
\newcommand{\xra@checksub}{\@ifnextchar_{\xra@sub}{\xra@do}}
\newcommand{\xra@sub}[2]{% #1 is _
  \def\xra@lower{#2}%
  \xra@checksup
}
\newcommand{\xra@do}{%
  \ifx\xra@sub\@empty
    \xrigtharrow{\xra@upper}%
  \else
    \xrightarrow[\xra@lower]{\xra@upper}
  \fi
}
\makeatother

\begin{document}

$A \xra B$

$A\xra^{f} B$

$A\xra^{f}_{g} B$

$A\xra_{g} B$

$A\xra_{g}^{f} B$

\end{document}

enter image description here

2

Here is one way. Probably not the best

\documentclass[a4paper]{article}
\usepackage{xparse,amsmath,amssymb}
\NewDocumentCommand\xra{ t_ t^  m}{
  \IfBooleanTF{#1}{
    \xrightarrow[#3]{}
  }{
    \IfBooleanTF{#2}{
      \xrightarrow{#3}
    }{
      \to #3
    }
  }
}
\begin{document}
\begin{align*}
  X \xra_ t  Y \\
  X \xra^ t  Y \\
  X \xra   Y \\
\end{align*}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.