[Tex/LaTex] Arrow with plus/minus sign on top

symbols

Below you can see an arrow with a plus and a minus sign on top of it. How is this achievable in latex? Further possibilities: arrow with only plus and arrow with only minus sign on top. How can I do this?

enter image description here

Best Answer

\stackrel

LaTeX provides \stackrel to place something on a relational operator:

\documentclass{article}

\begin{document}
\[
  A \stackrel{+/-}\longrightarrow
  B \stackrel{+}\rightarrow
  C \stackrel{-}\rightarrow
  D
\]
\end{document}

Result \stackrel

  • However the arrow is too short for the first case with +/-.
  • The plus/minus signs are perfectly horizontally centered, but it does not look too good because of the arrow tip. The plus/minus signs should be moved to the left a little.

\xrightarrow

Package amsmath addresses the previous problems by providing extensible arrows:

\xrightarrow[<below>]{<above>}

The length of the arrow grows with width of the annotations below or above. The arrow tip is taken into account.

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\[ A \xrightarrow{+/-} B \xrightarrow{+} C \xrightarrow{-} D \]
\end{document}

Result \xrightarrow

But there is room for improvement:

  • The minus and plus sign start and end with a horizontal line, thus the extra space of the arrow to the left and right can be reduced a little.
\documentclass{article}
\usepackage{amsmath}

\newcommand*{\rightarrowpm}[1]{%
  \xrightarrow{\!#1\!}%
}

\begin{document}
\[
   A \rightarrowpm{+/-}
   B \rightarrowpm{+}
   C \rightarrowpm{-}
   D
\]
\end{document}

Result \xrightarrow and !

If the size of the plus and minus signs are too large, then \scriptscriptstyle can be added:

\documentclass{article}
\usepackage{amsmath}

\newcommand*{\rightarrowpm}[1]{%
  \xrightarrow{\scriptscriptstyle\!#1\!}%                     
}

\begin{document}
\[
   A \rightarrowpm{+/-}
   B \rightarrowpm{+}
   C \rightarrowpm{-}
   D
\]
\end{document}

Result \xrightarrow + \, + \scriptscriptstyle

Scalable \xrightarrow

\xrightarrow of amsmath does not resize automatically with different math styles. The last example redefines \ext@arrow for \rightarrowpm to make it scalable according to the current math style. A smaller style than \scriptscriptstyle is achieved by scaling to 90%.

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}

\makeatletter
\newcommand*{\rightarrowpm}[1]{%
  \begingroup
    \let\ext@arrow\ext@arrow@scalable
    \xrightarrow{\scriptscriptstyle\!#1\!}%
  \endgroup
}
\newcommand*{\ext@arrow@scalable}[7]{%
  \mathrel{%
    \mathpalette{\@ext@arrow@scalable{#1}{#2}{#3}{#4}{#5}{#6}{#7}}{}%
  }%
}
\newcommand*{\@ext@arrow@scalable}[8]{%
  \mathop{%
    \setbox\z@\hbox{#5#8}%
    \setbox\tw@\vbox{%
      \m@th
      \ifx#8\scriptscriptstyle
        \let\my@hbox\hbox@scriptscriptscaled
      \else
        \let\my@hbox\hbox@math
      \fi
      \my@hbox{%
        \ifx#8\scriptstyle\scriptscriptstyle\else\scriptstyle\fi
        \mkern#3mu{#6}\mkern#4mu%
      }%
      \my@hbox{%
        \ifx#8\scriptstyle\scriptscriptstyle\else\scriptstyle\fi
        \mkern#3mu{#7}\mkern#4mu%
      }%
      \copy\z@
    }%
    \hbox to\wd\tw@{\unhbox\z@}}%
  \limits
    \@ifnotempty{#7}{%
      ^{%
        \ifx#8\scriptscriptstyle
          \expandafter\hbox@scriptscriptscaled
        \else
          \expandafter\@firstofone
        \fi
        {%
          \if0#1\else\mkern#1mu\fi
          #7%
          \if0#2\else\mkern#2mu\fi
        }%
      }%
    }%
    \@ifnotempty{#6}{%
      _{%
        \ifx#8\scriptscriptstyle
          \expandafter\hbox@scriptscriptscaled
        \else
          \expandafter\@firstofone
        \fi
        {%
          \if0#1\else\mkern#1mu\fi
          #6%
          \if0#2\else\mkern#2mu\fi
        }%
      }%
    }%
}

\newcommand*{\hbox@scriptscriptscaled}[1]{%
  \hbox{%
    \scalebox{.9}{$\scriptscriptstyle#1\m@th$}%
  }%
}
\newcommand*{\hbox@math}[1]{%
  \hbox{$#1\m@th$}%
}
\makeatother

\begin{document}
\[ A \rightarrowpm{+/-} B^{A\rightarrowpm{+/-}B^{A\rightarrowpm{+/-}B}} \]
\end{document}

Result