# Rowcolor in tabular vs Forest node, unwanted behavior

I have problems when using tabular and forest in the same document. The row colors is also active on nodes in Forest trees. See MWE:

\documentclass{standalone}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{tikz}
\rowcolors{2}{blue}{blue!20}
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={box, rounded corners = 2pt}
[Line one\\Line two\\Line three, fill=green, align=left, base=bottom
[Line one\\Line two, fill=blue, text=white, align=left, base=bottom
[Line one, fill=gray, align=left, base=bottom]
]
]
\end{forest}

\begin{tabular}{|l|c|r|}
\hline
foo   & bar    & fubar \\
fubar & toobar & foo \\
\hline
\end{tabular}

\end{document}


Output:

• Yes, forest also uses tabular internally. Move the rowcolors behind the tree, and/or use grouping. – Ulrike Fischer Sep 18 '17 at 14:08
• For multiline nodes, Forest uses tabular for formatting. Either you can limit the row colour definitions to the tabulars where you need them or you can switch them off for the trees where you don't. – cfr Sep 18 '17 at 21:38
• Your code cannot be compiled. Please fix it. – cfr Sep 18 '17 at 21:39

I modified your example in order to

1. make it compile;
2. create an example which would make the effect of row colouring in the tree rather more obvious - if you fill the nodes with colours, that really makes it hard for other people to see the problem at a glance.

The example below includes trees with and without fills. I didn't reinstate you undefined key/style/whatever as I have no idea what it is meant to be.

Forest does not always use tabular internally but, as Ulrike says, it is using it internally in your code. This is because you are using the align key. Hence, anything which affects tabular environments will affect the nodes of your trees which use this option.

You can either switch colouring on when you want it (in, presumably, all tabulars) or switch colouring off when you don't (in, presumably, all forests).

The first option is straightforward, so I demonstrate the second. In this case, we use a default preamble to overwrite the effect of align, after the tree is parsed and before it is typeset. We switch colouring off at the start of each affected node and on at the end.

\documentclass[border=10pt]{standalone}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{forest}
\rowcolors{2}{blue}{blue!20}
\makeatletter
\forestset{
default preamble={
before typesetting nodes={
where={>Ot={align}{}}{}{
content format={
\noexpand\begin{tabular}[\forestoption{base}]{\forestoption{align}}%
\noexpand\hiderowcolors
\forestoption{content}%
\noexpand\global\noexpand\@rowcolorstrue
\noexpand\end{tabular}%
},
},
},
},
}
\makeatother
\begin{document}
\begin{forest}
for tree={rounded corners = 2pt}
[Line one\\Line two\\Line three, align=left, base=bottom
[Line one\\Line two, align=left, base=bottom
[Line one, align=left, base=bottom]
]
]
\end{forest}

\begin{forest}
for tree={rounded corners = 2pt}
[Line one\\Line two\\Line three, fill=green, align=left, base=bottom
[Line one\\Line two, fill=blue, text=white, align=left, base=bottom
[Line one, fill=gray, align=left, base=bottom]
]
]
\end{forest}

\begin{tabular}{|l|c|r|}
\hline
foo   & bar    & fubar \\
fubar & toobar & foo \\
\hline
\end{tabular}

\begin{forest}
for tree={rounded corners = 2pt}
[Line one\\Line two\\Line three, align=left, base=bottom
[Line one\\Line two, align=left, base=bottom
[Line one, align=left, base=bottom]
]
]
\end{forest}

\begin{forest}
for tree={rounded corners = 2pt}
[Line one\\Line two\\Line three, fill=green, align=left, base=bottom
[Line one\\Line two, fill=blue, text=white, align=left, base=bottom
[Line one, fill=gray, align=left, base=bottom]
]
]
\end{forest}

\end{document}


• I do hope the OP is not planning on inflicting this colour scheme on a real audience. – cfr Sep 18 '17 at 22:23
• Sure NOT! :) Just for showing what I mean. – Jimmy Westberg Sep 19 '17 at 6:21
• Thanks for the help. This fixed my issue. And since the Forest package is using tabular internally I understand that my example was not an error but a known effect of that. – Jimmy Westberg Sep 19 '17 at 6:25
• @JimmyWestberg :-) It is useful to know, actually, because you can do something like align=lp{25mm}r and use & to separate columns within the node. It is set up so when you write align=left, it is treated as align=l. Anyway, glad it helped. – cfr Sep 19 '17 at 13:26
• This gives me this error though: Unknown operator O' or Ot' (in '>Ot={align}{}' – Jimmy Westberg Sep 22 '17 at 9:44

A simple group together with an empty row color fixed my problem. Thanks for the help though!

\documentclass[border=10pt]{standalone}

\usepackage[dvipsnames,table]{xcolor}
\usepackage{forest}
\rowcolors{2}{blue}{blue!20}
\begin{document}

\begin{forest}
for tree={rounded corners = 2pt}
[Line one\\Line two\\Line three, align=left, base=bottom
[Line one\\Line two, align=left, base=bottom
[Line one, align=left, base=bottom]
]
]
\end{forest}
\begingroup
\rowcolors{2}{}{}
\begin{forest}
for tree={rounded corners = 2pt}
[Line one\\Line two\\Line three, fill=green,align=left, base=bottom
[Line one\\Line two, fill=blue, text=white, align=left, base=bottom
[Line one, fill=gray, align=left, base=bottom]
]
]
\end{forest}
\endgroup

\begin{tabular}{|l|c|r|}
\hline
foo   & bar    & fubar \\
fubar & toobar & foo \\
\hline
\end{tabular}

\begin{forest}
for tree={rounded corners = 2pt}
[Line one\\Line two\\Line three, align=left, base=bottom
[Line one\\Line two, align=left, base=bottom
[Line one, align=left, base=bottom]
]
]
\end{forest}

\begingroup
\rowcolors{2}{}{}
\begin{forest}
for tree={rounded corners = 2pt}
[Line one\\Line two\\Line three, fill=green, align=left, base=bottom
[Line one\\Line two, fill=blue, text=white, align=left, base=bottom
[Line one, fill=gray, align=left, base=bottom]
]
]
\end{forest}
\endgroup

\end{document}