4

I'm using tikz to draw a matrix, and I want to circle some elements and fill the background. As I byproduct, the text inside these cells is opaque too. Is there a way to emphasize it or recolor with black? To put it differently, I want the text of the matrix to be in the front.

Here's a MWE:

\documentclass{article}

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

\begin{tikzpicture}

\matrix[matrix of math nodes,row sep=5pt,column sep = 1pt,color = black] (m) {
c &  2 &  3 &  4 &  5\\
};
\draw[fill=red!20, fill opacity=0.5] ($(m-1-2.north west)-(.5ex,0)$) -- 
($(m-1-5.north east)+(.5ex,0)$)-- ($(m-1-5.south east)+(.5ex,0)$)--($(m-1-2.south west)-(.5ex,0)$) --cycle
; 
\end{tikzpicture}

\end{document}

The output is:

output

1 Answer 1

11

For black texts you can use blend mode=multiply.

\documentclass{article}

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

\begin{tikzpicture}

\matrix[matrix of math nodes,row sep=5pt,column sep = 1pt,color = black] (m) {
c &  2 &  3 &  4 &  5\\
};
\draw[fill=red!20, fill opacity=0.5,blend mode=multiply] ($(m-1-2.north west)-(.5ex,0)$) -- 
($(m-1-5.north east)+(.5ex,0)$)-- ($(m-1-5.south east)+(.5ex,0)$)--($(m-1-2.south west)-(.5ex,0)$) --cycle
; 
\end{tikzpicture}

\end{document}

enter image description here

In general you can put the fill on the background layer using e.g. the backgrounds library.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{calc} 
\usetikzlibrary{backgrounds} 
\begin{document}

\begin{tikzpicture}

\matrix[matrix of math nodes,row sep=5pt,column sep = 1pt,color = black] (m) {
c &  2 &  3 &  4 &  5\\
};
\scoped[on background layer]{\draw[fill=red!20, fill opacity=0.5] 
($(m-1-2.north west)-(.5ex,0)$) -- 
($(m-1-5.north east)+(.5ex,0)$)-- ($(m-1-5.south east)+(.5ex,0)$)--($(m-1-2.south west)-(.5ex,0)$) --cycle
;} 
\end{tikzpicture}

\end{document}
1
  • That's exactly what I was looking for. Thanks! Apr 19, 2020 at 21:50

You must log in to answer this question.

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