Vertically aligning text and equation in two minipages

by samirem   Last Updated July 17, 2017 10:23 AM

I have two minipages next to each other, the first one containing text that describes an equation, and the second one containing the actual equation. I want to align the text vertically so that the first line of text uses the same baseline as the equation.

This is what I've got so far, but as you can see, not really right. The small spacing error annoys me and I feel like I don't understand how it works with the vertical spacing.

\documentclass[a4paper, 11pt, fleqn]{article}

\usepackage{autobreak}
\usepackage[left=15mm,right=15mm]{geometry}
\usepackage{etoolbox}

\newcommand{\zerodisplayskips}{%
  \setlength{\abovedisplayskip}{0pt}%
  \setlength{\belowdisplayskip}{0pt}%
  \setlength{\abovedisplayshortskip}{0pt}%
  \setlength{\belowdisplayshortskip}{0pt}}
\appto{\normalsize}{\zerodisplayskips} % To omit vertical spacing in the align-environment

\newlength{\equationheight}
\setlength{\mathindent}{0pt}
\newcommand{\textWidth}{.17\linewidth}
\newcommand{\eqnWidth}{.81\linewidth}


\begin{document}

    \paragraph{Long-Term Load}\mbox{}\\[-1.4ex]
    \settoheight{\equationheight}{$\alpha_{\mathrm{long}}=\alpha(1+\varphi_{\mathrm{creep}})$}
    \addtolength{\equationheight}{-1ex}
    \begin{minipage}[t]{\textWidth}
        \vspace{0ex} \vspace{\equationheight} Stiffness Ratio, Long-term \raggedright
    \end{minipage}
    \hfill
    \begin{minipage}[t]{\eqnWidth}
        \begin{align*}
            \begin{autobreak}
                \alpha_{\mathrm{long}}=\alpha(1+\varphi_{\mathrm{creep}})=6.06(1+0)=6.06
            \end{autobreak}
        \end{align*}
    \end{minipage}\\[0.5ex]
    %
    \settoheight{\equationheight}{$\dfrac{\dfrac{b\,h^2}{2} + (\alpha_{\mathrm{long}}-1)A_{\mathrm{s}} \, d}{A_{\mathrm{I,long}}}$}
    \addtolength{\equationheight}{-1ex}
    \begin{minipage}[t]{\textWidth}
        \vspace{0ex} \vspace{\equationheight} Distance to C.o.G \raggedright
    \end{minipage}
    \hfill
    \begin{minipage}[t]{\eqnWidth}
        \begin{align*}
            \begin{autobreak}
                x_{\mathrm{I,long}}
                    =\dfrac{\dfrac{b\,h^2}{2} + (\alpha_{\mathrm{long}}-1)A_{\mathrm{s}} \, d}{A_{\mathrm{I,long}}}
                    =\dfrac{\dfrac{300\cdot600^2}{2} + (6.06-1)679 \cdot 564}{183434}
                    =304.9\text{ mm}
            \end{autobreak}
        \end{align*}
    \end{minipage}\\[0.5ex]
    %
    \settoheight{\equationheight}{$\dfrac{b \, h^3}{12} + b \, h (\dfrac{h}{2}-x_{\mathrm{I,long}})^2 + (\alpha_{\mathrm{long}}-1) A_{\mathrm{s}} \, (d-x_{\mathrm{I,long}})^2$}
    \addtolength{\equationheight}{-1ex}
    \begin{minipage}[t]{\textWidth}
        \vspace{0ex} \vspace{\equationheight} Second Moment of Area \raggedright
    \end{minipage}
    \hfill
    \begin{minipage}[t]{\eqnWidth}
        \begin{align*}
            \begin{autobreak}
                I_{\mathrm{I,long}}
                    =\dfrac{b \, h^3}{12} + b \, h (\dfrac{h}{2}-x_{\mathrm{I,long}})^2 + (\alpha_{\mathrm{long}}-1) A_{\mathrm{s}} \, (d-x_{\mathrm{I,long}})^2
                    =\dfrac{300\cdot600^3}{12} + 300\cdot600 (\dfrac{600}{2}-304.9)^2 + (6.06-1) 679 (564-304.9)^2
                    =5.63\cdot10^9 mm^4
            \end{autobreak}
        \end{align*}
    \end{minipage}\\[0.5ex]

\end{document}

Output: output

Can anyone here help me understand what I'm missing? I thought that

\settoheight{\equationheight}{...<equation>...}
...
\vspace{\equationheight} <text>

