私は無限ルートの三分木の図を作成しようとしていますが、次のコードでうまくいきます。
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\pgfdeclarelindenmayersystem{3-ary rooted tree}{
\rule{L -> L [s+ [-L] [M] [+R]]}
\rule{M -> M [s [-L] [M] [+R]]}
\rule{R -> R [s- [-L] [M] [+R]]}
\symbol{L}{\pgflsystemdrawforward}
\symbol{M}{\pgflsystemdrawforward}
\symbol{R}{\pgflsystemdrawforward}
\symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
}
\draw[rotate = -90] l-system [l-system={3-ary rooted tree,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];
\end{tikzpicture}
\end{figure}
\end{document}
しかし、各文字に対応するエッジを異なる色で塗り分けたい。例えば、Lは赤、Mは緑、Gは青。試してみたところ、
\symbol{L}{\pgflsystemdrawforward}[red]
\symbol{M}{\pgflsystemdrawforward}[green]
\symbol{R}{\pgflsystemdrawforward}[blue]
しかし、何も起こらないようです。何か提案はありますか?
答え1
いろいろ考えた結果、R、M、L の各部分をそれぞれ異なる色で描くのが一番簡単な方法だと思います。そこで、、 、 の各部分3-ary rooted tree
に分割し、それぞれ 3 つの「部分」のうち 1 つだけを描き、他の 2 つにはの代わりにを使用します。すると、 3 つの異なる色で3 つの異なるコマンドを使用できます。
R
M
L
\pgflsystemmoveforward
\pgflsystemdrawforward
\draw
:)
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\pgfdeclarelindenmayersystem{3-ary rooted tree-L}{ %define the left branch
\rule{L -> L [s+ [-L] [M] [+R]]}
\rule{M -> M [s [-L] [M] [+R]]}
\rule{R -> R [s- [-L] [M] [+R]]}
\symbol{L}{\pgflsystemdrawforward} % draw the L branch
\symbol{M}{\pgflsystemmoveforward} % only move
\symbol{R}{\pgflsystemmoveforward} % only move
\symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
}
\pgfdeclarelindenmayersystem{3-ary rooted tree-M}{ % define the middle branch
\rule{L -> L [s+ [-L] [M] [+R]]}
\rule{M -> M [s [-L] [M] [+R]]}
\rule{R -> R [s- [-L] [M] [+R]]}
\symbol{L}{\pgflsystemmoveforward} % only move
\symbol{M}{\pgflsystemdrawforward} % draw the M branch
\symbol{R}{\pgflsystemmoveforward} % only move
\symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
}
\pgfdeclarelindenmayersystem{3-ary rooted tree-R}{ % define the right branch
\rule{L -> L [s+ [-L] [M] [+R]]}
\rule{M -> M [s [-L] [M] [+R]]}
\rule{R -> R [s- [-L] [M] [+R]]}
\symbol{L}{\pgflsystemmoveforward} % only move
\symbol{M}{\pgflsystemmoveforward} % only move
\symbol{R}{\pgflsystemdrawforward} % draw the R branch
\symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
}
\draw[rotate = -90,color=blue] l-system [l-system={3-ary rooted tree-L,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];
\draw[rotate = -90,color=red] l-system [l-system={3-ary rooted tree-M,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];
\draw[rotate = -90,color=green] l-system [l-system={3-ary rooted tree-R,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];
\end{tikzpicture}
\end{figure}
\end{document}
編集: ツリーを一度だけ宣言するトリック (バグ? 機能?) を見つけました。出力は上記とまったく同じです。トリックは、\symbol
描画せずに 3 方向に移動するだけのコマンドを使用して、3 つのうちの 1 つを描画するように指示すると、その方向の以前の「移動のみ」コマンドが上書きされることです。パラメータを与えると、必要なブランチを動的に描画できます。
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\newcommand{\defbranch}[1]{
\pgfdeclarelindenmayersystem{3-ary rooted tree#1}{ % dynamic definition of the branches
\rule{L -> L [s+ [-L] [M] [+R]]}
\rule{M -> M [s [-L] [M] [+R]]}
\rule{R -> R [s- [-L] [M] [+R]]}
\symbol{L}{\pgflsystemmoveforward} % only move
\symbol{M}{\pgflsystemmoveforward} % only move
\symbol{R}{\pgflsystemmoveforward} % only move
\symbol{#1}{\pgflsystemdrawforward} % draw the branch (overrides previous command)
\symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
}
}
\defbranch{L} % define the branches (needed only once per documents)
\defbranch{M}
\defbranch{R}
\newcommand{\branch}[2]{ %define command to draw branches.
%You can add more parameters for step, angle, order,... if needed
\draw[rotate = -90,color=#2] l-system [l-system={3-ary rooted tree#1,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];
}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\branch{L}{blue}
\branch{M}{red}
\branch{R}{green}
\end{tikzpicture}
\end{figure}
\end{document}