23

I need to draw a horizontal line inside an align environment. E.g., in the following MWE, I need that a line separates the third row.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
    \begin{align*}
    x &= 1\\
    y & = 2 \\
    \hline
    x + y &= 3 
    \end{align*}
\end{document}

EDIT I forgot to mention, the line should not be too long to the left (notice the attached image).

6 Answers 6

27

here is a solution

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
x &= 1\\
y & = 2 \\
 \cline{1-2}
x + y &= 3 
\end{align*}
\end{document}
3
  • Could you please give an explanation about what \cline{} is and what its arguments control.
    – mcp
    Commented Feb 24, 2022 at 20:10
  • This solution puts a tremendous amount of vertical whitespace around the line.
    – mcp
    Commented Feb 24, 2022 at 20:15
  • 1
    This is what I came up with.
    – mcp
    Commented Feb 25, 2022 at 3:50
7

I've done a lot of research on this problem and this is the simplest solution from all that I've seen. While touhami's answer is great, I take issue with the vertical spacing.

In order to use things like \hline in a math environment, I found you need to be in a an "sub" math environment* like split, aligned, etc.. Once so, you can use your code unchanged:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
  \begin{split}
    x &= 1 \\
    y &= 2 \\
    \hline
    x + y &= 3 
  \end{split}
\end{equation*}
\end{document}

nonsensical equation

If you want to include the equation number replace the equation* environment with equation:

nonsensical equation numbered

I'm not entirely pleased with the vertical spacing surrounding \hline. It could be a bit more with some formulas, but \hline \\[-.5ex] is not respected. Feel free to leave a solution in the comments and I'll incorporate it.

Other options to document might include trying to wrangle \frac{top}{bottom} to do what you want.


*Fortification of this statement welcome! My latex knowledge is limited.

Helpful resource:
User Guide for amsmath

Update [2.27.22]

I found a clean way to add a bit more space after the \hline!

\noalign{\smallskip} \hline \noalign{\smallskip}

Which can be stored in a command:

\newcommand{\eqline}{\noalign{\smallskip} \hline \noalign{\smallskip}}

Output:
nonsensical equation with good spacing

Updated tex:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\eqline}{\noalign{\smallskip} \hline \noalign{\smallskip}}
\begin{document}
\begin{equation*}
  \begin{split}
    x &= 1 \\
    y &= 2 \\
    \eqline{}
    x + y &= 3 
  \end{split}
\end{equation*}
\end{document}
6

Not too sophisticated but you could use a simple rule here.

% arara: pdflatex

\documentclass{article}
\usepackage{mathtools}

\begin{document}
    \begin{align*}
        x &= 1\\
        y & = 2 \\[-\jot] % you may decrease the vertical space here and in the next line as well.
        \mathclap{\rule{2cm}{0.4pt}}\\
        x + y &= 3 
    \end{align*}
\end{document}

enter image description here

2
  • What is the significance of \mathclap{}?
    – mcp
    Commented Feb 24, 2022 at 20:10
  • @young_souvlaki it sets the horizontal part of the bounding box to zero. Like this, the rule does not occupy any space in horizontal direction.
    – LaRiFaRi
    Commented Mar 3, 2022 at 14:17
5

Here's another

\documentclass{scrartcl}
\usepackage{mathtools,array}
\begin{document}
\[
  \begin{array}{>{\displaystyle{}}r@{}>{\displaystyle{}}l}
    x &= 1\\
    y & = 2 \\
     \hline
    x + y &= 3 
  \end{array}
\]
\end{document}

or may be you like the spacing of booktabs more

\documentclass{scrartcl}
\usepackage{mathtools,array,booktabs}
\begin{document}
\[
  \begin{array}{>{\displaystyle{}}r@{}>{\displaystyle{}}l}
    x &= 1\\
    y & = 2 \\
     \midrule
    x + y &= 3 
  \end{array}
\]
\end{document}
3

Just for sake of completeness. In plain TeX, we have no problem.

$$
  \eqalign{
        x &= 1 \cr
        y &= 2 \cr \noalign{\smallskip\hrule\smallskip}
    x + y &= 3 
  }
$$    
\bye

gives desired result.

0

By using \mathclap in mathtools package you have many options, like space between previous and after lines, line height and lenghth.

\documentclass{article}
 \usepackage{amsmath}
 \usepackage{mathtools}
\begin{document}
  \begin{eqnarray*}
   x& = 1 \\
   y& = 2 \\[-3mm]
 \hspace{13mm}\mathclap{\rule{6cm}{0.4pt}}\\[-2mm]
   x + y &= 3 
  \end{eqnarray*}
\end{document}
1
  • Can you explain a little bit the extra value of your answer to @LaRiFaRi's answer?
    – cabohah
    Commented Jan 8 at 7:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .