TeX - LaTeX Asked on August 14, 2021
I am doing parse trees for propositional logic formulas and I want to have the branches of a tree start from the main connective. Notice that the branching starts at a specific character for each node. Here is an example of what I am looking for:
This was done using phantoms to demonstrate. Here is the code:
documentclass{article}
usepackage{forest}
forestset{
parse tree/.style={for tree={s sep=3em, minimum size=1.5em}}
}
begin{document}
begin{center}
begin{forest}
parse tree
[phantom{ssssssssssssssssss}$neg (neg P to (Q lor R))$
[phantom{sssss}$neg P to (Q lor R)$
[phantom{ss}$neg P$ [$P$]]
[$Q lor R$
[$Q$]
[$R$]
]
]
]
end{forest}
end{center}
end{document}
I’m hoping a solution could include the Forest package, but I am fine with anything that is easy enough to use!
I am not an expert on forest
, but what about smashing the size of part of the equations with mathtools
? Like:
documentclass{article}
usepackage{forest}
usepackage{mathtools}
forestset{
parse tree/.style={for tree={s sep=3em, minimum size=1.5em}}
}
begin{document}
begin{center}
begin{forest}
parse tree
[$negmathrlap{(neg P to (Q lor R))}$
[$mathllap{neg} to mathrlap{(Q lor R)}$
[$neg mathrlap{P}$ [$P$]]
[$Q lor R$
[$Q$]
[$R$]
]
]
]
end{forest}
end{center}
end{document}
...the only problem could be the bounding box of the final box, which can (will) be wrong. If this is a feasible solution, you can use a macro:
newcommand{balancemath}[3]{mathllap{#1} #2 mathrlap{#3}}
begin{document}
begin{center}
begin{forest}
parse tree
[$balancemath{}{neg}{(neg P to (Q lor R))}$
[$balancemath{neg}{to}{(Q lor R)}$
[$balancemath{}{neg}{P}$ [$P$]]
[$Q lor R$
[$Q$]
[$R$]
]
]
]
end{forest}
end{center}
...to obtain the same result. If needed, the macro can be changed (not trivially) to have a phantom bounding box so that it encompasses the full formula.
Correct answer by Rmano on August 14, 2021
I'm adding this answer even if it does not address OP's main issue (starting the branch below the connective), because (i) it provides a solution for the same problem (logic parse trees) and (ii) I had it already developed for myself.
documentclass{article}
usepackage{forest}
forestset{
declare toks={left}{},
declare toks={right}{},
declare boolean={paren}{false},
formula as tree/.style={
formula@as@tree,
for tree={
content format={ensuremath{%
noexpandcolor{lightgray}forestoption{left}%
noexpandcolor{black}forestoption{content}%
noexpandcolor{lightgray}forestoption{right}%
}},
},
},
formula@as@tree/.style={
% assuming unary or binary branching nodes
delay={
for descendants children-first={
if n=1{
if={n_children("!u")==1}{
append content to parent,
}{
prepend content to parent,
}
}{
append content to parent,
},
},
},
},
prepend content to parent/.style={
if paren={
!u.+left/.process={OOOw3}{left}{content}{right}{(##1##2##3)},
}{
!u.+left/.process={OOOw3}{left}{content}{right}{##1##2##3},
},
},
append content to parent/.style={
if paren={
!u.right+/.process={OOOw3}{left}{content}{right}{(##1##2##3)},
}{
!u.right+/.process={OOOw3}{left}{content}{right}{##1##2##3},
}
},
}
begin{document}
begin{forest} formula as tree
[neg
[rightarrow,paren
[neg
[P]
]
[vee,paren
[Q]
[R]
]
]
]
end{forest}
end{document}
Answered by Sašo Živanović on August 14, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP