TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A Latex newbie has trouble doing shading here. This is the code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds, matrix,fit,matrix,decorations.pathreplacing, calc, positioning}

\begin{document}
\begin{tikzpicture}
\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) -- cycle;

\draw[-,dotted,blue] (-2.5,2)--(-2,2);
\draw[-,dotted,blue] (-2.5,1.6)--(-1.6,1.6);
\draw[-,dotted,blue] (-2.5,1.2)--(-1.2,1.2);
\draw[-,dotted,blue] (-2.5,0.8)--(-0.8,0.8);
\draw[-,dotted,blue] (-2.5,0.4)--(-0.4,0.4);

\draw[-,thick,red] (0.4,-0.4)--(0.4,-2.5);
\draw[-,thick,red] (0.8,-0.8)--(0.8,-2.5);
\draw[-,thick,red] (1.2,-1.2)--(1.2,-2.5);
\draw[-,thick,red] (1.6,-1.6)--(1.6,-2.5);
\draw[-,thick,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] (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}

enter image description here

I would highly appreciate it if someone can help me improve the following:

1) Make thicker or bigger dots in the northwest quadrant. 2) Get rid of the dashes along the borders of the southwest quadrant. 3) Change the shaded quadrant in the southwest from grey to uniform green.

share|improve this question
up vote 4 down vote accepted

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}

TikZ demo

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)$);

try to make content more readable

\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}
share|improve this answer
    
Thanks for teaching us. +1. White background for nodes content would help readability. – Sigur Jan 15 at 22:11
    
@Sigur Well... I wouldn't expect it to help you !! I wondered about white background but wasn't sure how best to implement it. It seems painful to have to add it after the matrix is drawn given variable sized content, but I guess that would be the only way? – cfr Jan 15 at 22:14
    
Hum, there is only one node on each entry of the matrix if I'm not wrong. Maybe small nodes inside each node and then it could be filled white. Thanks. – Sigur Jan 15 at 22:27
1  
@Sigur Please see edit above. – cfr Jan 15 at 22:39
    
offense? Why on Earth? Bookmarked for future reference on clean design. Thanks. – Rmano Jan 15 at 22:52

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}

enter image description here

share|improve this answer
    
Great! Thanks a lot... – bluepole Jan 15 at 21:46
1  
The - and . in your line styles don't seem necessary? – cfr Jan 15 at 22:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.