我是 tizk 新手。我有以下代码:
\begin{minipage}[c]{0.5\textwidth}
\begin{tikzpicture}
\begin{pgfonlayer}{foreground}
%links from central node
\draw[line width=1pt, color=cyan] (5,-5) to (7,-5);
\draw[line width=1pt, color=cyan] (5,-5) to (5,-3);
\draw[line width=1pt, color=cyan] (5,-5) to (5,-7);
\draw[line width=1pt, color=cyan] (5,-5) to (3,-5);
\draw[line width=1pt, color=cyan] (5,-5) to (3,-1);
\draw[line width=1pt, color=cyan] (5,-5) to (0,-1);
%some other links
\draw[line width=1pt, color=cyan] (5,-3) to (0,-1);
\draw[line width=1pt, color=cyan] (5,-3) to (3,-1);
\draw[line width=1pt, color=cyan] (0,-1) to (3,-5);
\draw[line width=1pt, color=cyan] (5,-7) to (3,-5);
%nodes
\draw[color=red, fill=red] (5, -5) circle (0.5);
\draw[color=cyan, fill=cyan] (7, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (0, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -3) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -7) circle (0.3);
\end{pgfonlayer}
\begin{pgfonlayer}{background}
\draw[fill=black, rounded corners] (-1, 0) rectangle (8, -8);
\end{pgfonlayer}
\end{tikzpicture}
\end{minipage}
%right side
\begin{minipage}[c]{0.5\textwidth}
\begin{tikzpicture}
\begin{pgfonlayer}{foreground}
%links from central node
\draw[line width=1pt, color=cyan] (5,-5) to (7,-5);
%some other links
\draw[line width=1pt, color=cyan] (5,-3) to (0,-1);
\draw[line width=1pt, color=cyan] (5,-3) to (3,-1);
\draw[line width=1pt, color=cyan] (0,-1) to (3,-5);
\draw[line width=1pt, color=cyan] (3,-5) to (5,-7);
\draw[line width=1pt, color=cyan] (5,-7) to (3,-1);
\draw[line width=1pt, color=cyan] (3,-1) to (0,-1);
\draw[line width=1pt, color=cyan] (7,-5) to (5,-3);
%nodes
\draw[color=red, fill=red] (5, -5) circle (0.5);
\draw[color=cyan, fill=cyan] (7, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (0, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -3) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -7) circle (0.3);
\end{pgfonlayer}
%background
\begin{pgfonlayer}{background}
\draw[fill=black, rounded corners] (-1, 0) rectangle (8, -8);
\end{pgfonlayer}
\end{tikzpicture}
\end{minipage}
输出结果如下:
首先,我不明白为什么一个图形在另一个图形下面。这是为什么?我以为{0.5\textwidth}
它们可以并排。
其次,我想给每张 tikzpicture 加上标签和标题。我发现这个答案但它在 minipage 中不起作用。有人知道其他方法吗?
答案1
您的代码中存在更多问题:
- 你没有提供代码的序言:从你的代码中重建它是不合适的
- 你的图片太大了,两张图不能放在一行,而且不能重叠
- 离题:你的代码太复杂了
如果不缩放图像尺寸,您的代码将生成:
并进行缩放0.8
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,%
main,%
foreground%
}
\begin{document}
\begin{figure}
\begin{subfigure}{0.45\textwidth}
\begin{tikzpicture}[scale=0.8]
\begin{pgfonlayer}{foreground}
%links from central node
\draw[line width=1pt, color=cyan] (5,-5) to (7,-5);
\draw[line width=1pt, color=cyan] (5,-5) to (5,-3);
\draw[line width=1pt, color=cyan] (5,-5) to (5,-7);
\draw[line width=1pt, color=cyan] (5,-5) to (3,-5);
\draw[line width=1pt, color=cyan] (5,-5) to (3,-1);
\draw[line width=1pt, color=cyan] (5,-5) to (0,-1);
%some other links
\draw[line width=1pt, color=cyan] (5,-3) to (0,-1);
\draw[line width=1pt, color=cyan] (5,-3) to (3,-1);
\draw[line width=1pt, color=cyan] (0,-1) to (3,-5);
\draw[line width=1pt, color=cyan] (5,-7) to (3,-5);
%nodes
\draw[color=red, fill=red] (5, -5) circle (0.5);
\draw[color=cyan, fill=cyan] (7, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (0, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -3) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -7) circle (0.3);
\end{pgfonlayer}
\begin{pgfonlayer}{background}
\draw[fill=black, rounded corners] (-1, 0) rectangle (8, -8);
\end{pgfonlayer}
\end{tikzpicture}
\caption{left figure}
\end{subfigure}
\hfill
\begin{subfigure}{0.45\textwidth}
\begin{tikzpicture}[scale=0.8]
\begin{pgfonlayer}{foreground}
%links from central node
\draw[line width=1pt, color=cyan] (5,-5) to (7,-5);
%some other links
\draw[line width=1pt, color=cyan] (5,-3) to (0,-1);
\draw[line width=1pt, color=cyan] (5,-3) to (3,-1);
\draw[line width=1pt, color=cyan] (0,-1) to (3,-5);
\draw[line width=1pt, color=cyan] (3,-5) to (5,-7);
\draw[line width=1pt, color=cyan] (5,-7) to (3,-1);
\draw[line width=1pt, color=cyan] (3,-1) to (0,-1);
\draw[line width=1pt, color=cyan] (7,-5) to (5,-3);
%nodes
\draw[color=red, fill=red] (5, -5) circle (0.5);
\draw[color=cyan, fill=cyan] (7, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (0, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -3) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -7) circle (0.3);
\end{pgfonlayer}
%background
\begin{pgfonlayer}{background}
\draw[fill=black, rounded corners] (-1, 0) rectangle (8, -8);
\end{pgfonlayer}
\end{tikzpicture}
\caption{right figure}
\end{subfigure}
\caption{My figures}
\label{fig:myfigure}
\end{figure}
\end{document}
附录 偏离主题您的代码可以明显更短...看看以下解决方案是否对您有用:
- 节点中的线针对两个图像确定
\tikzset
- 圆圈被绘制为节点
- 确定的是节点和线的默认样式
- 黑色背景被绘制为所有其他节点的节点
您的代码将变成:
\documentclass{article}
\usepackage{geometry}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, scopes}
\begin{document}
\begin{figure}[htb]
\tikzset{
C/.style args = {#1/#2}{shape=circle,
fill=#1, minimum size=#2},% C as circle ...
L/.style = {line width=1pt, color=#1},% L as line
C/.default = cyan/5mm,
L/.default = cyan
}
\begin{subfigure}{0.45\textwidth}
\begin{tikzpicture}[scale=0.8]
% nodes
\node (n1) [C=red/8mm] at (5, -5) {};
\node (n2) [C] at (7, -5) {};
\node (n3) [C] at (0, -1) {};
\node (n4) [C] at (5, -3) {};
\node (n5) [C] at (3, -1) {};
\node (n6) [C] at (5, -7) {};
\node (n7) [C] at (3, -5) {};
% links from central node
\draw[L] (n1) -- (n2) (n1) -- (n4) (n1) -- (n6)
(n1) -- (n7) (n1) -- (n5) (n1) -- (n3)
% other links
(n4) -- (n3) (n4) -- (n5) (n3) -- (n7)
(n6) -- (n7);
% background
\scoped[on background layer]
\node[fill=black, rounded corners, inner sep=3mm,
fit= (n2) (n3) (n6)] {};
\end{tikzpicture}
\caption{left figure}
\end{subfigure}%
\hfill
\begin{subfigure}{0.45\textwidth}
\begin{tikzpicture}[scale=0.8]
% nodes
\node (n1) [C=red/8mm] at (5, -5) {};
\node (n2) [C] at (7, -5) {};
\node (n3) [C] at (0, -1) {};
\node (n4) [C] at (5, -3) {};
\node (n5) [C] at (3, -1) {};
\node (n6) [C] at (3, -5) {};
\node (n7) [C] at (5, -7) {};
% links from central node
\draw[L] (n1) -- (n2)
% some other links
(n4) -- (n3) (n4) -- (n2) (n3) -- (n6)
(n6) -- (n7) (n7) -- (n5) (n5) -- (n3);
(n2) -- (n4);
% background
\scoped[on background layer]
\node[fill=black, rounded corners, inner sep=3mm,
fit= (n2) (n3) (n7)] {};
\end{tikzpicture}
\caption{right figure}
\end{subfigure}
\caption{My figures}
\label{fig:myfigure}
\end{figure}
\end{document}
答案2
一半的问题已经在评论中得到回答,即:%
在小页面之间使用,以避免小页面之间出现行和段落中断。
要获取子标题,请使用subcaption
包。它同时提供\subcaption{}
命令和subfigure
环境。在 subfigure 环境中,您可以使用普通的\caption
cmd,它的行为类似于\subcaption
,而在 minipages 中,请使用\subcaption
。
关于 Ti钾对于新手部分,我建议阅读前几节“给卡尔的一幅画”和其他部分,它们既有趣又有教育意义。我在绘图方面做了一些改进,使用样式和节点使其更加灵活。
\documentclass{article}
\usepackage{tikz,subcaption}
\usetikzlibrary{backgrounds}
\usepackage[margin=0.5cm]{geometry}
\begin{document}
\vspace*{1cm}
Using the referencing mechanism: Subfigure \ref{asubfig}, Subfigure \ref{anothersubfig} and Figure \ref{thewholefig}
\begin{figure}[h]
\centering
\begin{minipage}[c]{0.5\textwidth}
\begin{tikzpicture}[links/.style={cyan, minimum size=0.6cm, circle, inner sep=0pt, fill, draw},
special/.style={red, minimum size=1cm, circle, inner sep=0pt, draw, fill}]
%nodes
\node[special] at (5, -5) (sp) {};
\begin{scope}[every node/.style={links}]
\node at (7, -5) (a) {};
\node at (0, -1) (b) {};
\node at (5, -3) (c) {};
\node at (3, -1) (d) {};
\node at (3, -5) (e) {};
\node at (5, -7) (f) {};
\end{scope}
\foreach \i in {a,...,f} \draw[cyan, line width=1pt] (sp) -- (\i) node[black]{\i};
\draw[cyan, line width=1pt] (f) -- (e) -- (b) -- (c) -- (d);
\begin{scope}[on background layer]
\fill[black, rounded corners] (-1, 0) rectangle (8, -8);
\end{scope}
\end{tikzpicture}
\subcaption{A subfigure\label{asubfig}}
\end{minipage}%
%
\begin{minipage}[c]{0.5\textwidth}
\begin{tikzpicture}[links/.style={cyan, minimum size=0.6cm, circle, inner sep=0pt, fill, draw},
special/.style={red, minimum size=1cm, circle, inner sep=0pt, draw, fill}]
%nodes
\node[special] at (5, -5) (sp) {};
\begin{scope}[every node/.style={links}]
\node at (7, -5) (a) {};
\node at (0, -1) (b) {};
\node at (5, -3) (c) {};
\node at (3, -1) (d) {};
\node at (3, -5) (e) {};
\node at (5, -7) (f) {};
\end{scope}
\draw[cyan, line width=1pt] (d) -- (f) -- (e) -- (b) -- (d) -- (c) -- (a) -- (sp) (c) -- (b);
\begin{scope}[on background layer]
\fill[black, rounded corners] (-1, 0) rectangle (8, -8);
\end{scope}
\end{tikzpicture}
\subcaption{Another subfigure\label{anothersubfig}}
\end{minipage}
\caption{The whole figure\label{thewholefig}}
\end{figure}
\end{document}