8

Sometimes I want to plug in text using overbrace/stackrel inside an equation, and have it in more than one line. what is the most straightforward way to do it?

What I wish to have is in the picture below. My (somewhat dumb) workaround is:

\documentclass[a4paper]{article}
\usepackage{amsmath,times}
\begin{document}
$$e = \underbrace{m}_{\substack{\text{I wish this text}\\ \text{ to be in two lines}} }\cdot \underbrace{c^2}_{\text{and the same here} }$$
\end{document}

enter image description here

Any (better) solutions?

Thanks!

  • 1
    Do \usepackage{mathptmx} instead of \usepackage{times} or, even better, \usepackage{newtxtext,newtxmath}. – egreg Jan 04 '17 at 10:53

5 Answers5

6

You could use \parbox instructions. In the following example, I use \Centering (from the raggede package) rather than the basic \centering instruction, as \Centering provides automatic line length balancing.

enter image description here

\documentclass[a4paper]{article}
\usepackage{amsmath,newtxtext,ragged2e} % 'times' package is deprecated
\begin{document}
\[
E = \underbrace{m}_{\parbox{1.75cm}{\Centering\scriptsize I wish this text to be in two lines}}    
\cdot \underbrace{c^2}_{\parbox{1.25cm}{\Centering\scriptsize and the same here}}
\]
\end{document}
Mico
  • 506,678
4

Other possibilities are mentioned below:

$$e = \underbrace{m}_{\substack{\text{I wish this text}\\ \text{ to be in two lines}}}\cdot \underbrace{c^2}_{\text{and the same here} }$$

$$e = \underbrace{m}_{\parbox{10em}{\scriptsize I wish this text  to be in two lines}}\cdot \underbrace{c^2}_{\text{and the same here} }$$

$$e = \underbrace{m}_{\text{I wish this text }\atop\text{to be in two lines}}\cdot \underbrace{c^2}_{\text{and the same here} }$$
Saravanan
  • 1,475
4

You can define a \textsubstack command that applies \text to each chunk (separated by \\):

\documentclass[a4paper]{article}

\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\textsubstack}{m}
 {
  \seq_set_split:Nnn \l_omer_textsubstack_input_seq { \\ } { #1 }
  \seq_set_map:NNn \l_omer_textsubstack_output_seq
    \l_omer_textsubstack_input_seq
    { \exp_not:n { \text {##1} } }
  \substack{ \seq_use:Nn \l_omer_textsubstack_output_seq { \\ } }
 }
\seq_new:N \l_omer_textsubstack_input_seq
\seq_new:N \l_omer_textsubstack_output_seq
\ExplSyntaxOff

\begin{document}

\begin{equation*}
  e = \underbrace{m}_{\textsubstack{I wish this text\\to be in two lines}}
      \cdot \underbrace{c^2}_{\textsubstack{and the\\ same here}}
\end{equation*}

\end{document}

First the input is split at \\; then a new sequence is built surrounding each item with \text{...}, then the sequence is produced, with \\ as separator.

enter image description here

egreg
  • 1,121,712
3

Here, I use tabstackengine to make the stacks. While the stack could be made with a simple \Longstack from the stackengine package, the \tabbedLongstack (even without actual tabs) allows for the \TABstackTextstyle to be specified externally, which I am using here to specify the size of the undertext.

\documentclass[a4paper]{article}
\usepackage{amsmath,times,tabstackengine}
\TABstackTextstyle{\scriptsize}
\setstackgap{L}{8pt}
\begin{document}
\[e = \underbrace{m}_{\tabbedLongstack{I wish this text\\  to be in two lines}}\cdot    
  \underbrace{c^2}_{\tabbedLongstack[l]{and the same\\here but with\\left alignment} }
\]
\[e = \underbrace{m}_{\tabbedLongstack[r]{I wish this text\\  to be right
  aligned\\ in three lines}}\cdot
  \TABstackTextstyle{\tiny}
  \setstackgap{L}{6pt}
  \underbrace{c^2}_{\tabbedLongstack[l]{and the same\\here but with\\left alignment\\
    and tiny} }
\]
\end{document}

enter image description here

2

Your \substack/\text solution is quite reasonable, particularly as it does not require specifying the width. Just be aware of spurious spaces. However, if one wishes one can define a multiline version of \text:

Sample output

\documentclass[a4paper]{article}

\usepackage{amsmath,times,varwidth}

\makeatletter
\DeclareRobustCommand{\textblock}[1]{%
{\mathchoice
  {\textblockdef@\displaystyle\f@size{#1}}%
  {\textblockdef@\textstyle\f@size{\firstchoice@false #1}}%
  {\textblockdef@\textstyle\sf@size{\firstchoice@false #1}}%
  {\textblockdef@\textstyle \ssf@size{\firstchoice@false #1}}%
  \check@mathfonts
  }%
}
\def\textblockdef@#1#2#3{\begin{varwidth}{\textwidth}
  \everymath{#1}%
  \let\f@size#2\selectfont
  \baselineskip=\f@size pt
  \baselineskip=1.1\baselineskip
  \centering
  #3
  \end{varwidth}}

\begin{document}

\begin{equation*}
  e = \underbrace{m}_{\textblock{I wish this text\\to be in two lines}}
      \cdot \underbrace{c^2}_{\textblock{and the\\ same here}}
\end{equation*}

\end{document}

The definition of \textblock is based on that of \text from amstxt.sty, but just assumes we are in math mode. It will adapt its font size according to its placement in the equation, and will inherit font characteristics of the surrounding text. I have chosen to make the text centered in the box automatically. The math sizing information contains no specification of the baselineskip, so I have chosen a value that is slightly tighter than normal text.

Andrew Swann
  • 95,762