我在彼此内定义了 Tikz 图片。排版失败,导致边缘绘制不正确:边缘不是从格子图结构中的顶点中心绘制的,而是像下面这样在顶点的侧面绘制的
并且格子图结构在 2.MWE 中,而简化示例在 1.MWE 中没有这个问题:只有在拥有许多 tikz 环境时才会发生此问题。
1. MWE 在顶点中心之间很好地绘制了边缘
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=4pt,minimum size=3pt]
\draw (0,0)node{};
\draw (1,0)node{};
\draw (0.5,1)node{};
\draw (0,0)--(1,0);
\end{tikzpicture}
2. MWE 没有很好地绘制顶点中心之间的边缘,为什么?
\begin{tikzpicture}[scale=1.2]
\tikzstyle{every node}=[draw, circle]
\node(0) {
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=0pt,minimum size=3pt]
\draw (0,0)node{};
\draw (1,0)node{};
\draw (0.5,1)node{};
\end{tikzpicture}
};
\node[above =of 0](1){
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=1pt,minimum size=3pt]
\draw (0,0)node{} -- (1,0)node{};
\draw (0.5,1)node{};
\end{tikzpicture}
};
\node[above right =of 1](21){
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=1pt,minimum size=3pt]
\draw (0,0)node{} -- (1,0)node{} -- (0.5,1)node{};
\end{tikzpicture}
};
\node[above left =of 1](22){
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=1pt,minimum size=3pt]
\draw (0,0)node{} -- (0.5,1)node{} -- (1,0)node{};
\end{tikzpicture}
};
\node[above left =of 21](30){
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=1pt,minimum size=3pt]
\draw (0,0)node{} -- (1,0)node{} -- (0.5,1)node{};
\draw (0,0)node{} -- (0.5,1)node{};
\end{tikzpicture}
};
\draw (0) -- (1);
\draw (1) -- (21);
\draw (1) -- (22);
\draw (21) -- (30);
\draw (22) -- (30);
%\draw (21) -- (31);
%\draw (21) -- (32);
%\draw (21) -- (33);
%\draw (31) -- (41);
%\draw (31) -- (42);
%\draw (32) -- (42);
%\draw (33) -- (41);
%\draw (22) -- (32);
\end{tikzpicture}
其中格子图排版失败,中心之间的边缘没有画出来。所以
如何在许多 tikz 环境中从格子图中的顶点中心正确地绘制边?
或许相关
答案1
有几种方法可以实现此目的而无需嵌套tikzpicture
。例如,您可以先将五个圆圈绘制为节点,然后绘制相对于这些圆圈的点和线,最后在圆圈之间绘制线。
在下面的代码中\drawdots
,为了方便起见,我定义了一个宏,用于绘制每个圆圈中的三个点。它接受一个参数,即节点的名称,并有三行以下格式的代码:
\path (#1.center) ++(90:0.7cm) node [dotnode] (#1-top) {};
#1
表示传递给宏的节点名称,也是(#1.center)
节点的中心锚点。++(90:0.7cm)
大致意思是“将活动点移动到距离前一个点 0.7 厘米的点,角度为 90 度”。此时,用 声明了一个新节点node [dotnode] (#1-top) {};
。dotnode
样式定义为
dotnode/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,fill,circle}
因此它将创建一个小圆圈,填充黑色,该节点的名称为#1-top
。
\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
dotnode/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,fill,circle},
bigcircle/.style={draw,circle,minimum size=2cm}
}
\newcommand{\drawdots}[1]{
\path (#1.center) ++(90:0.7cm) node [dotnode] (#1-top) {};
\path (#1.center) ++(-30:0.7cm) node [dotnode] (#1-right) {};
\path (#1.center) ++(210:0.7cm) node [dotnode] (#1-left) {};
}
\begin{document}
\begin{tikzpicture}
% place circles
\node [bigcircle] (A) {};
\node [bigcircle,above=of A] (B) {};
\node [bigcircle,above right=of B] (C) {};
\node [bigcircle,above left=of B] (D) {};
\node [bigcircle,above right=of D] (E) {};
% draw dots
\foreach \A in {A,...,E}
\drawdots{\A};
% draw edges between dots
\draw (B-left) -- (B-right);
\draw (C-left) -- (C-right) -- (C-top);
\draw (D-left) -- (D-top) -- (D-right);
\draw (E-left) -- (E-right) -- (E-top) -- (E-left);
% draw edges between circles
\draw (A) -- (B) -- (C) -- (E) -- (D) -- (B);
\end{tikzpicture}
\end{document}
另一个选择是使用pic
,下面的代码中有一个示例。
\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\pgfmathsetmacro{\circrad}{0.7}
\tikzset{
dotnode/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,fill,circle},
pics/dotcircle/.style args={#1#2#3}{
code={
\node [circle,draw,inner sep=0pt,outer sep=0pt,minimum size=2*\circrad cm] (-circ) at (0,0) {};
\node [dotnode] (-top) at (90:0.7*\circrad cm) {};
\node [dotnode] (-right) at (-30:0.7*\circrad cm) {};
\node [dotnode] (-left) at (210:0.7*\circrad cm) {};
\ifnum #1>0
\draw (-left) -- (-right);
\fi
\ifnum #2 > 0
\draw (-right) -- (-top);
\fi
\ifnum #3 > 0
\draw (-left) -- (-top);
\fi
}}}
\begin{document}
\begin{tikzpicture}
\pic (A) at (0,0) {dotcircle={000}};
\pic (B) at (0,2) {dotcircle={100}};
\pic (C) at (2,4) {dotcircle={110}};
\pic (D) at (-2,4) {dotcircle={011}};
\pic (E) at (0,6) {dotcircle={111}};
\foreach \A/\B in {A/B,B/C,B/D,D/E,C/E}
\draw (\A-circ) -- (\B-circ);
\end{tikzpicture}
\end{document}