12

I have been using \overset and \underset to stack text, e.g.:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
    $\overset{\text{a}}{\text{b}}$ \\
$\underset{\text{c}}{\text{d}}$
\end{document}

The results seems better proportioned font size, line spacing, and baseline positioning than other solutions I have found for stacked text, however, recently, I found some incompatibilities between another package and amsmath.

  • How can I create an alternative to these commands which does not use math mode, but otherwise has identical font sizes and proportions (so that the output is visually the same)?

I found the source code for \overset and \underset in amsmath.dtx:

\newcommand{\overset}[2]{\binrel@{#2}%
    \binrel@@{\mathop{\kern\z@#2}\limits^{#1}}}

\newcommand{\underset}[2]{\binrel@{#2}%
    \binrel@@{\mathop{\kern\z@#2}\limits_{#1}}}

It seems to be using some code for \limits to define the stacking, however, I can't find anything in the rest of the source of the amsmath package showing how \limits works or how to use it without first specifying math mode.

2
6

\limits is a TeX primitive used in the specification of super- and subscripts for math operators.

You could still use math mode, but just force the arguments to be typeset in text mode via some manipulation of the original \overset and \underset definitions:

\overset and \underset without AMSmath

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath

\begin{document}
\mbox{}\phantom{Without}\llap{With} \verb|amsmath|: $\overset{\text{x}}{\text{a}}~\quad~\underset{\text{x}}{\text{a}}$

\makeatletter
\renewcommand{\overset}[2]{\ensuremath{\mathop{\kern\z@\mbox{#2}}\limits^{\mbox{\scriptsize #1}}}}
\renewcommand{\underset}[2]{\ensuremath{\mathop{\kern\z@\mbox{#2}}\limits_{\mbox{\scriptsize #1}}}}
\makeatother

\mbox{}Without \verb|amsmath|: \overset{x}{a}~\quad~\underset{x}{a}

\end{document}

In the above minimal example, the redefinition of \overset and \underset style sets the two arguments in math mode, although it is not required to be specified explicitly. Additionally, since you're not interested in a math mode application, the binary relation spacing has been removed.

3
  • Is it possible to just use the array option like: \begin{array}{c} \text{x}\\text{a} \end{array}
    – azetina
    Jan 27 '12 at 17:00
  • @azetina: Yes, and you can specify an optional argument to array as well, using \begin{array}[t]{c} for \underset and \begin{array}[b]{c} for \overset. However, the spacing will be different since the over/under placements are not limits; something that has to be corrected for.
    – Werner
    Jan 27 '12 at 17:04
  • The \kern\z@ is not needed here. So, \makeatletter, \makeatother are not needed here too.
    – wipet
    Apr 9 '21 at 18:10
16

How to implement a \textunderset macro and its companion \textoverset depends on your real necessities. Here's one that tries keeping the main text and the subscript aligned with each other, while avoiding clashes between ascendents and discendents

\documentclass{article}
\makeatletter
\newcommand{\dynscriptsize}{\check@mathfonts\fontsize{\sf@size}{\z@}\selectfont}
\makeatother
\newcommand\textunderset[2]{%
  \leavevmode
  \vtop{\offinterlineskip
    \halign{%
      \hfil##\hfil\cr % center
      \strut#2\cr
      \noalign{\kern-.3ex}
      \dynscriptsize\strut#1\cr
    }%
  }%
}
\newcommand\textoverset[2]{%
  \leavevmode
  \vbox{\offinterlineskip
    \halign{%
      \hfil##\hfil\cr % center
      \dynscriptsize\strut#1\cr
      \noalign{\kern-.3ex}
      \strut#2\cr
    }%
  }%
}

\begin{document}
Abc \textunderset{du}{axy} \textunderset{ud}{axy} def

\bigskip

Abc \textoverset{du}{axy} \textoverset{ud}{axy} def

\bigskip

Abc \textoverset{du}{axy} \textunderset{ud}{axy} def
\end{document}

enter image description here

5
  • Would you be able to give the companion \textoverset for this, please? It’s not clear to me how to adapt the definition of \textunderset. I would ask it as a new question (and can do so if you’d like), but \overset was already mentioned in this question, so it seems more appropriate to suggest adding it here. Jan 31 '17 at 12:55
  • 2
    @PeterLeFanuLumsdaine Done, with a brand new \textoverset macro
    – egreg
    Jan 31 '17 at 13:41
  • 1
    Fantastic, thankyou so much! Yet again, I wish I could thank you more for all the wonderful help you provide here. Just in case you’re ever in Stockholm on academic purposes, feel free look me up so I can at least buy you a beer :-) Jan 31 '17 at 13:58
  • @egreg: Sorry to be a bother. Actually my problem was the vertical disalign in Abc \textoverset{aaaj}{axy} \textoverset{aaab}{axy}, caused by the j (or g, y). Even if it might not be relevant to the question here, is there a way to fix or mitigate this?
    – tkruse
    Aug 26 '20 at 10:20
  • Maybe \strut#1\cr \vphantom{(Aj}#2\cr for generally less jumpy behavior (consuming extra vertical space. May be useful for text with lots of oversets and undersets, to provide a consistent baseline.
    – tkruse
    Aug 26 '20 at 10:58
0

Another simpler and more understandable possibility using the tabular environment (100% LaTeX code)

  • To adjust de size of the stacked text just change \scriptsize for your desired size. For example, \tiny
  • To adjust the separation to the stacked text modify the value -0.3 by your desired real value.

MWE:

\documentclass{article}

\newcommand{\textunderset}[2]{\begin{tabular}[t]{@{}c@{}}#2\\[-0.3em]\scriptsize#1\end{tabular}}
\newcommand{\textoverset}[2]{\begin{tabular}[b]{@{}c@{}}\scriptsize#1\\[-0.3em]#2\end{tabular}}

\begin{document}
Abc \textunderset{du}{axy} \textunderset{ud}{axy} def

\bigskip

Abc \textoverset{du}{axy} \textoverset{ud}{axy} def

\bigskip

Abc \textoverset{du}{axy} \textunderset{ud}{axy} def
\end{document}

enter image description here

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.