我的右侧有一个 tikz 图。
\documentclass{article}
\begin{document}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,positioning,shadows}
\begin{tikzpicture}[
level distance = 4em,
left/.style={edge from parent/.style={>=latex,<-,very thick,draw}},
right/.style={edge from parent/.style={>=latex,->,very thick,draw}},
sloped,
root/.style = {font=\footnotesize,very thick, shape=circle,
draw, align=center, drop shadow,
top color=white, bottom color=blue!20},
next/.style = {font=\footnotesize,text width=4mm, inner sep=1pt,very thick, shape=circle,
draw, align=center, drop shadow,
top color=white, bottom color=blue!20},
level 1/.style={sibling distance=8em},
level 2/.style={sibling distance=4em},
level 3/.style={sibling distance=2em}
]
\node[root] {Now} [grow=right,right]
child{node[next] {$+1$}
child{node[next] {$+2$}
child{node[next] {$+3$}}
child{node[next] {$+3$}}}
child{node[next] {$+2$}
child{node[next] {$+3$}}
child{node[next] {$+3$}}}}
child{node[next] {$+1$}
child{node[next] {$+2$}
child{node[next] {$+3$}}
child{node[next] {$+3$}}}
child{node[next] {$+2$}
child{node[next] {$+3$}}
child{node[next] {$+3$}}}}
;
\end{tikzpicture}
\end{document}
我想要一个从左边增长的镜像,只是每个孩子的文字都被减号替换。
有谁知道这是怎么做到的吗?
答案1
只需使用grow=180
,并+
替换-
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows}
\begin{document}
\begin{tikzpicture}[
level distance = 4em,
left/.style={edge from parent/.style={>=latex,<-,very thick,draw}},
right/.style={edge from parent/.style={>=latex,->,very thick,draw}},
sloped,
root/.style = {font=\footnotesize,very thick, shape=circle,
draw, align=center, drop shadow,
top color=white, bottom color=blue!20},
next/.style = {font=\footnotesize,text width=4mm, inner sep=1pt,very thick, shape=circle,
draw, align=center, drop shadow,
top color=white, bottom color=blue!20},
level 1/.style={sibling distance=8em},
level 2/.style={sibling distance=4em},
level 3/.style={sibling distance=2em}
]
\node[root] {Now} [grow=180,right] %<-
child{node[next] {$-1$}
child{node[next] {$-2$}
child{node[next] {$-3$}}
child{node[next] {$-3$}}}
child{node[next] {$-2$}
child{node[next] {$-3$}}
child{node[next] {$-3$}}}}
child{node[next] {$-1$}
child{node[next] {$-2$}
child{node[next] {$-3$}}
child{node[next] {$-3$}}}
child{node[next] {$-2$}
child{node[next] {$-3$}}
child{node[next] {$-3$}}}}
;
\end{tikzpicture}
\end{document}
有了它forest
你就可以自动绘制树。
\documentclass{article}
\usepackage[edges]{forest}
\usetikzlibrary{shadows}
\tikzset{root/.style = {font=\footnotesize,very thick, shape=circle,
draw, align=center, drop shadow,
top color=white, bottom color=blue!20},
next/.style = {font=\footnotesize,text width=4mm, inner sep=1pt,very thick, shape=circle,
draw, align=center, drop shadow,
top color=white, bottom color=blue!20},}
\begin{document}
\begin{forest}
for tree={grow=west,edge={>=latex,->,very thick,draw},
l sep+=1ex,
if level=0{root}{next,delay={content/.wrap pgfmath arg={$-#1$}{level}}}}
[Now
[
[
[]
[]
]
[
[]
[]
]
]
[
[
[]
[]
]
[
[]
[]
]
]
]
\end{forest}
\end{document}
您可以将其与原始树结合起来。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows}
\begin{document}
\begin{tikzpicture}[
level distance = 4em,
left/.style={edge from parent/.style={>=latex,<-,very thick,draw}},
right/.style={edge from parent/.style={>=latex,->,very thick,draw}},
sloped,
root/.style = {font=\footnotesize,very thick, shape=circle,
draw, align=center, drop shadow,
top color=white, bottom color=blue!20},
next/.style = {font=\footnotesize,text width=4mm, inner sep=1pt,very thick, shape=circle,
draw, align=center, drop shadow,
top color=white, bottom color=blue!20},
level 1/.style={sibling distance=8em},
level 2/.style={sibling distance=4em},
level 3/.style={sibling distance=2em}
]
\node[root] (lr){Now} [grow=180,right] %<-
child{node[next] {$-1$}
child{node[next] {$-2$}
child{node[next] {$-3$}}
child{node[next] {$-3$}}}
child{node[next] {$-2$}
child{node[next] {$-3$}}
child{node[next] {$-3$}}}}
child{node[next] {$-1$}
child{node[next] {$-2$}
child{node[next] {$-3$}}
child{node[next] {$-3$}}}
child{node[next] {$-2$}
child{node[next] {$-3$}}
child{node[next] {$-3$}}}}
;
\node[root,xshift=4em] {Now} [grow=right,right]
child{node[next] {$+1$}
child{node[next] {$+2$}
child{node[next] {$+3$}}
child{node[next] {$+3$}}}
child{node[next] {$+2$}
child{node[next] {$+3$}}
child{node[next] {$+3$}}}}
child{node[next] {$+1$}
child{node[next] {$+2$}
child{node[next] {$+3$}}
child{node[next] {$+3$}}}
child{node[next] {$+2$}
child{node[next] {$+3$}}
child{node[next] {$+3$}}}}
;
\end{tikzpicture}
\end{document}
如果你放弃xshift=4em
,你就会得到