1

Starting from the answer to this question, I have modified the code in order to have 4 different kind of cells (instead of two). Here the code:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikz[%
        every cell 0/.style={draw=black
                 , opacity=.5
                  ,  minimum height=0.1em
                  , outer sep=0pt},
       every cell 1/.style={draw=black
                  , fill=gray
                  , opacity=.5
                  ,  minimum height=0.1em
                  %, width=0.1em,
                  %,  depth=0.1em
                  , outer sep=0pt},
       every cell 2/.style={draw=red
                  , fill=gray
                  ,  minimum height=0.1em
                  %, width=0.1em,
                  %,  depth=0.1em
                  , outer sep=0pt},
       every cell 3/.style={draw=black
                  , fill=gray
                  ,  minimum height=0.1em
                  %, width=0.1em,
                  %,  depth=0.1em
                  , outer sep=0pt}
]
    \foreach \row [count=\y] in {%
        {0,0,0,0,0,1,1,1,0,0,0},%
        {0,0,0,1,1,1,0,1,1,0,0},%
        {0,0,0,1,0,0,0,0,1,0,0},%
        {0,0,0,2,0,0,0,1,1,0,0},%
        {0,0,0,3,0,0,0,1,0,0,0},%
        {0,0,1,1,0,0,0,1,0,0,0},%
        {0,0,1,1,0,0,0,1,1,1,1},%
        {0,0,1,1,0,0,0,0,0,0,0},%
        {1,1,1,1,0,0,0,0,0,0,0}}%
        \foreach \cell [count=\x] in \row  
            \path [every cell, every cell \cell]
                (\x,-\y) rectangle ++(1,1);
\end{document}

And this is the output (with two external edges missed):

enter image description here

I would like to put the cells corresponding to 2 in the foreground. Indeed the red border is visible only where the opacity parameter is setted (i.e. cells of type 0,2 not 3). In particular I would like to see the red rectangle without setting the opacity for the other cells. Here a similar question.

5
  • Your code produces errors. Please fix it. Oct 15, 2015 at 13:53
  • Seems the \foreach loop should be contained in a pair of {}.
    – Herr K.
    Oct 15, 2015 at 13:58
  • So do you want to have a red outline drawn around the 2 cell, or do want to fill it with red? Oct 15, 2015 at 14:02
  • @GonzaloMedina now it should work. Oct 15, 2015 at 14:03
  • @TomBombadil I'd like to have a red outline drawn around cell 2 Oct 15, 2015 at 14:04

1 Answer 1

3

An idea. Create layers named after your cell values, eg:

\pgfdeclarelayer{layer0}
\pgfdeclarelayer{layer1}
\pgfdeclarelayer{layer2}
\pgfdeclarelayer{layer3}

And set them in the desired order, i.e:

\pgfsetlayers{layer0,layer1,main,layer3,layer2}

Draw each cell in the layer layer\cell. This is the complete code which implements this idea (note that I added very thick to the style of cell 2):

\documentclass{standalone}
\usepackage{tikz}
\pgfdeclarelayer{layer0}
\pgfdeclarelayer{layer1}
\pgfdeclarelayer{layer2}
\pgfdeclarelayer{layer3}
\pgfsetlayers{layer0,layer1,main,layer3,layer2}

\begin{document}
\tikz[%
      every cell 0/.style={draw=black
                  , opacity=.5
                  ,  minimum height=0.1em
                  , outer sep=0pt},
      every cell 1/.style={draw=black
                  , fill=gray
                  , opacity=.5
                  ,  minimum height=0.1em
                  , outer sep=0pt},
      every cell 2/.style={draw=red, very thick,
                  , fill=gray
                  ,  minimum height=0.1em
                  , outer sep=0pt},
      every cell 3/.style={draw=black
                  , fill=gray
                  ,  minimum height=0.1em
                  %, width=0.1em,
                  %,  depth=0.1em
                  , outer sep=0pt}
] {
\foreach \row [count=\y] in {%
    {0,0,0,0,0,1,1,1,0,0,0},%
    {0,0,0,1,1,1,0,1,1,0,0},%
    {0,0,0,1,0,0,0,0,1,0,0},%
    {0,0,0,2,0,0,0,1,1,0,0},%
    {0,0,0,3,0,0,0,1,0,0,0},%
    {0,0,1,1,0,0,0,1,0,0,0},%
    {0,0,1,1,0,0,0,1,1,1,1},%
    {0,0,1,1,0,0,0,0,0,0,0},%
    {1,1,1,1,0,0,0,0,0,0,0},}%
    \foreach \cell [count=\x] in \row  {
      \begin{pgfonlayer}{layer\cell}
        \path [every cell, every cell \cell]
            (\x,-\y) rectangle ++(1,1);
      \end{pgfonlayer}
    }
}
\end{document}

And this is the result:

Result

1
  • This is exactly what I want produce. Oct 15, 2015 at 14:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .