答案1
让我们以非常简单的方式一步一步地研究这个问题。首先,我们可能想要为我们的节点设计一个自定义样式,因为这样可以更轻松地以相同的方式设置所有节点的样式。我们将首先定位节点,然后再连接它们。
\documentclass[border=10pt]{standalone}
% we need to load Ti*k*Z obviously
\usepackage{tikz}
% we define a custom style `square` which we can apply to all the nodes
\tikzset{
square/.style={
% draw a border
draw,
% make the node a square
minimum width=2em,
minimum height=2em,
}
}
\begin{document}
\begin{tikzpicture}
% inside the `tikzpicture` environment, we place a node using our `square` style
% we name this node `A` in order to be able to refer to it later
\node[square] at (0,0) (A) {1};
\end{tikzpicture}
\end{document}
钛钾Z 附带了很多不同的库,这些库在其非常庞大但也非常有用的文档中有详细描述手动的。我们可以使用positioning
库来相对定位节点。首先,让我们将一个节点放置在第一个节点右侧 1 厘米处:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
square/.style={
draw,
minimum width=2em,
minimum height=2em,
}
}
\begin{document}
\begin{tikzpicture}
\node[square] at (0,0) (A) {1};
\node[square, right=1cm of A] (B) {2};
\end{tikzpicture}
\end{document}
现在,有了这两个,我们可以使用该calc
库将另一个节点放置在位于这两个节点中间的坐标下方。该calc
库提供了一种强大的语法,乍一看可能有点尴尬:$(A)!0.5!(B)$
表示“位于(A)
和中间的坐标(B)
”。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}
\tikzset{
square/.style={
draw,
minimum width=2em,
minimum height=2em,
}
}
\begin{document}
\begin{tikzpicture}
\node[square] at (0,0) (A) {1};
\node[square, right=1cm of A] (B) {2};
\node[square, below=1cm of $(A)!0.5!(B)$] (C) {3};
\end{tikzpicture}
\end{document}
了解了这一点后,我们现在可以以类似的方式放置其他节点。只要按照正确的顺序定义节点,就可以做到这一点,而无需进行额外的复杂计算:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}
\tikzset{
square/.style={
draw,
minimum width=2em,
minimum height=2em,
}
}
\begin{document}
\begin{tikzpicture}
\node[square] at (0,0) (A) {1};
\node[square, right=1cm of A] (B) {2};
\node[square, below=1cm of $(A)!0.5!(B)$] (C) {3};
\node[square, right=1.5cm of $(B)!0.5!(C)$] (D) {4};
\node[square, right=1cm of D] (G) {7};
\node[square, below=0.75cm of G] (H) {8};
\node[square, below=0.75cm of H] (I) {9};
\node[square, left=1.5cm of I] (F) {6};
\node[square, left=1.5cm of F] (E) {5};
\node[square, right=1cm of H] (J) {10};
\end{tikzpicture}
\end{document}
现在我们可以在节点之间添加边了。让我们从在需要的地方在节点之间添加简单的水平线和垂直线开始。我们还可以使用相对坐标,例如++(1,0)
将一条线从一个节点延伸到某个方向:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}
\tikzset{
square/.style={
draw,
minimum width=2em,
minimum height=2em,
}
}
\begin{document}
\begin{tikzpicture}
\node[square] at (0,0) (A) {1};
\node[square, right=1cm of A] (B) {2};
\node[square, below=1cm of $(A)!0.5!(B)$] (C) {3};
\node[square, right=1.5cm of $(B)!0.5!(C)$] (D) {4};
\node[square, right=1cm of D] (G) {7};
\node[square, below=0.75cm of G] (H) {8};
\node[square, below=0.75cm of H] (I) {9};
\node[square, left=1.5cm of I] (F) {6};
\node[square, left=1.5cm of F] (E) {5};
\node[square, right=1cm of H] (J) {10};
\draw (A) -- (B);
\draw (E) -- (F);
\draw (H) -- (J);
\draw (J) -- ++(1,0);
\end{tikzpicture}
\end{document}
接下来,你应该熟悉锚点。根据节点的形状(默认为rectangle
),每个节点都有不同的锚点,这些锚点大多位于节点的边界上。几乎每个形状的顶部、底部、左侧和右侧都有一个锚点,分别称为north
、和。此外,锚点位于节点的中心。你可以使用节点的名称指向这样的锚点,添加一个点,然后south
添加锚点的名称,例如:east
west
center
A.south
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\tikzset{
square/.style={
draw,
minimum width=2em,
minimum height=2em,
}
}
\begin{document}
\begin{tikzpicture}
\node[square] at (0,0) (A) {1};
\fill[red] (A.south) circle[radius=1pt];
\end{tikzpicture}
\end{document}
-|
了解了这一点以及我们可以使用和|-
代替(我们用它来绘制简单的直线)来绘制正交边的事实--
,我们可以得出以下结论:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}
\tikzset{
square/.style={
draw,
minimum width=2em,
minimum height=2em,
}
}
\begin{document}
\begin{tikzpicture}
\node[square] at (0,0) (A) {1};
\node[square, right=1cm of A] (B) {2};
\node[square, below=1cm of $(A)!0.5!(B)$] (C) {3};
\node[square, right=1.5cm of $(B)!0.5!(C)$] (D) {4};
\node[square, right=1cm of D] (G) {7};
\node[square, below=0.75cm of G] (H) {8};
\node[square, below=0.75cm of H] (I) {9};
\node[square, left=1.5cm of I] (F) {6};
\node[square, left=1.5cm of F] (E) {5};
\node[square, right=1cm of H] (J) {10};
\draw (A) -- (B);
\draw (E) -- (F);
\draw (H) -- (J);
\draw (J) -- ++(1,0);
\draw (A.west) -- ++(-0.25,0) |- (C);
\draw (B.east) -- ++(0.25,0) |- (C);
\draw (D.east) -- ++(0.25,0) |- (F);
\draw (G.west) -- ++(-0.25,0) |- (I);
\draw (G.east) -- ++(0.25,0) |- (I);
\end{tikzpicture}
\end{document}
使用|-
或-|
语法的正交连接,可以相对简单地在此边的水平或垂直部分的中间以及水平和垂直部分相交的坐标上添加坐标(请注意,在下面的例子中,坐标仅放置在边的第二部分,该部分从 A 上方 0.5 厘米处开始,首先水平然后垂直到 B):
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
square/.style={
draw,
minimum width=2em,
minimum height=2em,
}
}
\begin{document}
\begin{tikzpicture}
\node[square] at (0,0) (A) {1};
\node[square, right=1cm of A] (B) {2};
\draw (A.north) -- ++(0,0.5) -| (B)
coordinate[at start] (C)
coordinate[near start] (D)
coordinate[midway] (E)
coordinate[near end] (F)
coordinate[at end] (G);
\fill[red] (C) circle[radius=1pt];
\fill[magenta] (D) circle[radius=1pt];
\fill[blue] (E) circle[radius=1pt];
\fill[cyan] (F) circle[radius=1pt];
\fill[green] (G) circle[radius=1pt];
\end{tikzpicture}
\end{document}
我们可以利用这一点,并使用以下示例中的语法在边上放置坐标,这最终帮助我们添加最后缺失的边:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}
\tikzset{
square/.style={
draw,
minimum width=2em,
minimum height=2em,
}
}
\begin{document}
\begin{tikzpicture}
\node[square] at (0,0) (A) {1};
\node[square, right=1cm of A] (B) {2};
\node[square, below=1cm of $(A)!0.5!(B)$] (C) {3};
\node[square, right=1.5cm of $(B)!0.5!(C)$] (D) {4};
\node[square, right=1cm of D] (G) {7};
\node[square, below=0.75cm of G] (H) {8};
\node[square, below=0.75cm of H] (I) {9};
\node[square, left=1.5cm of I] (F) {6};
\node[square, left=1.5cm of F] (E) {5};
\node[square, right=1cm of H] (J) {10};
\draw (A) -- (B);
\draw (E) -- (F);
\draw (H) -- (J);
\draw (J) -- ++(1,0);
\draw (A.west) -- ++(-0.25,0) |- (C) coordinate[near start] (AC);
\draw (B.east) -- ++(0.25,0) |- (C) coordinate[near start] (BC);
\draw (D.east) -- ++(0.25,0) |- (F) coordinate[near start] (DF);
\draw (G.west) -- ++(-0.25,0) |- (I);
\draw (G.east) -- ++(0.25,0) |- (I);
\draw (AC) -- ++(-0.25,0) |- (E) coordinate[near start] (ACE);
\draw (BC) -- (D);
\draw (DF) -- (H);
\draw (ACE) -- ++(-1,0);
\end{tikzpicture}
\end{document}
我们就完成了。
答案2
我会选择一个矩阵,其中节点 1、2 和 3 与节点 5 位于同一列,并且与节点 4 和 7 位于同一行。
节点 1、2 和 3 通过chains
库使用的特殊圆形放置机制进行放置。这或多或少会自动将节点定位到彼此。|[c]|
矩阵内放置了一些有用的坐标 ( ) 来代替节点。
其中许多连接是通过-|-
路径操作对于与节点 3 的连接,辅助坐标命名为x
和,y
用于将垂直部分放在正确的位置,就像与节点 1 和 2 平行的连接一样。
我用它rounded corners
来展示如何建立连接。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{calc, cd, chains, ext.paths.ortho}
\tikzset{
-|- through point/.style={
/tikz/horizontal vertical horizontal,
/tikz/execute at begin to=
\tikzset{insert path={let \p0=($(#1)-(\tikztostart)$) in},
ortho/distance/.expanded=abs(\x0), ortho/from center}},
matrix node/.default=name,
matrix node/.style={%
#1=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}}
\makeatletter
\tikzcdset{
to suffix/.code=\edef\tikzcd@ar@target{\tikzcd@ar@target#1},
from suffix/.code=\edef\tikzcd@ar@start {\tikzcd@ar@start#1}}
\makeatother
\tikzcdset{shortcuts/.style={
/tikz/ortho/install shortcuts,
/tikz/c/.style={shape=coordinate, yshift=axis_height},
/tikz/commutative diagrams/c/.style={
/tikz/every to/.append style={edge node={coordinate (##1)}}}}}
\tikzset{
counterclockwise placement/.style 2 args={% #1 = phase, #2 = n; snaps to y = ±1
/utils/exec=\pgfmathsetmacro\ang{#1+(\tikzchaincount-1)*360/(#2)},
at={(\ang:1|-0,{\ang<180?1:-1})}}}
\begin{document}
\begin{tikzcd}[
shortcuts, rounded corners,
cells={nodes={draw, minimum size=+8mm, align=center, text width=width("$00$")}},
row sep={1cm, between origins}, arrows=-]
& |[c]| \ar[r, to suffix=-1, -|-, c=x]
\ar[r, to suffix=-3, -|- through point=x]
& \path[y=+.5cm, start chain=placed {counterclockwise placement={30}{3}}]
nodeforeach \t in {2, 1, 3} [on chain, matrix node=name prefix](-\t){\t};
\ar [from suffix=-1, to suffix=-2]
\rar[from suffix=-2, -|-, c=y]
\rar[from suffix=-3, -|- through point=y]
& 4 \drar[-|-]
& & 7 \drar[-|-]
\\
|[c]| \urar[-|-] \drar[-|-]
& & & & |[c]| \rar \urar[-|-] \drar[-|-]
& 8 \rar
& 10 \rar
& |[c]|
\\
& |[c]| \rar
& 5 \rar
& 6 \urar[-|-]
& & 9 \urar[-|-]
\end{tikzcd}
\end{document}