I am trying to use foreach
to simplify the 1st env. code in my MN(ot)WE below.
There is an expansion problem regarding the optional argument in the 2nd code. Here is the output.
How can I fix this ?
\documentclass[12pt]{article}
\RequirePackage{forest}
\usetikzlibrary{calc}
\useforestlibrary{linguistics}
\newcommand\ptreeComment[3][black]{
\node [anchor=mid west] at (#2.mid -| ptreecomment coord) {%
\textcolor{#1}{#3}%
};
}
\begin{document}
\begin{forest}
for tree = {%
sn edges,
grow' = 0,
l = 2.5cm,
s sep = 1.2cm,
anchor = parent,
},
tikz+={
\coordinate (ptreecomment coord) at (current bounding box.east);
},
[
[$A$
[$B$, name = nB]
[$C$, name = nC]
]
[$D$
[$E$, name = nE]
[$F$, name = nF]
]
]
%
\ptreeComment{nB}{$X = 1$}
\ptreeComment[red]{nC}{$X = 3$}
\ptreeComment[orange]{nE}{$X = 3$}
\ptreeComment[black!60!green]{nF}{$X = 5$}
\end{forest}
\begin{forest}
for tree = {%
sn edges,
grow' = 0,
l = 2.5cm,
s sep = 1.2cm,
anchor = parent,
},
tikz+={
\coordinate (ptreecomment coord) at (current bounding box.east);
},
[
[$A$
[$B$, name = nB]
[$C$, name = nC]
]
[$D$
[$E$, name = nE]
[$F$, name = nF]
]
]
%
\foreach \color/\name/\text in {black/nB/$X = 1$,
red/nC/$X = 3$,
orange/nE/$X = 3$,
black!60!green/nF/$X = 5$}
{
\ptreeComment[\color]{\name}{\text}
}
\end{forest}
\end{document}
On the other hand, the following code works without problem.
\documentclass[12pt]{article}
\RequirePackage{tikz}
\newcommand\test[2][black]{
Option: #1 et Arg: #2\par
}
\newcommand\colorize[2][black]{
\textcolor{#1}{#2}\par
}
\begin{document}
\foreach \opt/\arg in {black/nB,red/nC,orange/nE,green/nF}{
\test[\opt]{\arg}
}
\bigskip
\foreach \opt/\arg in {black/nB,red/nC,orange/nE,green/nF}{
\colorize[\opt]{\arg}
}
\end{document}
! File ended while scanning use of \forest.
Anyway, using\color
is obviously a bad choice, since\textcolor
is defined in terms of\color
. – egreg Aug 9 '20 at 13:43