How to draw a horizonal line across a row (over the entries) in an array?
2 Answers
The following macro \overtabline
puts a line in the middle of the previous table row
assuming the row is not larger than \@arstrut
(\strut
for tables that also include the setting of \arraystretch
).
\documentclass{article}
\renewcommand*{\arraystretch}{1.5}
\makeatletter
\newcommand*{\overtabline}{%
\noalign{%
% normal "baselineskip" in tabular is height + depth of \@arstrutbox
\vskip-.5\dimexpr\ht\@arstrutbox+\dp\@arstrutbox\relax
% default line thickness is 0.4pt
\vskip-.2pt\relax
\hrule
\vskip-.2pt\relax
\vskip+.5\dimexpr\ht\@arstrutbox+\dp\@arstrutbox\relax
}%
}
\begin{document}
\centering
\begin{tabular}{ll}
abc&def\\
\overtabline
ghi&jkl\\
\overtabline
\end{tabular}
\[
\begin{array}{ll}
abc&def\\
\overtabline
ghi&jkl\\
\overtabline
\end{array}
\]
\end{document}
This can also be done using \tikzmark
:
Note:
This does require two runs. First one to determine the locations, and the second to do the drawing.
The
\tikzmark
is from Adding a large brace next to a body of text.
Code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw [#1] ($(#2)+(0,0.6ex)$) -- ($(#3)+(0,0.6ex)$);
\end{tikzpicture}%
}%
\begin{document}
\[
\begin{array}{c c c}
a & b & c \\
\tikzmark{StartA}1 & 2 & 3\tikzmark{EndA} \\
\tikzmark{StartB}f & g & h\tikzmark{EndB}
\end{array}
\]
\DrawLine[red, thick]{StartA}{EndA}
\DrawLine[blue, thick, dotted]{StartB}{EndB}
\end{document}