TransWikia.com

Compilation error using Forest package

TeX - LaTeX Asked by forest_trouble on May 24, 2021

I’ve been looking at this for a while, but can’t figure out what’s wrong with the code. It compiles in Overleaf, but with errors. I’ll show the code first, and then the errors.

documentclass{article}
usepackage[utf8]{inputenc}
usepackage[dvipsnames]{xcolor}
definecolor{mygreen}{rgb}{0,0.4,0}

newcommand{both}{$punderline{q}$}
newcommand{onlyP}{$p underline{neg q}$}
newcommand{onlyQ}{$neg punderline{q}$}
newcommand{neither}{$neg punderline{neg q}$}

usepackage{forest}
forestset{
sn edges/.style={for tree={circle, draw, thick, minimum size=1cm,inner sep=1,edge={->,thick}}}
}

title{forest_test.tex}

begin{document}
begin{forest}
sn edges
[, phantom, s sep = 0.6cm
    [both, mygreen, name=level0
        [both, mygreen, edge=mygreen, name=level10, edge label = {node [midway,left] {$a$} }
            [onlyP, red, edge=red, edge label = {node [midway,left] {$a$} }, name=level20]
            [neither, red, edge=red, edge label = {node [midway, right] {$a$} }, name=level21] 
        ]
        [onlyQ, mygreen, edge=mygreen, edge label = {node [midway,right] {$a$}}, name=level11
            [onlyP, mygreen, edge=mygreen, edge label = {node [midway,left] {$a$}},name=level22  [both, mygreen, edge=mygreen,name=h1, edge label = {node [midway,left] {$a$}} [both, red, edge=red, edge label = {node [midway,left] {$a$}}] [onlyQ, mygreen, edge=mygreen, edge label = {node [midway,right] {$a$}},name=h1new]] [onlyQ, mygreen, edge=mygreen, name=h2, edge label = {node [midway,right] {$a$}} [both, red, edge=red, edge label = {node [midway,left] {$a$}}] [onlyQ, red, edge=red, edge label = {node [midway,right] {$a$}}] ]] 
            [neither, mygreen, edge=mygreen , edge label = {node [midway,right] {$a$}}, name=level23 [both, mygreen, edge=mygreen,name=h3new1, edge label = {node [midway,left] {$a$}} [both, red, edge=red, edge label = {node [midway,left] {$a$}}] [onlyQ, mygreen, edge=mygreen, edge label = {node [midway,right] {$a$}}, name=h2new] ] [onlyQ, mygreen, edge=mygreen,name=h4, edge label = {node [midway,right] {$a$}} [both, red, edge=red, edge label = {node [midway,left] {$a$}}] [onlyQ, red, edge=red, edge label = {node [midway,right] {$a$}}]]] 
        ]   
    ]
    
    [onlyQ, mygreen, name=level00
        [both, mygreen, edge=mygreen,edge label = {node [midway,left] {$a$}}, name=level12
            [onlyP, mygreen, edge=mygreen,edge label = {node [midway,left] {$a$}}, name=level24 [both, mygreen, edge=mygreen,name=h5,edge label = {node [midway,left] {$a$}} [both, red, edge=red, edge label = {node [midway,left] {$a$}}] [onlyQ, red, edge=red, edge label = {node [midway,right] {$a$}}]] [onlyQ, mygreen, edge=mygreen,name=h6,edge label = {node [midway,right] {$a$}} [both, mygreen, edge=mygreen, edge label = {node [midway,left] {$a$}},name=h3new] [onlyQ, red, edge=red, edge label = {node [midway,right] {$a$}}] ]] 
            [neither, name=level25, mygreen, edge=mygreen,edge label = {node [midway,right] {$a$}} [both, mygreen, edge=mygreen,name=h7,edge label = {node [midway,left] {$a$}} [both, red, edge=red, edge label = {node [midway,left] {$a$}}] [onlyQ, red, edge=red, edge label = {node [midway,right] {$a$}}]] [onlyQ, mygreen, edge=mygreen,name=h8,edge label = {node [midway,right] {$a$}} [both, mygreen, edge=mygreen, name=h4new, edge label = {node [midway,left] {$a$}}] [onlyQ, red, edge=red, edge label = {node [midway,right] {$a$}}]]] 
        ]
        [onlyQ, mygreen, edge=mygreen,edge label = {node [midway,right] {$a$}}, name=level13
            [onlyP,name=level27, red, edge=red,edge label = {node [midway,left] {$a$}}] 
            [neither,name=level28, red, edge=red ,edge label = {node [midway,right] {$a$}}] 
        ]
    ]
]
end{forest}

