在数组中某一行的条目上绘制一条水平线

在数组中某一行的条目上绘制一条水平线

如何在数组中的一行(条目上方)画一条水平线?

答案1

下面的宏\overtabline在前一个表格行的中间放置一行,假设该行不大于\@arstrut\strut对于也包含设置的表格\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}

结果

答案2

也可以使用以下方法完成\tikzmark

在此处输入图片描述

笔记:

代码:

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

相关内容