如何繪製一條穿過表格行和列的線?

如何繪製一條穿過表格行和列的線?

有人可以告訴我如何畫一條穿過表格的某些列和行的線,就像本例中的那樣嗎?

例如,一條線穿過 a、b、c 行

\documentclass{article}
   \usepackage{float}
\begin{document}

\begin{table}[H]
    \centering
    \begin{tabular}{|c|c|c|}
\hline
        a & b & c \\
        \hline
        d & e & f \\
        \hline
        h & i & j \\
        \hline
    \end{tabular}

\end{table}

\end{document}

提前致謝。

答案1

一種可能性是使用鈦kZ 和tikzmark.

\documentclass{article}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}

\begin{table}[H]
    \centering
    \begin{tabular}{|c|c|c|}
\hline
        \tikzmarknode{a}{a} & b & \tikzmarknode{c}{c} \\
        \hline
        d & e & f \\
        \hline
        h & i & j \\
        \hline
    \end{tabular}%
\begin{tikzpicture}[overlay,remember picture]
 \draw[thick,red,-stealth] ([xshift=-1ex]a.west) -- ([xshift=1ex]c.east);
\end{tikzpicture}%  

\end{table}
\end{document}

在此輸入影像描述

\tikzmarknode請注意is的語法\tikzmarknode{<id>}{<content>},其中id是標識符,content是內容。因此,如果您想要一個1,而不是a,您可以使用相同的標識符,並且僅更改第二個參數\tikzmarknode{a}{1}。那麼表中就會有一個1。

\documentclass{article}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}

\begin{table}[H]
    \centering
    \begin{tabular}{|c|c|c|}
\hline
        \tikzmarknode{a}{1} & 2 & \tikzmarknode{c}{3} \\
        \hline
        \tikzmarknode{goat}{d} & e & f \\
        \hline
        \tikzmarknode{duck}{h} & i & j \\
        \hline
    \end{tabular}%
\begin{tikzpicture}[overlay,remember picture]
 \path[left color=blue,right color=red] 
  ([xshift=-1ex,yshift=-0.3pt]a.west) rectangle ([xshift=1ex,yshift=0.3pt]c.east);
 \draw[red,shorten >=-0.2ex,shorten <=-0.2ex]  (goat.north) -- (duck.south);
\end{tikzpicture}%  

\end{table}
\end{document}

在此輸入影像描述

這個例子也是為了說明用TikZ 您可以遠遠超越單色線條。

答案2

僅使用基本工具:

\documentclass{article}
\usepackage{float}
\usepackage[table, svgnames]{xcolor}
\usepackage{hhline, array}

\begin{document}

\begin{table}[H]
\centering\setlength{\extrarowheight}{1pt}
\begin{tabular}{|c|c|c|}
\hhline{|---|}
\noalign{\vskip \dimexpr1.5ex + 1pt-0.4pt\relax}
\hhline{>{\arrayrulecolor{Crimson}}--->{\arrayrulecolor{black}}|}
\noalign{\vskip\dimexpr-1.5ex- 1pt\relax}
    a & b & c \\
    \hhline{|---|}
    d & e & f \\
    \hhline{|---|}
    h & i & j \\
    \hhline{|---|}
\end{tabular}

\end{table}

\end{document} 

在此輸入影像描述

答案3

另一種方法如下

\documentclass{article}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
\begin{center}
  \begin{tikzpicture}
    \foreach \x in {0,2,...,6} \draw[thick] (\x,0)--(\x,6);
    \foreach \x in {0,2,...,6} \draw[thick] (0,\x)--(6,\x);

    \node at (1.5,1.5) {$g$};
   \node at (3.5,1.5) {$h$};
  \node at (5.5,1.5) {$i$};
   \node at (1.5,3.5) {$d$};
    \node at (3.5,3.5) {$e$};
   \node at (5.5,3.5) {$f$};
    \node at (1.5,5.5) {$a$};
   \node at (3.5,5.5) {$b$};
    \node at (5.5,5.5) {$c$};


    \color{magenta}
    \draw[thick,-] (8,2.5)--(0,2.5);

    \color{cyan}
    \draw[thick,-] (5,6)--(5,0);

    \color{green}
    \draw[thick,->] (.5,0)--(.5,7);

  \end{tikzpicture}
\end{center}
\end{document}

顯示器

在此輸入影像描述

筆記。在這種情況下,我沒有table像您那樣使用環境。將此作為顯示相同結果的替代方案。


我這樣做的靈感來自 @JoséCarlosSantos 在回答我的問題時的回答如何在表格內新增此箭頭和數字?

相關內容