我有 4 个图形(以 tikz 编写),我使用 tikz 中的 \matrix 将它们对齐为 2x2 矩阵格式。现在我想在每个图形周围画一个框,并在其各自的框下为每个图形添加标题。我的 tex 代码的格式是
\begin{tikzpicture}
\matrix[draw]
{
<figure-1 details>;
&
<figure-2 details>;
\\
<figure-3 details>;
&
<figure-4 details>;
\\
};
\end{tikzpicture}
图 1 tikz 代码
\coordinate(v3) at (-2.5,0);
\node[left] at (v3) {$v_3$};
\coordinate(v2) at (0,4.3301);
\node[above] at (v2) {$v_2$};
\coordinate(v4) at (2.5,0);
\node[right] at (v4) {$v_4$};
\draw[thick] (v2)--(v3)--(v4)--cycle;
\coordinate(v5) at (0,0);
\node[below] at (v5) {$v_5$};
\draw[thin] (v2)--(v5);
\coordinate(v6) at (-1.25,2.165);
\node[left] at (v6) {$v_6$};
\draw[thin] (v5)--(v6);
\coordinate(v7) at (1.25,2.165);
\node[right] at (v7) {$v_7$};
\draw[thin] (v5)--(v7);
\fill[black] (0,1.4433) circle (2pt) node[anchor=west] {$c_1$};
\fill[black] (-0.833,1.443) circle (2pt) node[anchor=north] {$c_2$};
\fill[black] (0.833,1.443) circle (2pt) node[anchor=north] {$c_3$};
\fill[very nearly transparent] (v2)--(v4)--(v5)--cycle;
除阴影区域的细微变化(即填充了一些其他内三角形)外,其他图形与图 1 相似。
答案1
您可以做的是在每个单元格末尾添加以下内容:
% draws a rectangle around the content of the cell, with some padding
\node [fit=(current bounding box),draw] (a) {};
% add caption below frame
\path
let \p1=(a.west),\p2=(a.east),\n1={\x2-\x1} in
node [below,text width=\n1] at (a.south) {%
\captionof{subfigure}{This is a figure showing etc.}
};
这是假设您希望每个面板下方都有一个编号的子标题。如果您根本不想要任何编号标题,请删除\captionof{subfigure}{
并关闭}
。
如果你想要标题里面框架,交换\node
和 的顺序\path
,并将标题节点放置在current bounding box.south
而不是a.south
。
请注意,在下面的代码中,我还建议在第二个单元格中采用不同的方式绘制图表。
两列的水平对齐方式不同似乎是由于三角形使用的坐标不同造成的。在您的代码中,左下角位于(-2.5,0)
,而在我的代码中位于(0,0)
。第二行的两个十字的左侧均位于 x=0。请注意,第一行的 x=0 与第二行的 x=0 对齐。
换句话说,只要对所有三角形使用相同的坐标,对齐就应该是正确的。
\documentclass[a4paper, 12pt]{article}
\usepackage{tikz}
\usepackage{subcaption} % <-- added
\usetikzlibrary{fit,calc} % <- added
\begin{document}
\begin{figure}
\begin{tikzpicture}[
dot/.style={circle,fill=black,inner sep=0pt,minimum size=4pt} % <-- added
]
\matrix%[draw]
{
\coordinate(v3) at (-2.5,0);
\node[left] at (v3) {$v_3$};
\coordinate(v2) at (0,4.3301);
\node[above] at (v2) {$v_2$};
\coordinate(v4) at (2.5,0);
\node[right] at (v4) {$v_4$};
\draw[thick] (v2)--(v3)--(v4)--cycle;
\coordinate(v5) at (0,0);
\node[below] at (v5) {$v_5$};
\draw[thin] (v2)--(v5);
\coordinate(v6) at (-1.25,2.165);
\node[left] at (v6) {$v_6$};
\draw[thin] (v5)--(v6);
\coordinate(v7) at (1.25,2.165);
\node[right] at (v7) {$v_7$};
\draw[thin] (v5)--(v7);
\fill[black] (0,1.4433) circle (2pt) node[anchor=west] {$c_1$};
\fill[black] (-0.833,1.443) circle (2pt) node[anchor=north] {$c_2$};
\fill[black] (0.833,1.443) circle (2pt) node[anchor=north] {$c_3$};
\fill[very nearly transparent] (v2)--(v4)--(v5)--cycle;
\node [fit=(current bounding box),draw] (a) {};
\path
let \p1=(a.west),\p2=(a.east),\n1={\x2-\x1} in
node [below,text width=\n1] at (a.south) {\captionof{subfigure}{This is a figure showing etc.}};
&
\draw [thick] (0,0) coordinate[label=left:$v_3$] (v3)
-- coordinate[label=below:$v_5$] (v5)
++(0:5) coordinate[label=right:$v_4$] (v4)
-- coordinate[label=right:$v_7$] (v7)
++(120:5) coordinate[label=above:$v_2$] (v2)
-- coordinate[label=left:$v_6$] (v6)
cycle;
\draw (v6) -- node[dot,pos=0.3,label=below:$c_2$] (c2) {} (v5)
-- node[dot,pos=0.7,label=below:$c_3$] (c3) {} (v7);
\node[dot,label=right:$c_1$] at (c2 -| v5) {};
\draw (v2) -- (v5);
\fill[very nearly transparent] (v2)--(v4)--(v5)--cycle;
\node [fit=(current bounding box),draw] (b) {};
\path
let \p1=(b.west),\p2=(b.east),\n1={\x2-\x1} in
node [below,text width=\n1] at (b.south) {\captionof{subfigure}{This is also a figure showing etc.}};
\\
\draw (0,0) -- (3,3) (0,3) -- (3,0);
\node [fit=(current bounding box),draw] (b) {};
\path
let \p1=(b.west),\p2=(b.east),\n1={\x2-\x1} in
node [below,text width=\n1] at (b.south) {\captionof{subfigure}{This is something else.}};
&
\draw (0,0) -- (3,3) (0,3) -- (3,0);
\node [fit=(current bounding box),draw] (b) {};
\path
let \p1=(b.west),\p2=(b.east),\n1={\x2-\x1} in
node [below,text width=\n1] at (b.south) {\captionof{subfigure}{This is something else.}};
\\
};
\end{tikzpicture}
\caption{Caption for the whole thing}
\end{figure}
\end{document}
或者在每个单元格中使用 atabular
和 a tikzpicture
,例如
\documentclass[a4paper, 12pt]{article}
\usepackage{tikz}
\usepackage{subcaption} % <-- added
\usetikzlibrary{fit,calc} % <- added
\newcommand\diagram{% this macro is just to make the example easier to construct, see below
\begin{tikzpicture}[
dot/.style={circle,fill=black,inner sep=0pt,minimum size=4pt}
]
\draw [thick] (0,0) coordinate[label=left:$v_3$] (v3)
-- coordinate[label=below:$v_5$] (v5)
++(0:5) coordinate[label=right:$v_4$] (v4)
-- coordinate[label=right:$v_7$] (v7)
++(120:5) coordinate[label=above:$v_2$] (v2)
-- coordinate[label=left:$v_6$] (v6)
cycle;
\draw (v6) -- node[dot,pos=0.3,label=below:$c_2$] (c2) {} (v5)
-- node[dot,pos=0.7,label=below:$c_3$] (c3) {} (v7);
\node[dot,label=right:$c_1$] at (c2 -| v5) {};
\draw (v2) -- (v5);
\fill[very nearly transparent] (v2)--(v4)--(v5)--cycle;
\node [fit=(current bounding box),draw,minimum size=6cm] (b) {};
\path
let \p1=(b.west),\p2=(b.east),\n1={\x2-\x1} in
node [below,text width=\n1] at (b.south) {\captionof{subfigure}{This is also a figure showing etc.}};
\end{tikzpicture}}
\begin{document}
\begin{figure}
\begin{tabular}{cc}
% I used a macro just for convenience, in your case put in the four
% different tikzpicture environments instead of \diagram
\diagram & \diagram \\
\diagram & \diagram
\end{tabular}
\caption{Caption for the whole thing}
\end{figure}
\end{document}
答案2
如果不看你的代码,很难说出最好的方法是什么,但考虑到你已经在里面有你的代码蒂克兹 \matrix
最简单的方法可能就是添加matrix of nodes
一些\matrix
样式,在每个节点周围放置一个矩形。您可以手动添加标题,也可以通过添加一些样式使其成为矩阵的一部分。前者比较简单,所以我用这种方法做了。
代码如下:
\documentclass[tikz, border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (M)[matrix of nodes, every node/.style={rectangle, draw},
row sep=6mm, column sep=4mm,]
{
figure-1 details
&
figure-2 details
\\
figure-3 details
&
figure-4 details
\\
};
\node at ([yshift=-6]M-1-1.south){\small Caption 1};
\node at ([yshift=-6]M-1-2.south){\small Caption 2};
\node at ([yshift=-6]M-2-1.south){\small Caption 3};
\node at ([yshift=-6]M-2-2.south){\small Caption 4};
\end{tikzpicture}
\end{document}
后面(M)
的\matrix
意思是节点在非空单元格中有标签(M-1-1)
等(M-1-2)
(使用选项,nodes in empty cells
您也可以在空单元格中有节点)。您可以将其更改(M)
为任何您喜欢的内容。