20

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).

5 Answers 5

25

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
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{}? Feb 24 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
    Mar 3 at 14:17
4

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

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}
2

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.

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.