
我正在嘗試使用森林包創建一棵樹,其中葉節點都位於同一層。
然而,連接較高節點的線會穿過其他節點。這個包有什麼辦法可以防止這種情況發生嗎?
這是相關的程式碼片段:
\begin{forest}
where n children=0{tier=word}{}
[equation, %for tree={parent anchor=south, child anchor=north}
[VARIABLE[y]]
[{=}]
[expression
[additionOrSubtraction
[multiplicationOrDivision
[atom[VARIABLE[x]]]
]
[{*}]
[atom
[{(}]
[expression
[additionOrSubtraction
[multiplicationOrDivision
[atom[INTEGER[2]]]
]
[{+}]
[multiplicationOrDivision
[atom[INTEGER[3]]]
[{*}]
[atom[VARIABLE[x]]]
]
]
]
[{)}]
]
]
]
]
\end{forest}
這是它的圖像:
答案1
我首先將路徑設定為使用正交邊緣(在edge path
森林手冊第 33 頁部分中進行了描述)。
s sep
接下來,我根據具體情況增加該數值(手冊第 32 頁中描述),從而擴大了孩子之間的距離。在某些級別上,10mm 就足夠了,但在其他級別上,20mm 是必要的。
我不確定您是否會用符號或圖標替換“multiplicationOrDivision”和類似的字串(我會推薦 - 或允許在這些字串中換行),所以我只是對距離進行了快速近似。當然,出版意味著它們仍然需要微調。
不幸的是,圖像尺寸變大了一些。也許 asidewaysfigure
或 a\resizebox
可以適合您需求的上下文?
代碼:
\documentclass{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}for tree={%
edge path={\noexpand\path[\forestoption{edge}] (!u.parent anchor) -- +(0,-\baselineskip) -| (.child anchor)\forestoption{edge label};}},
where n children=0{tier=word}{}
[equation, s sep=15mm %for tree={parent anchor=south, child anchor=north}
[VARIABLE[y]]
[{=}]
[expression
[additionOrSubtraction, s sep=20mm
[multiplicationOrDivision
[atom[VARIABLE[x]]]
]
[{*}]
[atom, s sep=20mm
[{(}]
[expression
[additionOrSubtraction, s sep=20mm
[multiplicationOrDivision
[atom[INTEGER[2]]]
]
[{+}]
[multiplicationOrDivision, s sep=10mm
[atom[INTEGER[3]]]
[{*}]
[atom[VARIABLE[x]]]
]
]
]
[{)}]
]
]
]
]
\end{forest}
\end{document}
答案2
如果方形邊緣可以接受,它們可能會成為最整齊的樹,正如建議的那樣。
但是,不需要手動配置它,因為目前的 Forestforked edges
在其edges
庫中提供了這種配置。
\usepackage[edges,linguistics]{forest}
在樹的序言中
forked edges,
我還建議fit=band
避免手動移動事物或透過反覆試驗找出正確的尺寸。
for tree={%
fit=band,
},
inner xsep=0pt
除最後一層之外的所有節點的設定也可能有所幫助。
where n children=0{%
tier=word,
}{%
inner xsep=0pt,
},
我還將它們分成...Or...
多行節點。我不知道這是否適合您的使用,但它確實使圖表更易於管理。
我建議設定幾個巨集:一個用於加法/減法,一個用於乘法/除法。這將允許您透過簡單地修改序言中的通用巨集定義來嘗試不同的選項。
例如:
\newcommand*\multordiv{Multiplication\\or\\Division}
\newcommand*\addorsub{Addition\\or\\Subtraction}
以及星號的一些東西-這些星號應該居中而不是在頂部嗎?您可能需要考慮對最後一層節點使用數學模式。
\newcommand*\asthere{\textasteriskcentered}
然後是樹規範
[equation
[VARIABLE[y]]
[{=}]
[expression
[\addorsub
[\multordiv
[atom
[VARIABLE
[x]
]
]
]
[\asthere]
[atom
[{(}]
[expression
[\addorsub
[\multordiv
[atom
[INTEGER
[2]
]
]
]
[{+}]
[\multordiv
[atom
[INTEGER
[3]
]
]
[\asthere]
[atom
[VARIABLE
[x]
]
]
]
]
]
[{)}]
]
]
]
]
產生的東西相當整潔,即使不是很緊湊,至少也更包含。
完整程式碼:
\documentclass[border=5pt,tikz,multi]{standalone}
\usepackage[edges,linguistics]{forest}
\newcommand*\multordiv{Multiplication\\or\\Division}
\newcommand*\addorsub{Addition\\or\\Subtraction}
\newcommand*\asthere{\textasteriskcentered}
\begin{document}
\begin{forest}
where n children=0{%
tier=word,
}{%
inner xsep=0pt,
},
for tree={%
fit=band,
},
forked edges,
[equation
[VARIABLE[y]]
[{=}]
[expression
[\addorsub
[\multordiv
[atom
[VARIABLE
[x]
]
]
]
[\asthere]
[atom
[{(}]
[expression
[\addorsub
[\multordiv
[atom
[INTEGER
[2]
]
]
]
[{+}]
[\multordiv
[atom
[INTEGER
[3]
]
]
[\asthere]
[atom
[VARIABLE
[x]
]
]
]
]
]
[{)}]
]
]
]
]
\end{forest}
\end{document}