我有以下代码:
\documentclass[letterpaper]{article}
\usepackage{tikz}
\tikzset{
node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
every node/.style={circle,thin, fill=black}, % sets the properties for each ’state’ node
}
\begin{document}
Uma representação geométrica deste plano segue abaixo.
\begin{figure}[H]
\centering
\begin{tikzpicture}[node distance = 3cm,
every node/.style={
circle, thin, inner sep = 2pt,fill, black!80
},
black!80
]
\node[label = {$1$}] (1) {};
\node[right of=1,label = {$2$}] (2) {};
\node[right of=2,label = {$3$}] (3) {};
\node[below of=1,label = left:{$4$}] (4) {};
\node[below of=2,label = {[label distance = .12cm]10:$5$}] (5) {};
\node[below of=3,label = right:{$6$}] (6) {};
\node[below of=4,label = below:{$7$}] (7) {};
\node[below of=5,label = below:{$8$}] (8) {};
\node[below of=6,label = below:{$9$}] (9) {};
\draw[-]
(1) edge[asparagus] (2)
(2) edge[asparagus] (3)
(4) edge[red!80] (5)
(5) edge[red!80] (6)
(7) edge[purple] (8)
(8) edge[purple] (9)
(3) edge[orange!80] (5)
(5) edge[orange!80] (7)
(1) edge[gray] (5)
(5) edge[gray] (9)
(3) edge[bleudefrance!80] (6)
(6) edge[bleudefrance!80] (9)
(1) edge[cyan!80] (4)
(4) edge[cyan!80] (7)
(2) edge[pink] (5)
(5) edge[pink] (8)
(2) edge[dashed] (4)
(4) edge[dashed, out=225, in=225, looseness=2] (9)
(1) edge[orange!80,dashed, out=200, in=225,looseness=2.5] (8)
(8) edge[orange!80,dashed] (6)
(3) edge[purple,dashed, out=140, in = 140, looseness =2] (4)
(4) edge[purple,dashed] (8)
(6) edge[cyan,dashed] (2)
(2) edge[cyan,dashed, out=140, in=140, looseness = 2.5] (7);
\end{tikzpicture}
\end{figure}
\end{document}
结果如下:
正如在输出,空格和图形之间的垂直间距太大。如果我尝试在图形后书写,也会发生这种情况,也就是说,如果我在图形后添加文本,也会有较大的垂直间距。有没有办法减少这个间距?
答案1
- 您的 MWE 无法编译。遗漏了边缘颜色的定义。
- 尝试使用
bbox
库(可在 CTAN 上使用):
\documentclass[letterpaper, ]{article}
\usepackage{tikz}
\usetikzlibrary{bbox}
\begin{document}
Uma representação geométrica deste plano segue abaixo.
\begin{figure}[ht]
\centering
\begin{tikzpicture}[bezier bounding box, % <----
node distance = 22mm,
start chain = right,
dot/.style = {circle, thin, inner sep = 2pt, fill=black!80},
]
\begin{scope}[nodes={dot}]
\node[label = {$1$}] (1) {};
\node[right of=1,label = {$2$}] (2) {};
\node[right of=2,label = {$3$}] (3) {};
\node[below of=1,label = left:{$4$}] (4) {};
\node[below of=2,label = {[label distance = .12cm]10:$5$}] (5) {};
\node[below of=3,label = right:{$6$}] (6) {};
\node[below of=4,label = below:{$7$}] (7) {};
\node[below of=5,label = below:{$8$}] (8) {};
\node[below of=6,label = below:{$9$}] (9) {};
\end{scope}
\draw[-]
(1) edge[asparagus] (2)
(2) edge[asparagus] (3)
(4) edge[red!80] (5)
(5) edge[red!80] (6)
(7) edge[purple] (8)
(8) edge[purple] (9)
(3) edge[orange!80] (5)
(5) edge[orange!80] (7)
(1) edge[gray] (5)
(5) edge[gray] (9)
(3) edge[bleudefrance!80] (6)
(6) edge[bleudefrance!80] (9)
(1) edge[cyan!80] (4)
(4) edge[cyan!80] (7)
(2) edge[pink] (5)
(5) edge[pink] (8)
(2) edge[dashed] (4)
(4) edge[dashed, out=225, in=225, looseness=2] (9)
(1) edge[orange!80,dashed, out=200, in=225,looseness=2.5] (8)
(8) edge[orange!80,dashed] (6)
(3) edge[purple,dashed, out=140, in = 140, looseness =2] (4)
(4) edge[purple,dashed] (8)
(6) edge[cyan,dashed] (2)
(2) edge[cyan,dashed, out=140, in=140, looseness = 2.5] (7);
\end{tikzpicture}
\end{figure}
\end{document}
强制编译后得到以下结果:
附录:
题外话:重新设计 OP 图像(以及代码的样式)的示例。更改的内容包括:
- 标签的位置定义为标签锚点,对于所有节点都是相同的。
- 节点之间的线在背景中绘制。
- 线条颜色从默认颜色中选择
xcolor
(由包加载tikz
)。现在 MWE 可以编译而不会出现警告和错误。 - 节点通过使用
chains
库来放置。这使代码更短更清晰。
\documentclass[letterpaper, ]{article}
\usepackage[a5paper,
showframe]{geometry}
\usepackage{tikz}
\usetikzlibrary{backgrounds,
bbox, % <----
chains,
positioning}
\begin{document}
Uma representação geométrica deste plano segue abaixo.
\begin{figure}[ht]
\centering
\begin{tikzpicture}[bezier bounding box, % <----
node distance = 22mm,
start chain = right,
dot/.style = {circle, thin, inner sep = 2pt, fill=black!80,
on chain,
label=#1, node contents={},
},
every label/.append style = {font=\small, fill=white, anchor=south west,
inner sep=1.2pt, outer sep=1pt}
]
\node (1) [dot = $1$];
\node (2) [dot = $2$];
\node (3) [dot = $3$];
\node (4) [dot = $4$, below=of 1];
\node (5) [dot = $5$];
\node (6) [dot = $6$];
\node (7) [dot = $7$, below=of 4];
\node (8) [dot = $8$];
\node (9) [dot = $9$];
\scoped[on background layer]
\draw (1) edge[olive] (2) edge[olive] (3) % asparagus
(4) edge[red!80] (5) edge[red!80] (6)
(7) edge[purple] (8) edge[purple] (9)
%
(3) edge[orange!80] (5) edge[orange!80] (7)
(1) edge[gray] (5) edge[gray] (9)
%
(1) edge[cyan!80] (4) edge[cyan!80] (7)
(2) edge[pink] (5) edge[pink] (8)
(3) edge[blue!80] (6) edge[blue!80] (9) % bleudefrance!80
;
\draw[orange!80,dashed]
(6) to (8) edge[out=225, in=220,looseness=2.5] (1);
\draw[dashed]
(2) to (4) edge[out=225, in=225, looseness=2] (9);
\draw[cyan,dashed]
(6) to (2) edge[out=140, in=140, looseness=2.5] (7);
\draw[purple!80,dashed]
(8) to (4) edge[out=140, in=140, looseness=2] (3);
\end{tikzpicture}
\end{figure}
\end{document}
答案2
使用float
包,如以下代码所示:
\documentclass{article}
\usepackage{tikz,calc,lipsum,float}
\begin{document}
\lipsum[1]
\begin{figure}[H]
\begin{tikzpicture}[scale=1.5]
\draw[gray!30] (-3,-3) grid (3,3);
\draw[-latex,blue] (-3.4,0)--(3.4,0) node[right] (x) {\footnotesize $x$};
\draw[-latex,blue] (0,-3.4)--(0,3.4) node[above] (y) {\footnotesize $y$};
\foreach \i in {-3,-2,...,3}{
\foreach \j in {-3,-2,...,3}{
%\pgfmathsetmacro\h{int(\i+\j)};
\filldraw[black] (\i,\j) circle(.8pt);
\node[blue,right] at (\i,\j+.1) (\i) {\tiny (\i,\j)};
}
}
\end{tikzpicture}
\caption{Tikzpcture below text}
\end{figure}
\lipsum[2]
\end{document}
输出: