# TikZ, forest and star macro

I would like to define a star version of a macro to be used for the value of edge label. The second code does the wanted the job but its starred translation fails to work. Is there a subtil impossibility to work with starable macros or not ?

## Failing code

\documentclass[12pt,a4paper]{article}

\usepackage{forest}

\makeatletter

\newcommand\probaweight{\@ifstar{\@probaweight@star}{\@probaweight@no@star}}
\newcommand\@probaweight@no@star[1]{node[midway, fill=white]{#1}}
\newcommand\@probaweight@star[1]{}

\makeatother

\begin{document}

\begin{forest}
[
[$A$, edge label=\probaweight{$a$}
[$B$, edge label=\probaweight{$b$}]
[$C$, edge label=\probaweight{$c$}]
]
[$D$, edge label=\probaweight*{$d$}
[$E$, edge label=\probaweight*{$e$}]
[$F$, edge label=\probaweight*{$f$}]
]
]
\end{forest}

\end{document}


## Working code

\documentclass[12pt,a4paper]{article}

\usepackage{forest}

\newcommand\probaweight[1]{node[midway, fill=white]{#1}}
\newcommand\probanoweight[1]{}

\begin{document}

\begin{forest}
[
[$A$, edge label=\probaweight{$a$}
[$B$, edge label=\probaweight{$b$}]
[$C$, edge label=\probaweight{$c$}]
]
[$D$, edge label=\probanoweight{$d$}
[$E$, edge label=\probanoweight{$e$}]
[$F$, edge label=\probanoweight{$f$}]
]
]
\end{forest}

\end{document}

• I'd be wary of using * as it is one of the characters Forest is looking for. I'd either look to jump out of the package's parsing (and use the star) or use some other approach. Obviously you can make it work, as the answers show, but I think you're adding fragility needlessly. Though you could ask Sašo. Even if it is safe, though, it is deeply confusing - especially using a * in a style name, because this has a standardised meaning in Forest already. – cfr Oct 10 '19 at 23:32
• Thanks a lot for this clarification. – projetmbc Oct 11 '19 at 19:13

I cannot answer the question about the usage starred macros in forest keys, but it is generally not recommended to use macros that expand to pgf keys. Rather, this is what styles are for. And with styles there is no problem, and the code becomes even shorter.

\documentclass[12pt,a4paper]{article}
\usepackage{forest}
\forestset{el/.style={edge label={node[midway, fill=white]{#1}}},
el*/.style={}}
\begin{document}

\begin{forest}
[
[$A$, el={$a$}
[$B$, el={$b$}]
[$C$, el={$c$}]
]
[$D$, el*={$d$}
[$E$, el*={$e$}]
[$F$, el*={$f$}]
]
]
\end{forest}

\end{document}


ADDENDUM: As mentioned by @cfr, it may be safer not to use a star. So maybe

\documentclass[12pt,a4paper]{article}
\usepackage{forest}
\forestset{el/.style={edge label={node[midway, fill=white]{#1}}},
el!/.style={}}
\begin{document}

\begin{forest}
[
[$A$, el={$a$}
[$B$, el={$b$}]
[$C$, el={$c$}]
]
[$D$, el!={$d$}
[$E$, el!={$e$}]
[$F$, el!={$f$}]
]
]
\end{forest}

\end{document}


is a safer choice. Who knows.

• Great ! :-) The use of the macro was just the consequence of my lake of knowledge of the TiKz machinery. – projetmbc Oct 10 '19 at 20:12
• @projetmbc Strictly speaking it is the pgf machinery, and it gets used by TikZ, pgfplots, forest etc. Internally these keys are macros, quite possible that a * causes problems, but I really do not know. – Schrödinger's cat Oct 10 '19 at 20:17
• It is at least confusing since, as you know, <key>* already has a standardised meaning in Forest for (relevant) standard keys. I know these are .styles, but .... I don't know. It doesn't feel good to me, even if it is safe (which I don't know either). There's no per se problem with using * in the name of a key, as Forest does it. But I don't know when it is being passed to TikZ .... – cfr Oct 10 '19 at 23:36
• @cfr Thanks for the hint! Yes, sure, using something other than a star may be a safer choice. I was not sure if the OP wanted literally a star or the start was just chosen because there is the \@ifstar thingy. (AFAIK the keys can contain stars, but I may be wrong. They certainly can contain minuses.) I'll add a safer version. Thanks! – Schrödinger's cat Oct 11 '19 at 0:41
• @Schrödinger'scat Sure. I think they are safe, but Forest reserves stars in style names for a quite particular purpose (as it does minuses, pluses etc.). Certainly they are safe qua TikZ keys and probably, as you say, for Forest. But it seems confusing. I expect <style>* to be a special form of <style> in Forest .... – cfr Oct 11 '19 at 20:02

I'd go with a style, as suggested in the other answer.

The problem seems to lie in expandability, as the following working code shows.

\documentclass[12pt,a4paper]{article}

\usepackage{forest,xparse}

\NewExpandableDocumentCommand{\probaweight}{sm}{%
\IfBooleanTF{#1}%
{}%
{node[midway, fill=white]{#2}}%
}

\begin{document}

\begin{forest}
[
[$A$, edge label=\probaweight{$a$}
[$B$, edge label=\probaweight{$b$}]
[$C$, edge label=\probaweight{$c$}]
]
[$D$, edge label=\probaweight*{$d$}
[$E$, edge label=\probaweight*{$e$}]
[$F$, edge label=\probaweight*{$f$}]
]
]
\end{forest}

\end{document}