4

I have a TikZ code that generates this:

enter image description here

using something along the lines of the following snippet. The full code corresponding to this snippet can be obtained from the answer to question tikz-two-blocks-matrix:

    \pgfmathparse{\y-\x<1?"draw":""}
    \node[\pgfmathresult](mynode-\x-\y) at (\y,-\x) {$\times$};

but I would like to instead have the following theme for those selected nodes. The code where this example originates from can be found in the answer to this other question tikz-marking-several-blocks-in-a-matrix and uses matrix array definition rather than painting everything using TikZ:

enter image description here

How can I do that using TikZ? it means to basically not have a border line but rather change the background of the square to a specific color ...

UPDATE: the following code and output is self-contained and shows the existing Tikz graph I have to which I would like to add the background color changes.

\documentclass{standalone}
    \usepackage{tikz}

    \usetikzlibrary{calc,fit,matrix,arrows,automata,positioning}
\begin{document}
\begin{tikzpicture}
[zeros/.style={},transform shape]
\foreach \y in {1,...,4}{
    \foreach \x in {1,...,5}{
    \ifnum\x>\numexpr\y+1
    \node[zeros] (mynode-\x-\y) at (\y,-\x) {0};
    \else
    \pgfmathparse{\y-\x<1?"draw":""}
    \node[\pgfmathresult](mynode-\x-\y) at (\y,-\x) {$\times$};
    \ifnum\x<5
    \ifnum\x<\y\draw[<-] (mynode-\x-\y) -- +(0,-0.7);\fi
    \ifnum\x=\y\draw[<-] (mynode-\x-\y) -- +(0,-0.7);\fi
    \ifnum\y<4\draw[<-] (mynode-\x-\y) -- +(0.7,-0.7);\fi
    \fi
    \fi
    }
}
\node[fit=(mynode-1-1)(mynode-5-4),draw,thick,red,label=90:{Conquer diagonal}]{};

\begin{scope}[xshift=5.2cm]
    \foreach \x[remember=\x as \lastx] in {1,...,5}{
        \node (myvecs1-\x) at (0,-\x) {$\times$};
        \node (myvecs2-\x) at (1,-\x) {$\times$};
        \node (myvecs3-\x) at (2,-\x) {$\times$};
        \node (myvecs4-\x) at (3,-\x) {$\times$};
        \node (myvecs5-\x) at (4,-\x) {$\times$};
        \node (myvecsn-\x) at (5,-\x) {$\times$};
        \ifnum\x>1
        \draw[->] (myvecs1-\x) -- (myvecs1-\lastx);
        \draw[->] (myvecs2-\x) -- (myvecs2-\lastx);
        \draw[->] (myvecs3-\x) -- (myvecs3-\lastx);
        \draw[->] (myvecs4-\x) -- (myvecs4-\lastx);
        \draw[->] (myvecs5-\x) -- (myvecs5-\lastx);
        \draw[->] (myvecsn-\x) -- (myvecsn-\lastx);
        \fi
        }
\node[fit=(myvecs1-1)(myvecsn-5),draw,thick,green,label=90:{Apply as deep as possible}] {};
\end{scope}
\end{tikzpicture}
\end{document}

The resulting output is:

enter image description here

SkyWalker
  • 1,897
  • Could you post a small example document that provides the basic framework so people have something to play with? – Jake Sep 04 '12 at 13:49
  • @Giovanni: None of the questions you linked to contain a full document, they're missing the \documentclass and the packages. I know it's not hard to guess these to create a compilable example, but it's not a very fun task for people who want to work on an answer. It's generally preferred to post full compilable minimal example documents instead of just snippets. – Jake Sep 04 '12 at 14:10
  • @Werner there he uses pmatrix and uses TikZ to highlight something on pmatrix. Here the whole code is in TikZ so I have some node ... basically I would like this node to have a background color. – SkyWalker Sep 04 '12 at 14:11
  • @Jake true but I have a very complicated multi-page project and it is not exactly easy to get a full self contained working example but I can try ... – SkyWalker Sep 04 '12 at 14:12
  • @GiovanniAzua: I understand that it's difficult to whittle a large project down to a minimal example, but people who want to suggest a solution would have to build an example document from scratch as well. By coming up with a self-contained example document, the question also becomes more valuable to others because it's more general and doesn't just apply to your specific use-case. – Jake Sep 04 '12 at 14:14
  • 1
    OK full TikZ example added ... – SkyWalker Sep 04 '12 at 17:58
  • Please check what I have added to your code. It's not like we are the Stackexchange police but when you add a MWE it should be working when copy/pasted otherwise we have to make extra guesses each time about what part of the code is missing. It's just four lines of code and then testing if it compiles. See it as returning the favor to people who are willing to help you. – percusse Sep 04 '12 at 18:40

1 Answers1

7

You could do it like this:

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{fit}

\begin{document}

\begin{tikzpicture}
[   zeros/.style={},
    transform shape
]
\foreach \y in {1,...,4}
{   \foreach \x in {1,...,5}{
  \ifnum\x>\numexpr\y+1
    \node[zeros] (mynode-\x-\y) at (\y,-\x) {0};
  \else
    \node(mynode-\x-\y) at (\y,-\x) {$\times$};
    \ifnum\x<5
        \ifnum\x=\y\fill[red,opacity=0.1] (mynode-\x-\y.south west) rectangle (mynode-\x-\y.north east);\fi
    \fi
    \ifnum\y=\numexpr\x-1\fill[opacity=0.1] (mynode-\x-\y.south west) rectangle (mynode-\x-\y.north east);\fi
  \fi
  }
}
\node[fit=(mynode-1-1)(mynode-5-4),draw,thick,red,label=90:{Conquer diagonal}]{};
\end{tikzpicture}

\end{document}

Result

enter image description here

Tom Bombadil
  • 40,123