end{document}

The intended output is:

enter image description here

The errors I get are:

     main.tex, line 44
Runaway argument?
{,afterthought={ 
! Paragraph ended before forestOappto was complete.
<to be read again> 
                   par 
l.44 end{forest}
                 
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.
 main.tex, line 44
Extra }, or forgotten endgroup.
<argument> ...iven options}{,afterthought={ par }
                                                  }
l.44 end{forest}
                 
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.
 main.tex, line 44
Extra }, or forgotten endgroup.
<argument> ...ven options}{,afterthought={ par }}
                                                  
l.44 end{forest}
                 
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

One Answer

Your main problem is already solved by @Ulrike Fischer comment, however some remarks, which (to my opinion at least) can make your forest code shorter and more clear:

  • your image is wider than standard article text width
  • do you really need named all nodes in tree?
  • positioning of edge labels can be automatically positioned (see MWE below)
  • use of styles you can make your code significantly shorter
documentclass[dvipsnames, margin=3mm]{standalone}% that whole image can be seen

newcommand{both}{$punderline{q}$}
newcommand{onlyP}{$p underline{neg q}$}
newcommand{onlyQ}{$neg punderline{q}$}
newcommand{neither}{$neg punderline{neg q}$}

usepackage{forest}
usetikzlibrary{arrows.meta}
definecolor{mygreen}{rgb}{0,0.4,0}
tikzset{
ELS/.style = {% Edge Label Style
              pos=0.4, node font=footnotesize, 
              inner sep=2pt, anchor=#1},
        }
newcommandEC[1]{#1, edge=#1}


begin{document}
    begin{forest}
for tree = {% nodes
            circle, draw, semithick,
            minimum size=10mm, inner sep=0pt,
            % tree
            edge={-Straight Barb, semithick},
            s sep = 2mm,
            },% end for tree
EL/.style={% Edge Label, automatic positioning
           if n=1{edge label={node[ELS=east]{$#1$}}}
                 {edge label={node[ELS=west]{$#1$}}}
          },
EC/.style={color=#1, edge=#1}% coloring nodes and edges
[, phantom, 
% left tree
    [both, EC=mygreen
        [both, EC=mygreen, EL=a
            [onlyP, EC=mygreen,  EL=a
                [neither, EC=red, EL=a]
            ]
        ]
        [onlyQ, EC=mygreen, EL=a
            [onlyP, EC=mygreen, EL=a
                [both,  EC=mygreen, EL=a
                    [both,  EC=red,     EL=a]
                    [onlyQ, EC=mygreen, EL=a]
                ] 
                [onlyQ, EC=mygreen, EL=a
                    [both, EC=red,  EL=a] 
                    [onlyQ, EC=red, EL=a] 
                ]
            ]
            [neither, EC=mygreen, EL=a
                [both, EC=mygreen,EL=a
                    [both, EC=red, EL=a] 
                    [onlyQ, EC=mygreen, EL=a] 
                ]
                [onlyQ, EC=mygreen, EL=a
                    [both, EC=red,  EL=a]
                    [onlyQ, EC=red, EL=a]
                ]
            ]
        ]
    ]
% right tree
    [onlyQ, EC=mygreen, 
        [both, EC=mygreen, EL=a
            [onlyP, EC=mygreen, EL=a
                [both, EC=mygreen,  EL=a
                    [both, EC=red,  EL=a] 
                    [onlyQ, EC=red, EL=a]
                ] 
                [onlyQ, EC=mygreen, EL=a
                    [both, EC=mygreen, EL=a] 
                    [onlyQ, EC=red,    EL=a] 
                ]
            ]
            [neither, EC=mygreen,EL=a
                [both, EC=mygreen, EL=a
                    [both, EC=red, EL=a] 
                    [onlyQ, EC=red, EL=a]
                ] 
                [onlyQ, EC=mygreen,EL=a
                    [both, EC=mygreen, EL=a] 
                    [onlyQ, EC=red,    EL=a]
                ]
            ]
        ]
        [onlyQ, EC=mygreen,  EL=a
            [onlyP,EC=red,   EL=a]
            [neither,EC=red, EL=a]
        ]
    ]
]
    end{forest}
end{document}

enter image description here

Answered by Zarko on May 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP