4

At the moment I'm using this command in the preamble to fill the color of the specified row with grey:

 row 2/.style={
            nodes={fill=gray!10}
        }

Unfortunately I have to write this for every line. Can't I say something like

 row 1-10/.style={
            nodes={fill=gray!10}
        }

Copy from answer:

Thanks for your answers. Though I'm not sure what's not clear in my example the problem is that I wanted to use the row option (within \tikzset{} before \begin{document} ) because (maybe I don't know how else to do it) I only want certain rows to be colored that way not everything. Other rows have a different color such that everything looks nice and colorful as it should be. Since I'm very new to tikz I solved this in an awkward way by writing

\tixset{
  row 2/.style={
            nodes={fill=gray!10}
        },
                        row 3/.style={
            nodes={fill=gray!10}
        },
                        row 4/.style={
            nodes={fill=gray!10}
        },
        column 1/.style={
            nodes={text width=14em}
        },
                        column 3/.style={
            nodes={text width=9em}
        },
                            column 4/.style={
            nodes={text width=9em}
        }
}

Here I also specified different widths for the columns. Probably as well this one can solve better by not writing the entire ./style every time... @last: myrowstyle is just a name or? so my new definition to later call it am I right?

MWE [copied from deleted answer by cfr]

\documentclass[table]{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    table/.style={
        matrix of nodes,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={
            rectangle,
            draw=black,
            align=center,
                   },
                        %baseline={([yshift=-0.5ex]current bounding box.center)},
        minimum height=1.5em,
        text depth=0.5em,
        text height=1em,
                text centered,
        nodes in empty cells,
%%
                                row 1/.style={
            nodes={
                fill=black,
                text=white,
                %font=\bfseries
            }
        },
                myrowstyle/.style={
                    row #1/.style={nodes={fill=gray!10}}
        },
             column 1/.style={
            nodes={text width=14em}
        },
                        column 3/.style={
            nodes={text width=9em}
        },
                            column 4/.style={
            nodes={text width=9em}
        }
    }
}

\begin{document}

\begin{frame}
    \begin{tikzpicture}
    \matrix (first) [table,text width=4em, myrowstyle/.list={1,2,3}, row 2/.style={text depth=1.5em}, ampersand replacement=\&]
{ 
        ... \& \# ...\& \# ... \& ... \\
     01.06.-02.06. \break (hello world) \& 1 \& 2 \& \\
     02.06.-03.06. (hello) \& 3 \& 3 \& \\
};
\end{tikzpicture}
\end{frame}

\end{document}
3
  • 1
    It's not very clear what you're asking. Other users will be able to answer your question better if you provide (a) more context and (b) a minimal working example that begins with \documentclass{...} and ends with \end{document}. – Andrew Uzzell Jun 1 '16 at 16:57
  • Why use the row styles at all, can't you just add nodes={fill=gray!10} to the matrix options directly? – Torbjørn T. Jun 1 '16 at 17:16
  • 2
    See the rows and columns keys in my answer. You can use rows={1,3,8,...,12}{fill=gray!10} or—taken from the linked answer—columns={6,...,8}{align=right}, columns={5}{align=center} – Qrrbrbirlbel Jun 1 '16 at 18:45
8

You can use the /.list handler.

\tikzset{myrowstyle/.style = {row #1/.style={nodes={fill=gray!10}}}

and later in the picture you can then use

myrowstyle/.list={1,...,10}

or any other argument list.

3

This answer demonstrates how to modify your MWE to use the suggestions provided in comments and other answers.

The reason that Qrrbrbirlbel's suggestion seemed not to work is probably that you didn't include the code to which the comment directed you. It is difficult to say for sure because your reported failure concerned a code fragment and not a complete example, which always makes it difficult to diagnose (let alone solve) problems.

Basically, you need to actually include the relevant part of Qrrbrbirlbel's code if it is to do you any good. You can't expect TikZ to find it by looking up Qrrbrbirlbel's answer!

\tikzset{
  rows/.style 2 args={%
    /utils/temp/.style={%
      row ##1/.append style={nodes={#2}},
    },
    /utils/temp/.list={#1},
  },
  columns/.style 2 args={%
    /utils/temp/.style={%
      column ##1/.append style={nodes={#2}},
    },
    /utils/temp/.list={#1},
  },
}

The reason that percusse's answer appears not to work is that your question provided insufficient information about the context in which you wanted to use the code. As a result, it is necessary to modify the suggested .style definition to take account of the context in which you wish to use the .list by doubling the hashes in the usual way to ensure they refer to the correct argument.

    my row style/.style={
      row ##1/.append style={nodes={fill=gray!10}}
    },

This can also be modified to deal with a list of {<row>}{<colour>} pairs, if desired.

    my other row style/.style n args=2{
      row ##1/.append style={nodes={fill=##2}}
    },

This is why it is so important to provide a complete minimal working example when you ask a question.

The following modification of your MWE demonstrates all 3 of the above approaches.

\documentclass[table]{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{% from Qrrbrbirlbel's answer at https://tex.stackexchange.com/a/137581/
  rows/.style 2 args={%
    /utils/temp/.style={%
      row ##1/.append style={nodes={#2}},
    },
    /utils/temp/.list={#1},
  },
  columns/.style 2 args={%
    /utils/temp/.style={%
      column ##1/.append style={nodes={#2}},
    },
    /utils/temp/.list={#1},
  },
}
\tikzset{
  table/.style={
    matrix of nodes,
    row sep=-\pgflinewidth,
    column sep=-\pgflinewidth,
    nodes={
      rectangle,
      draw=black,
      align=center,
    },
    minimum height=1.5em,
    text depth=0.5em,
    text height=1em,
    text centered,
    nodes in empty cells,
    row 1/.style={
      nodes={
        fill=black,
        text=white,
      }
    },
    my row style/.style={% from percusse's answer at https://tex.stackexchange.com/a/312595/ but we need to double the hashes in this case in order to use it as the OP wants below
      row ##1/.append style={nodes={fill=gray!10}}
    },
    my other row style/.style n args=2{
      row ##1/.append style={nodes={fill=##2}}
    },
    column 1/.append style={
      nodes={text width=8em}
    },
    column 3/.append style={
      nodes={text width=5em}
    },
    column 4/.append style={
      nodes={text width=5em}
    }
  }
}

\begin{document}
\begin{frame}
  \begin{tikzpicture}
    \matrix (first) [table, ampersand replacement=\&, text width=4em, my row style/.list={2}, my other row style/.list={{3}{cyan}}, rows={2,3}{text depth=1.5em}, ]
    {
      ... \& \# ...\& \# ... \& ... \\
      01.06.-02.06. \break (hello world) \& 1 \& 2 \& \\
      02.06.-03.06. (hello) \& 3 \& 3 \& \\
    };
  \end{tikzpicture}
\end{frame}
\end{document}

coloured rows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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