0

I would like that certain cells in my tabular have a rounded white background (as shown in the image - though I don't care if the background for "Values" would fill the whole cell as well). But I cannot figure out how I would accomplish that. All the commands for coloring cells just support a color which fills the cell.

enter image description here

Is there a way to do that? I thought about just drawing the background in the cell but I don't know how to determine how large the cell is exactly (e.g. \TX@col@width in a cell reports a lower width than it actually is and for height I found nothing). I would like to use tabularx for that table, but I'm open to switching if it makes it easier.

1 Answer 1

1

Welcome! How about using a TikZy matrix for that?

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds}
\definecolor{bf}{RGB}{207,229,210}
\begin{document}
\begin{tikzpicture}[pics/colorbar/.style={code={
\draw[ultra thin,left color=red,right color=green!70!black,middle color=yellow] (-2,-0.2) rectangle
    (2,0.2);
 \draw[fill=white] (-2+#1*0.04,0) -- ++(-60:0.3) -- ++ (180:0.3) -- cycle;}},
nn/.style={fill=white,rounded corners,align=center,minimum
width=#1,font=\sffamily},
nn/.default=6em]

 \matrix[column sep=1em,row sep=1ex,cells={nodes=nn},
    column 1/.style={nodes={nn=10em}},
    column 4/.style={nodes={sharp corners}}](mat){
  &  \node{Values}; &  \node{Refervence \\ values}; & \\ 
 \node{Car}; &  \node{57\%}; &  \node{100\%}; & 
 \pic{colorbar=57};\\ 
 \node{Bike}; &  \node{67\%}; &  \node{100\%}; & 
 \pic{colorbar=67};\\ 
 };
 \begin{scope}[on background layer]
  \fill[bf] (mat.south west) rectangle (mat.north east);
 \end{scope} 
\end{tikzpicture}
\end{document}

enter image description here

Or with a different shading.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds}
\definecolor{bf}{RGB}{207,229,210}
\pgfdeclarehorizontalshading{Maradox}{100bp}{%cf https://tex.stackexchange.com/a/344548/194703
  rgb(0bp)=(1,0,0);
  rgb(25bp)=(1,0,0);
  rgb(50bp)=(1,1,0.2);
  rgb(60bp)=(1,1,0.2);
  rgb(80bp)=(.25,0.5,.15);
  rgb(90bp)=(.25,0.5,.15);
  rgb(100bp)=(.25,0.5,.15)}
\begin{document}
\begin{tikzpicture}[pics/colorbar/.style={code={
\draw[shading=Maradox] (-2,-0.2) rectangle
    (2,0.2);
 \draw[fill=white] (-2+#1*0.04,0) -- ++(-60:0.3) -- ++ (180:0.3) -- cycle;}},
nn/.style={fill=white,rounded corners,align=center,minimum
width=#1,font=\sffamily},
nn/.default=6em]

 \matrix[column sep=1em,row sep=1ex,cells={nodes=nn},
    column 1/.style={nodes={nn=10em}},
    column 4/.style={nodes={sharp corners}}](mat){
  &  \node{Values}; &  \node{Refervence \\ values}; & \\ 
 \node{Car}; &  \node{57\%}; &  \node{100\%}; & 
 \pic{colorbar=57};\\ 
 \node{Bike}; &  \node{67\%}; &  \node{100\%}; & 
 \pic{colorbar=67};\\ 
 };
 \begin{scope}[on background layer]
  \fill[bf] (mat.south west) rectangle (mat.north east);
 \end{scope} 
\end{tikzpicture}
\end{document}

enter image description here

2
  • That is absolutely perfect. One side question: How could I color the colorbar differentely. Let's say a gradient from: 0%=red, 50%=yellow, 70%=still yellow, 100%=green?
    – Maradox
    Commented Jan 28, 2020 at 16:15
  • @Maradox You can declare your own shading with \pgfdeclarehorizontalshading. I added an example. It typically requires one to experiment a bit until one has precisely what one wants.
    – user194703
    Commented Jan 28, 2020 at 16:33

You must log in to answer this question.

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