4

Could someone tell me how can I draw a line that pass through some of the columns and rows of a table like the one on this example?

e.g. a line crossing a,b,c row

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

Thanks in advance.

Bernard
  • 271,350

3 Answers3

7

With only basic tools:

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

enter image description here

Bernard
  • 271,350
  • Thanks. Why when I add \usepackage[table, svgnames]{xcolor} marks an error saying "Option clash for package xcolor" ? I then deleted usepackage{xcolor} but the error doesn't go away, it's still there – I likeThatMeow Nov 28 '19 at 23:00
  • 1
    Probably because you already load xcolor, possibly via some package (TikZ, beamer, …), and with another or no option at all. A simple remedy consists in passing this option through the \documentclass. – Bernard Nov 28 '19 at 23:05
  • @Sebastiano: Yes, I am rather nitpicky about the correct writing of proper names. B.t.w., do you know that since our Revolution, names with a particle have been shortened of their particles (and some people were shortened, too…)? – Bernard Nov 28 '19 at 23:09
  • This afternoon I read a question about math.SE. about how to spell this name :) But I like the old version of the name. To write de l'Hospital makes me think about the hospital :-) – Sebastiano Nov 28 '19 at 23:14
  • Yes. However in modern French the 'o' takes a circumflex, non come l'ospedale. At the time of the Marquis, it was with an 's', and as, it is a proper name, I think it's better to keep the old spelling. – Bernard Nov 28 '19 at 23:18
6

One possibility is to use TikZ and 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}

enter image description here

Note that the syntax of \tikzmarknode is \tikzmarknode{<id>}{<content>}, where id is an identifier and content is the content. So, if you want a 1, say instead of the a, you could use the same identifier and only change the second argument, \tikzmarknode{a}{1}. Then there will be a 1 in the table.

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

enter image description here

This example is also to illustrate that with TikZ you can go far beyond monochrome lines.

  • Thanks. Does this Tikz feature works only for letters ? I tried with numbers but doesn't displays what's expected – I likeThatMeow Nov 28 '19 at 23:08
  • 1
    @user178403 It works for everything. Note that the syntax of \tikzmarknode is \tikzmarknode{<id>}{<content>}, where id is an identifier and content is the content. So, if you want a 1, say instead of the a, you could use the same identifier and only change the second argument, \tikzmarknode{a}{1}. Then there will be a 1 in the table. –  Nov 28 '19 at 23:11
  • Do you know why I have the error Package pgf Error: No shape named as aa is know? aa is the name gave instead of a – I likeThatMeow Nov 28 '19 at 23:25
  • Also in the line where is \tikzmarknode{aa}{1} marks Undefined control sequence – I likeThatMeow Nov 28 '19 at 23:26
  • @user178403 This probably means you were trying to use a node named aa but did not create one. You can only use aa in a path if you have previously created a node of that name, i.e. with this id. Note also that if you have a very old TeX installation, your version of tikzmark may not yet contain \tikzmarknode, so you'd need to update the installation (which is a good idea regardless). –  Nov 28 '19 at 23:29
  • I see. Thank you again. – I likeThatMeow Nov 28 '19 at 23:50
  • Schrödinger's cat, how can I draw a vertical line? I did tried and changed to north, south but the result was an inclined arrow, not a straight line. And when I change to rectangleinstead of -- the result was indeed a rectangle. – I likeThatMeow Nov 29 '19 at 00:03
  • 1
    @user178403 I added an example of a vertical line to the second example. –  Nov 29 '19 at 00:08
0

Another way to do this is the following

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

Displays

enter image description here

NOTE. In this case I'm not using the environment table as you did. Take this as an alternative to display the same result.


I did this, inspired by the answer by @JoséCarlosSantos in his answer to my question How can I add this arrows and numbers inside a table?