TransWikia.com

Table with partially colored cells

TeX - LaTeX Asked by Pandoro on July 26, 2021

I convinced my girlfriend to write her thesis with Latex, so now whenever she gets stuck I’m the go to guy. Sadly she has requested something of me which I don’t know either. She needs a table in which some of the cells are colored as it can be seen in the following image.enter image description here

I created this using paint, but obviously I would rather use a correct way of doing it. I found both examples of how to get the diagonal lines into a cell and how to get colored cells, but I haven’t been able to combine the two.

Does anyone know a solution to this? The other alternative I thought of would be to create the table using latex, convert to pdf, load it into inkscape, add the lines and the colors and include it as a figure again. This would mean that I need to redo it every time the table changes though. I’m hoping this can be done in a more elegant way!

3 Answers

Here's a solution using matrix of nodes from TikZ:

Code

documentclass[parskip]{scrartcl}
usepackage[margin=15mm]{geometry}
usepackage{tikz}
usetikzlibrary{matrix}

begin{document}

pgfdeclarelayer{background}
pgfsetlayers{background,main}

tikzset{limon/.style={fill=lime}}

begin{tikzpicture}
    matrix (magic) [matrix of nodes,nodes={minimum width=3cm,minimum height=1cm,draw,very thin},draw,inner sep=0]
    {   8 & 1 & 6 
        3 & |[limon]| 5 & |[limon]| 7 
        4 & 9 & 2 
    };
    begin{pgfonlayer}{background}
        fill[lime,draw=black] (magic-2-1.north east) -- (magic-2-1.west) -- (magic-2-1.south east) -- cycle;
    end{pgfonlayer}

end{tikzpicture}

end{document}

Output

enter image description here

Correct answer by Tom Bombadil on July 26, 2021

Here's one of the the overkill TikZ solutions mentioned by Peter Grill. I used a modification of this solution to Gradient color in one cell of a table.

documentclass{article}
usepackage[table]{xcolor}
usepackage{tikz}

usetikzlibrary{calc,shadings}

% Andrew Stacey's code from
% https://tex.stackexchange.com/a/50054/3954
makeatletter
tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    save picture id=#1,
  },
  save picture id/.code={%
    edefpgf@temp{#1}%
    immediatewritepgfutil@auxout{%
      noexpandsavepointas{pgf@temp}{pgfpictureid}}%
  },
  if picture id/.code args={#1#2#3}{%
    @ifundefined{save@pt@#1}{%
      pgfkeysalso{#3}%
    }{
      pgfkeysalso{#2}%
    }
  }
}

defsavepointas#1#2{%
  expandaftergdefcsname save@pt@#1endcsname{#2}%
}

deftmk@labeldef#1,#2@nil{%
  deftmk@label{#1}%
  deftmk@def{#2}%
}

tikzdeclarecoordinatesystem{pic}{%
  pgfutil@in@,{#1}%
  ifpgfutil@in@%
    tmk@labeldef#1@nil
  else
    tmk@labeldef#1,(0pt,0pt)@nil
  fi
  @ifundefined{save@pt@tmk@label}{%
    tikz@scan@one@pointpgfutil@firstofonetmk@def
  }{%
  pgfsys@getposition{csname save@pt@tmk@labelendcsname}save@orig@pic%
  pgfsys@getposition{pgfpictureid}save@this@pic%
  pgf@process{pgfpointoriginsave@this@pic}%
  pgf@xa=pgf@x
  pgf@ya=pgf@y
  pgf@process{pgfpointoriginsave@orig@pic}%
  advancepgf@x by -pgf@xa
  advancepgf@y by -pgf@ya
  }%
}

colorlet{mycolor}{green!60!orange}

newcommandtikzmark[2][]{%
tikz[remember picture with id=#2] {#1;}}

newcommandTriCell[2]{%
  begin{tikzpicture}[overlay,remember picture]%
    fill[mycolor] ( $ (pic cs:#1) + (0pt,0.5ex) $ ) --  ( $ (pic cs:#2) + (0pt,1.9ex) $ ) -- ( $ (pic cs:#2) + (0pt,-0.8ex) $ ) --cycle ;
  end{tikzpicture}%
}%

begin{document}

TriCell{start1}{end1}
begin{tabular}{|c|c|c|c|}
hline
some text & some text & some text & some text 
hline
multicolumn{1}{!{vruletikzmark{start1}} c !{vruletikzmark{end1}}}{some text} & cellcolor{mycolor}some text & cellcolor{mycolor}some text & cellcolor{mycolor}some text 
hline
some text & some text & some text & some text 
hline
end{tabular}

end{document}

enter image description here

Answered by Gonzalo Medina on July 26, 2021

Here is a solution with {NiceTabular} of nicematrix.

That environment is similar to {tabular} (of array) but creates PGF/Tikz nodes under the cells, rows and columns of the array. It's possible to use these nodes in the CodeBefore to draw whatever you want with Tikz before the construction of the array.

documentclass{article}
usepackage{xcolor}
usepackage{nicematrix,tikz}

begin{document}

begin{NiceTabular}[hvlines,columns-width=3cm]
  {>{rule[-6mm]{0mm}{15mm}}ccc}
CodeBefore
  begin{tikzpicture}
  fill [lime] (2.5-|1) -- (3-|2) -- (3-|last) -- (2-|last) -- (2-|2) -- cycle ;
  draw [line join = bevel] (2-|2) -- (2.5-|1) -- (3-|2) ;
  end{tikzpicture}
Body
  8 & 1 & 6 
  3 & 5 & 7 
  4 & 9 & 2 
end{NiceTabular}

end{document}

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

Output of the above code

Answered by F. Pantigny on July 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP