更改樹的節點大小

更改樹的節點大小

我有這棵樹:

在此輸入影像描述

這是用以下程式碼建立的:

\begin{figure}[h]
        \centering
        \begin{tikzpicture}
            \node[circle,draw]{}
            child{
                node[circle,draw]{a}
            }
            child[missing]{};
        \end{tikzpicture}
    \end{figure}

如何增加根圓的大小,以便即使在空的情況下,它看起來也像底部圓?或者有更好的方法來創建這些樹嗎?

答案1

使用該forest包:

\documentclass[margin=3mm]{standalone}
\usepackage{forest}

\begin{document}
    \begin{forest}
for tree = {circle,
            draw,
            minimum size=1.1em,
            inner sep=2pt,
            font=\small,
    l sep=9mm,
    s sep=6mm
            }
[
    [a]
    [, coordinate, no edge]
]   
    \end{forest}
\end{document}

在此輸入影像描述

答案2

您可以簡單地將樹節點的大小定義為最小值。由於大多數節點很可能也會被繪製為圓形,並且您可能有不止一棵類似這樣的樹,因此創建一個包含所有這些參數的用於重複使用的全域樣式是有意義的。

如果您要繪製很多樹,我強烈建議切換到forest.基本的 TikZ 樹繪製工具確實不太好用:它們的語法非常笨拙,並且不會進行自動節點打包。

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\tikzset{my tree/.style={every node/.style={draw,circle,minimum size=1.5em}}}\begin{document}
        \begin{tikzpicture}[my tree]
            \node{}
            child{
                node{a}
            }
            child[missing]{};
        \end{tikzpicture}
\end{document}

在此輸入影像描述

答案3

歡迎來到 TeX.SE 社區。我將使用一個快速解決方案,將其放入\node[circle,draw]{...}幻像字元中:\phantom{\small a}

在此輸入影像描述

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usepackage{graphicx}

\begin{document}
\begin{figure}[h]
        \centering
        \begin{tikzpicture}
            \node[circle,draw]{\phantom{\small a}}
            child{
                node[circle,draw]{a}
            }
            child[missing]{};
        \end{tikzpicture}
    \end{figure}
\end{document}

相關內容