我有以下代码:
\documentclass[a4paper, 11pt]{article}
\usepackage{comment} % enables the use of multi-line comments (\ifx \fi)
\usepackage{fullpage} % changes the margin
\usepackage[swedish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{titlesec}
\usepackage{pgf, tikz}
\usetikzlibrary{arrows, automata}
\usepackage{color}
\begin{document}
\begin{tikzpicture}[
> = stealth, % arrow head style
shorten > = 1pt, % don't touch arrow head to node
auto,
node distance = 2cm, % distance between nodes
semithick % line style
]
\tikzstyle{every state}=[
draw = black,
thin,
fill = cyan!29,
minimum size = 5mm
]
\node[state] (a) {$a$};
\node[state] (b)[below left=0.2cm and 0.2cm of a] {$b$};
\node[state] (c)[below left=0.2cm and 0.2cm of b] {$c$};
\node[state] (f)[below left=0.2cm and 0.2cm of c] {$f$};
\node[state] (j)[below left=0.2cm and 0.2cm of f ] {$j$};
\node[state] (d)[below left=0.2cm and 0.2cm of j] {$d$};
\node[state] (i)[below left=0.2cm and 0.2cm of d] {$i$};
\path[->] (a) edge node {} (b);
\path[->] (b) edge node {} (c);
\path[->] (c) edge node {} (f);
\path[->] (f) edge node {} (j);
\path[->] (j) edge node {} (d);
\path[->] (d) edge node {} (i);
\path[->] (b) edge[bend right=45, blue, very thick] node {} (j);
\end{tikzpicture}
\end{document}
我希望节点彼此之间更紧密,而不是分散在左侧。我该如何实现这样的定位?谢谢。
答案1
您可以根据需要调整参数node distance
,例如
node distance=1cm and 0cm
第一个是垂直偏移,第二个是水平偏移。请注意,below left
您附加到节点的“左下”角,甚至也会and 0cm
向左产生位移。您可以将这个数字设为负数,但如果您想要垂直堆叠,那么您应该只使用below
而不是below left
:
\documentclass[a4paper, 11pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows, automata}
\begin{document}
\begin{tikzpicture}[ > = stealth, % arrow head style
shorten > = 1pt, % don't touch arrow head to node
semithick % line style
]
\tikzstyle{every state}=[
draw = black,
thin,
fill = cyan!29,
minimum size = 7mm
]
\begin{scope}[node distance=1cm and 0cm, every node/.style=state]
\node (a) {$a$};
\node (b) [below left=of a] {$b$};
\node (c) [below left=of b] {$c$};
\node (f) [below left=of c] {$f$};
\node (j) [below left=of f] {$j$};
\node (d) [below left=of j] {$d$};
\node (i) [below left=of d] {$i$};
\end{scope}
\path[->] (a) edge (b)
(b) edge (c)
(c) edge (f)
(f) edge (j)
(j) edge (d)
(d) edge (i)
(b) edge[bend right=45, blue, very thick] (j);
\end{tikzpicture}
\hspace{2cm}
\begin{tikzpicture}[ > = stealth, % arrow head style
shorten > = 1pt, % don't touch arrow head to node
semithick % line style
]
\tikzstyle{every state}=[
draw = black,
thin,
fill = cyan!29,
minimum size = 7mm
]
\begin{scope}[node distance=1cm, every node/.style=state]
\node (a) {$a$};
\node (b) [below=of a] {$b$};
\node (c) [below=of b] {$c$};
\node (f) [below=of c] {$f$};
\node (j) [below=of f] {$j$};
\node (d) [below=of j] {$d$};
\node (i) [below=of d] {$i$};
\end{scope}
\path[->] (a) edge (b)
(b) edge (c)
(c) edge (f)
(f) edge (j)
(j) edge (d)
(d) edge (i)
(b) edge[bend right=45, blue, very thick] (j);
\end{tikzpicture}
\end{document}
答案2
您可以像这样自动执行所有过程:
\documentclass[a4paper, 11pt]{article}
\usepackage{comment} % enables the use of multi-line comments (\ifx \fi)
\usepackage{fullpage} % changes the margin
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{titlesec}
\usepackage{pgf, tikz}
\usetikzlibrary{arrows, automata}
\usepackage{color}
\xdef\mybelow{0.4cm} %< Select distance for below
\xdef\myleft{0.12cm} %< Select distance for left
\begin{document}
\begin{tikzpicture}[
> = stealth, % arrow head style
shorten > = 1pt, % don't touch arrow head to node
auto,
node distance = 2cm, % distance between nodes
semithick % line style
]
\tikzstyle{every state}=[
draw = black,
thin,
fill = cyan!29,
minimum size = 5mm
]
\foreach \letter [count=\i from 1] in {a,b,c,f,j,d,i}{
\ifnum\i=1
\xdef\myprevpoint{\letter}
\node[state] (\letter) {$\letter$};
\else
\node[state] (\letter)[below left=\mybelow and \myleft of \myprevpoint] {$\letter$};
\path[->] (\myprevpoint) edge node {} (\letter);
\xdef\myprevpoint{\letter}
\fi
}
\path[->] (b) edge[bend right=45, blue, very thick] node {} (j);
\end{tikzpicture}
\end{document}
输出:
tikz 是一个功能强大的软件包,最好将其自动化,以便将来更容易进行操作(如果需要更改纸张尺寸或其他操作)