would be sufficient to get the same baseline for the text and the equation.

PS. The align* and autobreak environments are always there because I do not have full control over the input, and occasionally the equations are too long to fit a single line.



Answers 3


My solution is based on TeX primitives and plain TeX macros only. The basic idea is that each \eqline includes \hbox with two \vtops, because you need to have first lines aligned. First \vtop includes settings of \hsize and \raggedright. Second \vtop includes \halign with two columns declared as \displaystyle for left and right sides of equations. More lines in an equation must be separated by \cr.

\def\eqlin#1#2{\medskip\hbox{\vtop{\hfuzz=2pt \hsize=.17\hsize \raggedright \noindent#1}%
   \kern.02\hsize
   \vtop{\halign{\hfil$\displaystyle##$&$\displaystyle{}##$\hfil\cr#2\crcr}}}}

\eqlin{Stiffness Ratio, Long-term}
  {\alpha_{\rm long} &= \alpha(1+\varphi_{\rm creep}) = 6.06(1+0) = 6.06}

\eqlin{Distance to C.o.G}
  {x_{\rm I,long} &= {\displaystyle {b\,h^2\over2} + (\alpha_{\rm long} - 1) A_{\rm s} d \over A_{\rm I,long}} 
                   = {\displaystyle {300\cdot600^2\over2} + (6.06-1)\,697\cdot564 \over 183434} = 304.9\,\rm mm
  }

\eqlin{Second Moment of Area}
  {I_{\rm I,long} &= {b\,h^3\over12} + b\,h \left({h\over2}-x_{\rm I,long}\right)^2 
                    + (\alpha_{\rm long} - 1)\, A_{\rm s}\, (d-x_{\rm I,long}) \cr
                  &= {300\cdot600^3\over12} + 300\cdot600\,\left({600\over2} - 304.9\right)^2
                    + (6.06 - 1)\,679\,(564-304.9)^2 \cr
                  &= 5.63\cdot 10^9 \rm mm^4
  }

\bye

EDIT OK, due to your comment: there is second solution, which implements a simple "autobreak" feature: first equal sign behaves like &= and other equal sign behaves like \penalty0{}= and this is a possible break point. Moreover, equal signs are aligned in all equations.

\def\adef#1{\catcode`#1=13 \begingroup \lccode`\~=`#1\lowercase{\endgroup\def~}}
\def\eqA{&=}
\def\eqlin{\bgroup\let\tmp=\relax \adef={\ifx\tmp\relax\expandafter\eqA\else\penalty0{}=\fi}\eqlinA}
\def\eqlinA#1#2{\medskip\hbox to\hsize{\vtop{\hfuzz=2pt \hsize=.17\hsize \raggedright \noindent#1}%
   \hfil
   \vtop{\halign{\hfil$\displaystyle##$&\vtop{\hsize=.74\hsize
                 \rightskip=0pt plus1fil\noindent\binoppenalty=10000 \let\tmp=\empty 
                 $\displaystyle{}##$}\hfil\cr#2\crcr}}}\egroup}

\eqlin{Stiffness Ratio, Long-term}
  {\alpha_{\rm long} = \alpha(1+\varphi_{\rm creep}) = 6.06(1+0) = 6.06}

\eqlin{Distance to C.o.G}
  {x_{\rm I,long}  = {\displaystyle {b\,h^2\over2} + (\alpha_{\rm long} - 1) A_{\rm s} d \over A_{\rm I,long}} 
                   = {\displaystyle {300\cdot600^2\over2} + (6.06-1)\,697\cdot564 \over 183434}
                   = 304.9\,\rm mm
  }

\eqlin{Second Moment of Area}
  {I_{\rm I,long}  = {b\,h^3\over12} + b\,h \left({h\over2}-x_{\rm I,long}\right)^2 
                    + (\alpha_{\rm long} - 1)\, A_{\rm s}\, (d-x_{\rm I,long})
                   = {300\cdot600^3\over12} + 300\cdot600\,\left({600\over2} - 304.9\right)^2
                    + (6.06 - 1)\,679\,(564-304.9)^2 
                   = 5.63\cdot 10^9 \rm mm^4
  }

\bye
wipet
wipet
July 17, 2017 11:03 AM

I propose a much simpler solution, without autobreak. I added siunitx for a better formatting of numbers with units, and vertically aligned all = signs:

\documentclass[a4paper, 11pt, fleqn]{article}

\usepackage{autobreak}
\usepackage[hmargin=15mm, showframe]{geometry}
\usepackage{etoolbox}
\newcommand{\zerodisplayskips}{%
  \setlength{\abovedisplayskip}{0pt}%
  \setlength{\belowdisplayskip}{0pt}%
  \setlength{\abovedisplayshortskip}{0pt}%
  \setlength{\belowdisplayshortskip}{0pt}}
\appto{\normalsize}{\zerodisplayskips} % To omit vertical spacing in the align-environment
\setlength{\mathindent}{0pt}

\usepackage{siunitx}
\sisetup{exponent-product =\cdot}

\newcommand\descrbox[1]{\parbox[t]{0.2\linewidth}{\raggedright#1}}

\begin{document}

\paragraph{Long-Term Load}\mbox{}\\[-1.4ex]
\begin{align*}
 & \descrbox{Stiffness Ratio, Long-term\raggedright} & \alpha_{\mkern1mu\mathrm{long}} & =\alpha(1+\varphi_{\mathrm{creep}})=6.06(1+0)=6.06 \\
 & \descrbox{Distance to C.o.G}& x_{\mkern1mu\mathrm{I,long}}
                     &=\dfrac{\dfrac{b\,h^2}{2} + (\alpha_{\mathrm{long}}-1)A_{\mathrm{s}} \, d}{A_{\mathrm{I,long}}}
                    =\dfrac{\dfrac{300\cdot600^2}{2} + (6.06-1)679 \cdot 564}{183434} = \SI{304.9}{mm}\\[1.5ex]
 & \descrbox{Second Moment of Area}
                &I_{\mathrm{I,long}}
                  & =\dfrac{b \, h^3}{12} + b \, h \Bigl(\dfrac{h}{2}-x_{\mathrm{I,long}}\Bigr)^2 + (\alpha_{\mathrm{long}}-1) A_{\mathrm{s}} \, (d-x_{\mathrm{I,long}})^2 \\
 & & & =\dfrac{300\cdot600^3}{12} + 300\cdot600\Bigl(\dfrac{600}{2}-304.9\Bigr)^2 + (6.06-1) 679 (564-304.9)^2 \\
   & & & =\SI{5.63e9}{mm^4}
\end{align*}

\end{document} 

enter image description here

Bernard
Bernard
July 17, 2017 11:34 AM

Are use of minipages must be? Without it (in hope that I correct understand question) I obtain the following result:

enter image description here

If instead minipages is used tabularx the code become far more simple (and by this concise):

\documentclass[a4paper, 11pt, fleqn]{article}
\usepackage[showframe,
            left=15mm,right=15mm]{geometry}
\usepackage{amsmath}

\usepackage{booktabs,tabularx}
\usepackage[exponent-product=\cdot]{siunitx}



\begin{document}
    \paragraph{Long-Term Load}\mbox{}\\%[-1.4ex]

\noindent%
    \begin{tabularx}{\linewidth}{@{}>{\raggedright}X>{$\displaystyle}l<{$}}
Stiffness Ratio, Long-term
    &   \alpha_{\mathrm{long}}
            = \alpha(1+\varphi_{\mathrm{creep}})
            = 6.06(1+0)
            = 6.06
            \\  \addlinespace[1ex]
Distance to C.o.G 
    &   x_{\mathrm{I,long}}
        = \dfrac{\dfrac{b\,h^2}{2} + (\alpha_{\mathrm{long}}-1)A_{\mathrm{s}}\, d}
                {A_{\mathrm{I,long}}}
        = \dfrac{\dfrac{300\cdot600^2}{2} + (6.06-1)679 \cdot 564}{183434}  %\\
        = \SI{304.9}{mm}
            \\  \addlinespace[3ex]
Second Moment of Area 
    &   \begin{aligned}[t]
        I_{\mathrm{I,long}}
            & = \dfrac{b \, h^3}{12} + b \, h (\dfrac{h}{2}-x_{\mathrm{I,long}})^2 +
                (\alpha_{\mathrm{long}}-1) A_{\mathrm{s}} \, (d-x_{\mathrm{I,long}})^2  \\
            & = \dfrac{300\cdot600^3}{12} + 300\cdot600 (\dfrac{600}{2}-304.9)^2 +
                (6.06-1) 679 (564-304.9)^2  
                \\
            & = \SI{5.6e9}{mm^4}
        \end{aligned}
    \end{tabularx}

\end{document}
Zarko
Zarko
July 17, 2017 11:48 AM

Related Questions


Turn off floats temporally

Updated June 04, 2015 03:10 AM