请问我可以使用哪些键来获得以下两个类别中相同大小的圆圈?minimum size
并且inner sep
不改善输出。如果需要,请使用您选择的任何循环宏。
\begin{document}
\begin{tikzpicture}[scale=.8,auto=left,every node/.style={circle,thick}]
\foreach \x/\y in {a/{-1,-2},b/{1,-2},c/{2,-1},d/{2,1},e/{1,2},
f/{-1,2},g/{-2,1},h/{-2,-1}}{
\node (\x) at (\y) [fill=blue!20,draw=yellow] {\x};
}
\foreach \x/\y {a/b,b/c,c/d,d/e,e/f,f/g,g/h,h/a}{
\draw [->] (\x)--(\y) node [midway,fill=red!20,draw=blue,] {\x--\y};
}
\end{tikzpicture}
\end{document}
答案1
在以下示例中,我替换了\foxloop
(package ltxtools
),因为它会导致出现错误消息。我还增加了缩放因子以减少重叠。
示例使用了两次循环。在第一次循环中,计算了节点内容的最大框尺寸。(更新:\tempDimBox
为带有 的节点添加了缺失内容\x--\y
。)
\documentclass{article}
\usepackage{tikz}
\newdimen\tempDimWD
\newdimen\tempDimHT
\newdimen\tempDimDP
\newcommand*{\tempDimBox}[1]{%
\begingroup
\sbox0{\makebox[\tempDimWD]{#1}}%
\ht0=\tempDimHT
\dp0=\tempDimDP
\usebox0 %
\endgroup
}
\newcommand*{\tempDimMeasure}[3]{%
\node at (0,0) {%
\global\tempDimWD=0pt
\global\tempDimHT=0pt
\global\tempDimDP=0pt
\foreach #1 in #2 {%
\sbox0{#3}%
\ifdim\wd0>\tempDimWD
\global\tempDimWD=\wd0 %
\fi
\ifdim\ht0>\tempDimHT
\global\tempDimHT=\ht0
\fi
\ifdim\dp0>\tempDimDP
\global\tempDimDP=\dp0
\fi
}%
};%
}
\begin{document}
\begin{tikzpicture}[
% scale=.8,
auto=left,
every node/.style={circle,thick},
]
\def\yellowlist{%
a/{-1,-2},b/{1,-2},c/{2,-1},d/{2,1},e/{1,2},f/{-1,2},g/{-2,1},h/{-2,-1}%
}
\def\bluelist{a/b,b/c,c/d,d/e,e/f,f/g,g/h,h/a}
\tempDimMeasure{\x/\y}{\yellowlist}{\x}%
\foreach \x/\y in \yellowlist {
\node (\x) at (\y) [fill=blue!20,draw=yellow] {\tempDimBox{\x}};
}
\tempDimMeasure{\x/\y}{\bluelist}{\x--\y}
\foreach \x/\y in \bluelist {
\draw [->] (\x) -- (\y) node [midway,fill=red!20,draw=blue,] {\tempDimBox{\x--\y}};
}
\end{tikzpicture}
\qquad
\begin{tikzpicture}[
% scale=.8,
scale=1.2,
auto=left,
every node/.style={circle,thick},
]
\def\yellowlist{%
a/{-1,-2},b/{1,-2},c/{2,-1},d/{2,1},e/{1,2},f/{-1,2},g/{-2,1},h/{-2,-1}%
}
\def\bluelist{a/b,b/c,c/d,d/e,e/f,f/g,g/h,h/a}
%\tempDimMeasure{\x/\y}{\yellowlist}{\x}% Since we're looking for largest circle.
\tempDimMeasure{\x/\y}{\bluelist}{\x--\y}
\foreach \x/\y in \yellowlist {
\node (\x) at (\y) [fill=blue!20,draw=yellow] {\tempDimBox{\x}};
}
\foreach \x/\y in \bluelist {
\draw [->] (\x) -- (\y)
node [midway,fill=red!20,draw=blue] {\tempDimBox{\x--\y}};
}
\end{tikzpicture}
\end{document}
a
一种变体,其中节点h
位于正八边形的顶点上。
\documentclass{article}
\usepackage{tikz}
\newdimen\tempDimWD
\newdimen\tempDimHT
\newdimen\tempDimDP
\newcommand*{\tempDimBox}[1]{%
\begingroup
\sbox0{\makebox[\tempDimWD]{#1}}%
\ht0=\tempDimHT
\dp0=\tempDimDP
\usebox0 %
\endgroup
}
\newcommand*{\tempDimMeasure}[3]{%
\node at (0,0) {%
\global\tempDimWD=0pt
\global\tempDimHT=0pt
\global\tempDimDP=0pt
\foreach #1 in #2 {%
\sbox0{#3}%
\ifdim\wd0>\tempDimWD
\global\tempDimWD=\wd0 %
\fi
\ifdim\ht0>\tempDimHT
\global\tempDimHT=\ht0
\fi
\ifdim\dp0>\tempDimDP
\global\tempDimDP=\dp0
\fi
}%
};%
}
\begin{document}
\begin{tikzpicture}[
% scale=.8,
auto=left,
every node/.style={circle,thick},
]
\def\yellowlist{a,...,h}
\def\bluelist{a/b,b/c,c/d,d/e,e/f,f/g,g/h,h/a}
\tempDimMeasure{\x/\y}{\yellowlist}{\x}%
\foreach [count=\xi] \x in \yellowlist {
\node (\x) at ({180+360/16+360/8*\xi:2})
[fill=blue!20,draw=yellow] {\tempDimBox{\x}};
}
\tempDimMeasure{\x/\y}{\bluelist}{\x--\y}
\foreach \x/\y in \bluelist {
\draw [->] (\x) -- (\y) node [midway,fill=red!20,draw=blue,] {\tempDimBox{\x--\y}};
}
\end{tikzpicture}
\qquad
\begin{tikzpicture}[
% scale=.8,
scale=1.2,
auto=left,
every node/.style={circle,thick},
]
\def\yellowlist{a,...,h}
\def\bluelist{a/b,b/c,c/d,d/e,e/f,f/g,g/h,h/a}
%\tempDimMeasure{\x/\y}{\yellowlist}{\x}% Since we're looking for largest circle.
\tempDimMeasure{\x/\y}{\bluelist}{\x--\y}
\foreach [count=\xi] \x/\y in \yellowlist {
\node (\x) at ({180+360/16+360/8*\xi:2})
[fill=blue!20,draw=yellow] {\tempDimBox{\x}};
}
\foreach \x/\y in \bluelist {
\draw [->] (\x) -- (\y)
node [midway,fill=red!20,draw=blue] {\tempDimBox{\x--\y}};
}
\end{tikzpicture}
\end{document}
答案2
也使用两个多边形的解决方案
命令\GrapheBoucle[0.8]{8}{a, b, c, d, e, f, g, h}{ab, bc, cd,, ef, fg, gh, ha}{4.5cm}
生成两个角度偏移的多边形
- 第一个顶点到第一个列表的项
{a, b, c, d, e, f, g, h}
,这样的顶点由弧连接, - 第二个峰值对应第二个列表
{ab, bc, cd,, ef, fg, gh, ha}
- 通过第一个多边形顶点的圆的半径由最后一个参数指定
{4.5cm}
- 第一个参数(可选)
[0.8]
是拔模比,用于指定第二个多边形相对于第一个多边形的半径。 - 第二个参数
{8}
,多边形的顶点数
\documentclass{文章}
\usepackage{tikz}
enter code here
\usetikzlibrary{shapes,arrows,patterns,snakes,decorations,chains,fit}
\newcommand{\GrapheBoucle}[5][0.8]{
\node[regular polygon, regular polygon sides=#2, minimum size=#5,rotate=180](sommet) at (0,0) {};
\node[regular polygon, regular polygon sides=#2, minimum size={#5*#1},rotate={180+(180)/#2}](lien) at (0,0) {};
\pgfmathparse{180/(#2+1)}
\foreach \i in {1,...,#2}{
\node (LGraphe\i) at (lien.corner \i){};
}
\begin{scope}[start chain=graphe placed {at=(sommet.corner \tikzchaincount)}, every join/.style={bend right=\pgfmathresult},->]
\foreach \i in {#3}{
\node [on chain,join, circle, draw,fill=blue!20,draw=yellow](NGraphe\tikzchaincount) {\i};
}
\end{scope}
\begin{scope}[start chain=noeud placed {at=(lien.corner \tikzchaincount)}, every join/.style={bend right=\pgfmathresult}]
\foreach \i in {#4}{
\node [on chain, circle, draw,fill=red!20,draw=blue](NGraphe\tikzchaincount) {\i};
}
\end{scope}
\draw (graphe-end) to[bend right={\pgfmathresult}] (graphe-begin);
}
\begin{document}
\begin{tikzpicture}
\GrapheBoucle[0.8]{8}{a,b,c,d,e,f,g,h}{a-b,b-c,c-d,d-e,e-f,f-g,g-h,h-a}{4.5cm}
\end{tikzpicture}
\end{document}