![如何改变节点之间的距离并将它们组合在一起并将它们带到 TikZ 中的中心](https://linux22.com/image/284089/%E5%A6%82%E4%BD%95%E6%94%B9%E5%8F%98%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E8%B7%9D%E7%A6%BB%E5%B9%B6%E5%B0%86%E5%AE%83%E4%BB%AC%E7%BB%84%E5%90%88%E5%9C%A8%E4%B8%80%E8%B5%B7%E5%B9%B6%E5%B0%86%E5%AE%83%E4%BB%AC%E5%B8%A6%E5%88%B0%20TikZ%20%E4%B8%AD%E7%9A%84%E4%B8%AD%E5%BF%83.png)
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[remember picture,overlay,every node/.style={inner sep=2pt}]
\node[anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2]
at ($(current page.north east)+(-0.5in,-0.1\paperheight)$) {C};
\node[anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2]
at ($(current page.north east)+(-0.5in,-0.2\paperheight)$) {Y};
\node[anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2]
at ($(current page.north east)+(-0.5in,-0.3\paperheight)$) {B};
\node[anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2]
at ($(current page.north east)+(-0.5in,-0.4\paperheight)$) {Y};
\node[anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2]
at ($(current page.north east)+(-0.5in,-0.5\paperheight)$) {R};
\node[anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2]
at ($(current page.north east)+(-0.5in,-0.6\paperheight)$) {U};
\node[anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2]
at ($(current page.north east)+(-0.5in,-0.7\paperheight)$) {S};
\node[anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2]
at ($(current page.north east)+(-0.5in,-0.8\paperheight)$) {1};
\node[anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2]
at ($(current page.north east)+(-0.5in,-0.9\paperheight)$) {4};
\end{tikzpicture}
\end{document}
这里,我如何减少节点之间的距离并将它们分组并将它们置于边缘的中心。
答案1
您可以使用positioning
库和密钥node distance
来实现这一点。我有 Gonzalo 的方框技术,因为可以很容易地将方框的中心放在页面东侧。
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\tikzset{mynode/.style={anchor=center,circle,fill=magenta,text=white,font=\sffamily,scale=2,inner
sep=2pt,minimum width=0.6cm}}
\newsavebox\mybox
\savebox\mybox{%
\begin{tikzpicture}[node distance=.1cm]
\node[mynode] (c) {C};
\node[mynode,below=of c] (y1) {Y};
\node[mynode,below=of y1] (b) {B};
\node[mynode,below=of b] (y2) {Y};
\node[mynode,below=of y2] (r) {R};
\node[mynode,below=of r] (u) {U};
\node[mynode,below=of u] (s) {S};
\node[mynode,below=of s] (1) {1};
\node[mynode,below=of 1] (4) {4};
\end{tikzpicture}%
}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\node at ($(current page.east)+(-0.5in,0)$) {\usebox\mybox};
\end{tikzpicture}
\end{document}
改变node distance
将会改变节点之间的分离。
答案2
一个选项是使用链条轻松定位节点,使用盒子保存初始内容;node distance
可以用来控制距离:
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc,chains,positioning}
\newsavebox\mybox
\savebox\mybox{%
\begin{tikzpicture}[
start chain=going below,
node distance=1mm,
every node/.style={
anchor=center,
circle,
fill=magenta,
text=white,
font=\sffamily,
scale=2,
inner sep=2pt,
outer sep=0pt,
on chain
},
]
\node {C};
\node {Y};
\node {B};
\node {Y};
\node {R};
\node {U};
\node {S};
\node {1};
\node {4};
\end{tikzpicture}%
}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\node at ($(current page.east)+(-0.5in,0)$) {\usebox\mybox};
\end{tikzpicture}
\end{document}