4

In a table, made with Tikz, I tried to make the first row darker than the rest by, I think, a little of brute force. It happens that the borders I want must be rounded and the extreme cells (north west, north east, south east and south west ones) are overring it as this image shows:

enter image description here

(The lighter cyan is a little hard to see, but yes, still overriding the border)

How to solve this problem? Here's my MWE:

\documentclass{article}

\usepackage{tikz} \usetikzlibrary{matrix}

\begin{document} \begin{tikzpicture}[scale=.9] \matrix[matrix of math nodes,minimum width=2.5cm,minimum height=6mm,column 1/.style={nodes={fill=cyan!7}}, column 2/.style={nodes={fill=cyan!7}}, row 1/.style={nodes={fill=cyan!45}}] (func) {

 x & y = 2x + 1\\
-3&-5\\
-2&-3\\
-1&-1\\
0&1\\
1&3\\
2&5\\
3&7\\

};

\draw [rounded corners=5pt,thick] (func-1-1.north west) rectangle (func-8-2.south east); \draw [thick] (func-1-1.south west) -- (func-1-2.south east); \draw [thick] (func-1-1.north east) -- (func-8-1.south east); \end{tikzpicture} \end{document}

Feripinho
  • 129

2 Answers2

3

Got something working for you!

Instead of doing the fill via the matrix command, I did it afterwards, using arcs for the corners.

Tikz colored rounded table fixed

The code requires \usetikzlibrary{calc} because relative coordinates are calculated. Also, note how I am using layers to have the fill go behind the text of your matrix.

\documentclass{article}

\usepackage{tikz} \usetikzlibrary{matrix} \usetikzlibrary{calc}

\begin{document} \begin{tikzpicture}[scale=.9] \pgfdeclarelayer{background} \pgfsetlayers{background,main}

\matrix[matrix of math nodes,minimum width=2.5cm,minimum height=6mm,column 1/.style={nodes={fill=cyan!7}}, column 2/.style={nodes={fill=cyan!7}}, row 1/.style={nodes={fill=none}}] (func) { x & y = 2x + 1\ -3&-5\ -2&-3\ -1&-1\ 0&1\ 1&3\ 2&5\ 3&7\ };

% Fill in top row \begin{pgfonlayer}{background} \filldraw [fill=cyan!45,thick] (func-1-1.south west) -- ($ (func-1-1.north west)+(0,-5pt)$ ) arc (180:90:5pt) -- ($ (func-1-2.north east) + (-5pt,0) $) arc (90:0:5pt) -- (func-1-2.south east); \end{pgfonlayer}

\draw [rounded corners=5pt,thick] (func-1-1.north west) rectangle (func-8-2.south east); \draw [thick] (func-1-1.south west) -- (func-1-2.south east); \draw [thick] (func-1-1.north east) -- (func-8-1.south east); \end{tikzpicture} \end{document}

The path starts at the south-west corner of the first row. Then it goes up to the top-left corner, just 5pt below, which is the radius of your rounded corner. Then, I have the arc of 5pt, and you see how the pattern continues all the way to the south-east corner of the first row.

I must admit I'm not very familiar with tikz, so perhaps there is a quicker way to do this. Anyway, I think this is a very versatile technique, and that has some elegancy too!

Finally, I think this table is acquirable using normal TeX. Personally, I would prefer that for use in documents. Ask me if you need assistance on that.

0

With {NiceTabular} of nicematrix (version ≥ 6.23).

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

$\begin{NiceArray}{c|c}[rounded-corners,hvlines,columns-width=20mm,color-inside] \rowcolor{cyan!45} x & y = 2x+1 \ \Hline \Block[fill=cyan!7]{-1}{} % color for the first column -3 & \Block[fill=cyan!7]{-1}{} % color for the second column -5 \ -2 & -3 \ -1 & -1 \ 0 & 1 \ 1 & 3 \ 2 & 5 \ 3 & 7 \ \end{NiceArray}$

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250