![表格与森林节点中的行颜色,不需要的行为](https://linux22.com/image/362124/%E8%A1%A8%E6%A0%BC%E4%B8%8E%E6%A3%AE%E6%9E%97%E8%8A%82%E7%82%B9%E4%B8%AD%E7%9A%84%E8%A1%8C%E9%A2%9C%E8%89%B2%EF%BC%8C%E4%B8%8D%E9%9C%80%E8%A6%81%E7%9A%84%E8%A1%8C%E4%B8%BA.png)
在同一文档中使用表格和森林时,我遇到了问题。行颜色在森林树中的节点上也处于活动状态。请参阅 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}
答案1
我修改了你的例子,以便
- 使其编译;
- 创建一个示例,使树中的行着色效果更加明显 - 如果您用颜色填充节点,那么其他人很难一眼看出问题。
下面的示例包括有填充和无填充的树。我没有恢复你未定义的键/样式/任何东西,因为我不知道它是什么意思。
Forest 并不总是tabular
在内部使用,但正如 Ulrike 所说,它在您的代码中内部使用它。这是因为您正在使用密钥align
。因此,影响tabular
环境的任何事情都会影响使用此选项的树的节点。
当您需要颜色时,您可以打开颜色(大概是所有tabular
颜色),或者当您不需要颜色时,可以关闭颜色(大概是所有forest
颜色)。
第一个选项很简单,所以我演示了第二个选项。在本例中,在解析树之后和排版之前,我们使用default preamble
来覆盖 的效果。我们在每个受影响节点的开头关闭颜色,在结尾打开颜色。align
\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}
答案2
一个简单的组和一个空行颜色解决了我的问题。不过还是谢谢你的帮助!
\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}