Shading with TikZ
This is intended to demonstrate a couple of different things:
- style can be added to nodes within a matrix of nodes by adding the configuration between
|...|
within the cell itself, and this seems the easiest way to fill the lower left node with anything; fill=<colour>
fills with a uniform colour, as opposed to a shading;- to avoid the dots and lines semi-obscuring the content of the nodes, they need to be drawn behind them and, since we can't easily add options to fill only half a node into the matrix,
on background layer
comes into its own here; - the
patterns
library provides various patterns which may be used to fill paths - these are not very flexible, but they are simple and efficient if they happen to be suitable (which may or may not be the case here); - avoid hard-coding specific locations and dimensions when possible to maintain flexibility and consistency e.g. use node anchors rather than absolute coordinates to specify the paths for decorating the matrix.
Putting this into practice:
\begin{tikzpicture}
It will give a tidier result if we draw the border at the end, so let's not bother here.
\matrix (S) [matrix of math nodes, nodes={outer sep=0pt, minimum width=25mm, minimum height=25mm}]
{
R_{11} & R_{12}\\
Fill the lower left node as part of the node specification and it will automatically fill behind the node content.
|[fill=green!50!black!50]| R_{21} & R_{22}\\
};
Use node anchors to add the dashed lines, avoiding the borders around the lower left node completely.
\draw [dashed, gray] (S-2-2.north east) -| (S-1-1.north east) (S-1-1.north west) -- (S-2-2.south east);
Put the pattern fills behind the nodes so that they don't obscure the content; use node anchors again and try out two standard patterns from the library.
\begin{scope}[on background layer]
\path [pattern=dots, pattern color=blue] (S-1-1.north west) |- (S-1-1.south east) -- cycle;
\path [pattern=vertical lines, pattern color=red] (S-2-2.north west) |- (S-2-2.south east) -- cycle;
\end{scope}
I haven't changed this because it will tend to alter spacing but the recommendation is to switch to the syntax provided by the positioning
library. (You're loading this but not using it.)
\node [above] (G1) at (S-1-1.north) {G1};
\node [left] (G1) at (S-1-1.west) {G1};
\node [above] (G2) at (S-1-2.north) {G2};
\node [left] (G2) at (S-2-1.west) {G2};
Finally, draw the border over everything else so that we ensure neat edges.
\draw (S-2-1.south west) rectangle (S-1-2.north east);
\end{tikzpicture}
Complete code:
\documentclass[border=5pt, multi, tikz]{standalone}
\usetikzlibrary{backgrounds,matrix,patterns}
\begin{document}
\begin{tikzpicture}
\matrix (S) [matrix of math nodes, nodes={outer sep=0pt, minimum width=25mm, minimum height=25mm}]
{
R_{11} & R_{12}\\
|[fill=green!50!black!50]| R_{21} & R_{22}\\
};
\draw [dashed, gray] (S-2-2.north east) -| (S-1-1.north east) (S-1-1.north west) -- (S-2-2.south east);
\begin{scope}[on background layer]
\path [pattern=dots, pattern color=blue] (S-1-1.north west) |- (S-1-1.south east) -- cycle;
\path [pattern=vertical lines, pattern color=red] (S-2-2.north west) |- (S-2-2.south east) -- cycle;
\end{scope}
\node [above] (G1) at (S-1-1.north) {G1};
\node [left] (G1) at (S-1-1.west) {G1};
\node [above] (G2) at (S-1-2.north) {G2};
\node [left] (G2) at (S-2-1.west) {G2};
\draw (S-2-1.south west) rectangle (S-1-2.north east);
\end{tikzpicture}
\end{document}
EDIT
Sigur's point about legibility is an important one, although it is difficult to decide how best to address this.
One way is to use the calc
library and to draw a small square relative to the centre of the affected nodes. I've used a 5mm square but obviously this could be adjusted or a rectangle used, according to need.
I'll try to resist the temptation to explain further and I'm sorry if my explanations misfired (in this case or generally). They are not intended to cause offence. I guess I am just used to trying to break things down into simpler steps.
\path [fill=white] ($(S-2-2.center) + (-2.5mm,-2.5mm)$) rectangle ($(S-2-2.center) + (2.5mm,2.5mm)$) ($(S-1-1.center) + (-2.5mm,-2.5mm)$) rectangle ($(S-1-1.center) + (2.5mm,2.5mm)$);
\documentclass[border=5pt, multi, tikz]{standalone}
\usetikzlibrary{backgrounds,matrix,patterns,calc}
\begin{document}
\begin{tikzpicture}
\matrix (S) [matrix of math nodes, nodes={outer sep=0pt, minimum width=25mm, minimum height=25mm}]
{
R_{11} & R_{12}\\
|[fill=green!50!black!50]| R_{21} & R_{22}\\
};
\begin{scope}[on background layer]
\draw [dashed, gray] (S-2-2.north east) -| (S-1-1.north east) (S-1-1.north west) -- (S-2-2.south east);
\path [pattern=dots, pattern color=blue] (S-1-1.north west) |- (S-1-1.south east) -- cycle;
\path [pattern=vertical lines, pattern color=red] (S-2-2.north west) |- (S-2-2.south east) -- cycle;
\path [fill=white] ($(S-2-2.center) + (-2.5mm,-2.5mm)$) rectangle ($(S-2-2.center) + (2.5mm,2.5mm)$) ($(S-1-1.center) + (-2.5mm,-2.5mm)$) rectangle ($(S-1-1.center) + (2.5mm,2.5mm)$);
\end{scope}
\node [above] (G1) at (S-1-1.north) {G1};
\node [left] (G1) at (S-1-1.west) {G1};
\node [above] (G2) at (S-1-2.north) {G2};
\node [left] (G2) at (S-2-1.west) {G2};
\draw (S-2-1.south west) rectangle (S-1-2.north east);
\end{tikzpicture}
\end{document}
to 1) You may move style definitions at another place. This is convenient, if the styles are more complex and they can be changed easily at this central place. You can see this in the options block after begin tikzpicture.
to 2) The unwanted dashes result from the cycle. Here it is sufficient to make a straight line from one edge to another.
to 3) The shading allows the definition of top and bottom colors. In my code example it is 50% green and 70% black.
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds, matrix,fit,matrix,decorations.pathreplacing, calc, positioning}
\begin{document}
\begin{tikzpicture}[
line blue/.style={., dotted, blue, very thick},
line red/.style={-, red, very thick},
]
\matrix [draw, inner sep=0pt] (S)
[matrix of math nodes,nodes={outer sep=0pt,minimum width=25mm,minimum height=25mm}]
{R_{11} & R_{12}\\ R_{21} & R_{22}\\};
\draw [dashed,gray] (S-2-2.north east) --++(180:5cm);
\draw [dashed,gray] (S-2-1.south east) --++(90:5cm);
\draw [dashed,gray] (S-1-1.north west) -- (S-2-2.south east);
\draw [line blue] (-2.5,2)--(-2,2);
\draw [line blue] (-2.5,1.6)--(-1.6,1.6);
\draw [line blue] (-2.5,1.2)--(-1.2,1.2);
\draw [line blue] (-2.5,0.8)--(-0.8,0.8);
\draw [line blue] (-2.5,0.4)--(-0.4,0.4);
\draw [line red] (0.4,-0.4)--(0.4,-2.5);
\draw [line red] (0.8,-0.8)--(0.8,-2.5);
\draw [line red] (1.2,-1.2)--(1.2,-2.5);
\draw [line red] (1.6,-1.6)--(1.6,-2.5);
\draw [line red] (2.0,-2.0)--(2.0,-2.5);
\begin {scope}[on background layer]
% [show background rectangle, background rectangle/.style={fill=magenta}]
\draw [thin, shade,top color=green!50!black!70]
(S-2-1.south west) |- (S-2-2.south west) |-
(S-1-2.south west) |- (S-1-1.south west) --
cycle;
\end{scope}
\node[rotate=0,above] (G1) at (S-1-1.north) {G1};
\node[rotate=0,left] (G1) at (S-1-1.west) {G1};
\node[rotate=0,above] (G2) at (S-1-2.north) {G2};
\node[rotate=0,left] (G2) at (S-2-1.west) {G2};
\end{tikzpicture}
\end{document}