[Tex/LaTex] Horizontal lines above and below text

alignrules

How can I have two lines – one above and one below a portion of text (or numbers) in an align environment. Like for the sum in a math problem for kids. In the given example, I need a line above and below the 26 on the last row, without affecting the row spacing a lot. I much prefer not having to use a table.

 \textbf{Amy has 12 red buttons and 14 green buttons. How many buttons does she have in all?} \par
\vspace{5mm}
\begin{align*}
&\text{Number of red buttons} & & 12\\
&\text{Number of green buttons} & &14\\
&\text{Total number of buttons Amy has} & &26\\
\end{align*}

Best Answer

You can set the answer in a tabular with an \hline at the top and bottom. To ensure it doesn't affect the line spacing, \smash it:

enter image description here

\documentclass{article}

\usepackage{amsmath}

\newcommand{\Answer}[1]{\smash{\begin{tabular}{l} \hline #1 \\ \hline \end{tabular}\hspace*{-\tabcolsep}}}

\begin{document}

\noindent
Amy has 12 red buttons and 14 green buttons. How many buttons does she have in all?
\begin{align*}
  & \text{Number of red buttons}           & 12 \\
  & \text{Number of green buttons}         & 14 \\
  & \text{Total number of buttons Amy has} & \Answer{26}
\end{align*}

\noindent
Amy has 12 red buttons and 14 green buttons. How many buttons does she have in all?
\begin{center}
  \begin{tabular}{ l @{\qquad} r }
    Number of red buttons           & 12 \\
    Number of green buttons         & 14 \\
    Total number of buttons Amy has & \Answer{26}
  \end{tabular}
\end{center}

\end{document}

You can set the content in a tabular as well, and adjust the vertical padding to match that of an align, if needed.

Related Question