使用 Forest 包时出现编译错误

使用 Forest 包时出现编译错误

我看了一段时间,但还是搞不清楚代码哪里出了问题。它在 Overleaf 中编译,但有错误。我先展示代码,然后再展示错误。

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

\newcommand{\both}{$p\underline{q}$}
\newcommand{\onlyP}{$p \underline{\neg q}$}
\newcommand{\onlyQ}{$\neg p\underline{q}$}
\newcommand{\neither}{$\neg p\underline{\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}

预期输出是:

在此处输入图片描述

我收到的错误是:

     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$}'.

答案1

您的主要问题已通过@Ulrike Fischer 的评论得到解决,但是一些评论(至少在我看来)可以使您的森林代码更简短,更清晰:

  • 您的图片比标准文章文本宽度更宽
  • 您真的需要命名树中的所有节点吗?
  • 边缘标签的定位可以自动定位(参见下面的 MWE)
  • 使用样式可以大大缩短代码
\documentclass[dvipsnames, margin=3mm]{standalone}% that whole image can be seen

\newcommand{\both}{$p\underline{q}$}
\newcommand{\onlyP}{$p \underline{\neg q}$}
\newcommand{\onlyQ}{$\neg p\underline{q}$}
\newcommand{\neither}{$\neg p\underline{\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},
        }
\newcommand\EC[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}

在此处输入图片描述

相关内容