TeX - LaTeX Asked on May 6, 2021
Kind of a weird circumstance, but I have a table cell where I need the text to be in the cell normally (non-split) and the cell colour to be split diagonally.
I know how to use slashbox
to split the cell, but there doesn’t seem to be any way to split the colour without also splitting the text. Basically, I need it to look like this:
Any suggestions would be greatly appreciated.
Since you didn't add an MWE, I can only suppose this is what you need:
documentclass{article}
usepackage{makecell}
usepackage{tikz}
usetikzlibrary{matrix}
% code (slightly modified) from https://tex.stackexchange.com/a/343392/101651
tikzset{
diagonal fill/.style 2 args={fill=#2, path picture={%
fill[#1] (path picture bounding box.south west) -|
(path picture bounding box.north east) -- cycle;}},
reversed diagonal fill/.style 2 args={fill=#2, path picture={
fill[#1] (path picture bounding box.north west) |-
(path picture bounding box.south east) -- cycle;}}
}
begin{document}
begin{table}
centering
begin{tikzpicture}
matrix[matrix of nodes,
row sep=-pgflinewidth,
column sep=-pgflinewidth,
column 2/.style={nodes={text width=4.5cm}},
row 1/.style={nodes={minimum height=7ex}},
align=center,
text centered,
nodes={text width=2cm,
text height=1.5ex,
text depth=.25ex,
minimum height=4ex,
}] (mymatr) {
|[reversed diagonal fill={green}{red}]|makecell{Let'srecap} & makecell{Van Duck's rules}
1& Read the manuals
2& Look at the log
3& Search on TeX.SE
4& |[diagonal fill={yellow}{orange}]|color{blue}bfseries Always add an MWE
};
draw[thick] (mymatr-1-1.north west) -- (mymatr-1-2.north east);
draw (mymatr-1-1.south west) -- (mymatr-1-2.south east);
draw[thick] (mymatr-5-1.south west) -- (mymatr-5-2.south east);
end{tikzpicture}
end{table}
end{document}
Credits to ferahfeza for the diagonal fill
and reversed diagonal fill
styles.
Correct answer by CarLaTeX on May 6, 2021
With {NiceTabular}
of nicematrix
. That environment is similar to {tabular}
(of nicematrix
) but creates PGF/Tikz nodes under the cells, rows and columns.
Thus, it's possible to use Tikz to draw whatever you want under the cells of the array.
documentclass{article}
usepackage{nicematrix}
usepackage{tikz}
begin{document}
renewcommand{arraystretch}{1.4}
begin{NiceTabular}{ccc}
CodeBefore
begin{tikzpicture}
fill [red!15] (1-|1) -- (2-|1) -- (2-|2) -- cycle ;
fill [blue!15] (1-|1) -- (1-|2) -- (2-|2) -- cycle ;
end{tikzpicture}
Body
Some text & other text & another text
Some text & other text & another text
Some text & other text & another text
end{NiceTabular}
bigskip
begin{NiceTabular}{ccc}[hvlines]
CodeBefore
begin{tikzpicture}
fill [red!15] (1-|1) -- (2-|1) -- (2-|2) -- cycle ;
fill [blue!15] (1-|1) -- (1-|2) -- (2-|2) -- cycle ;
end{tikzpicture}
Body
Some text & other text & another text
Some text & other text & another text
Some text & other text & another text
end{NiceTabular}
end{document}
You need several compilations (because nicematrix
uses PGF/Tikz nodes under the hood).
Answered by F. Pantigny on May 6, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP