5

I want modify the following example to obtain a colored sub-table (only the border) as in the present picture.

sub-table

\documentclass{article}
\usepackage{array}


\begin{document}

\begin{tabular}{|c|c|c|c|c|}
\hline
1 & 3 & 2& B&  C \\
\hline
A & B&  C& B&  C \\
\hline
D & E & K& B&  F \\
\hline
G & H & I& B&  C\\
\hline
\end{tabular}


 \end{document}

4 Answers 4

7

That's just for fun: you can draw pretty much anything with tikz, so why not here? In particular, when using tikzmark you can conveniently annotate whatever you already have.

\documentclass{article}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\begin{document}

\begin{tabular}{|c|c|c|c|c|}
\hline
1 & \tikzmarknode{3}{3} & 2& B&  C \\
\hline
A & B&  \tikzmarknode{C1}{C}& B&  C \\
\hline
D & E & K& \tikzmarknode{B}{B}&  F \\
\hline
G & H & I& B&  \tikzmarknode{C2}{C}\\
\hline
\end{tabular}
\begin{tikzpicture}[overlay,remember picture]
\draw[red,thick] ([yshift=-1pt]$(3)!0.5!(C1)$) rectangle ([yshift=-1pt]$(B)!0.5!(C2)$);
\end{tikzpicture}
\end{document}

enter image description here

To be clear, I am not suggesting to use this if you only want to draw some red lines. In this case, I think that Leandris' very nice answer is the way to go. However, at the very moment you are interested in more fancy things, then this here might be the way to go.

\documentclass{article}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\begin{document}

\begin{tabular}{|c|c|c|c|c|}
\hline
1 & \tikzmarknode{3}{3} & 2& B&  C \\
\hline
A & B&  \tikzmarknode{C1}{C}& B&  C \\
\hline
D & E & K& \tikzmarknode{B}{B}&  F \\
\hline
G & H & I& B&  \tikzmarknode{C2}{C}\\
\hline
\end{tabular}
\begin{tikzpicture}[overlay,remember picture]
\newlength{\Border}
\Border=0.8pt
\fill[left color=red,right color=blue,shading angle=45,even odd rule] 
([yshift={-1pt+0.5\Border},xshift={-0.5\Border}]$(3)!0.5!(C1)$) rectangle 
([yshift={-1-0.5\Border},xshift={+0.5\Border}]$(B)!0.5!(C2)$)
[rounded corners]
([yshift={-1pt-0.5\Border},xshift={0.5\Border}]$(3)!0.5!(C1)$) rectangle 
([yshift={-1pt+0.5\Border},xshift={-0.5\Border}]$(B)!0.5!(C2)$)
;
\end{tikzpicture}
\end{document}

enter image description here

7

Not that elegant, but you could use a combination of \cline and \multicolumn to achieve the desired red frame around the four cells:

\documentclass{article}
\usepackage{array}
\usepackage[table]{xcolor}

\begin{document}

\begin{tabular}{|c|c|c|c|c|}
\hline
1 & 3 & 2& B&  C \\
\cline{1-2} \arrayrulecolor{red}\cline{3-4} \arrayrulecolor{black}\cline{5-5}
A & \multicolumn{1}{c!{\color{red}\vrule}}{B}& C& \multicolumn{1}{c!{\color{red}\vrule}}{B}&  C \\
\hline
D & \multicolumn{1}{c!{\color{red}\vrule}}{E} & K& \multicolumn{1}{c!{\color{red}\vrule}}{B}&  F \\
\cline{1-2} \arrayrulecolor{red}\cline{3-4} \arrayrulecolor{black}\cline{5-5}
G & H & I& B&  C\\
\hline
\end{tabular}

\end{document}

enter image description here

0
3

You can use \cellcolor{..} to color single cells. You need the xcolor package or just the colortbl package, the former loads the latter. Note that some PDF viewers hide table lines behind the color.

\documentclass{article}
\usepackage{array}
\usepackage[table]{xcolor}

\begin{document}

\begin{tabular}{|c|c|c|c|c|}
\hline
1 & 3 & 2& B&  C \\\hline
A & B&  \cellcolor{blue!25}C& \cellcolor{blue!25}B&  C \\\hline
D & E & \cellcolor{blue!25}K& \cellcolor{blue!25}B&  F \\\hline
G & H & I& B&  C \\\hline
\end{tabular}

\end{document}

enter image description here

2
  • 1
    I want please coloring only the border.
    – user105476
    Dec 1, 2018 at 22:07
  • 2
    @user105476, than you need to make your question more clear. please edit it and explain what you like to have. above answer gives exactly what you ask.
    – Zarko
    Dec 1, 2018 at 22:28
1

You can use {NiceTabular} of nicematrix. This environment is based upon {array} (of array) but creates PGF/Tikz nodes under the cells, rows, and columns.

Thus, it's possible to draw whatever rule you want by using Tikz.

Remark: In fact, {NiceTabular} has a key hvlines which draws (in black) all the horizontal and vertical rules in the tabular. I have used that key (and drawn the rectangle in red with Tikz).

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\begin{NiceTabular}{ccccc}[hvlines]
1 & 3 & 2& B&  C \\
A & B&  C& B&  C \\
D & E & K& B&  F \\
G & H & I& B&  C \\
\CodeAfter
\tikz \draw [red, thick] (2-|3) rectangle (4-|5) ;
\end{NiceTabular}

\end{document}

Output of the above code

It's also possible to use the command \Block (provided by nicematrix) in the array itself.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{ccccc}[hvlines]
1 & 3 & 2& B&  C \\
A & B & \Block[draw=red,line-width=1pt,transparent]{2-2}{}
        C& B&  C \\
D & E & K& B&  F \\
G & H & I& B&  C \\
\end{NiceTabular}

\end{document}

The output is the same.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .