\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.3\textwidth}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (0,0) grid (1.5,3.5);
% Row Headers
\node at (-0.25,+3.25) {$r_0$};
\node at (-0.25,+2.75) {$r_1$};
\node at (-0.25,+2.25) {$r_2$};
\node at (-0.25,+1.75) {$r_3$};
\node at (-0.25,+1.25) {$r_4$};
\node at (-0.25,+0.75) {$r_5$};
\node at (-0.25,+0.25) {$r_6$};
% Column 1
\node at (+0.25,+3.75) {$c_1$};
\node at (+0.25,+3.25) {0};
\node at (+0.25,+2.75) {1};
\node at (+0.25,+2.25) {2};
\node at (+0.25,+1.75) {3};
\node at (+0.25,+1.25) {4};
\node at (+0.25,+0.75) {5};
\node at (+0.25,+0.25) {6};
% Column 2
\node at (+0.75,+3.75) {$c_2$};
\node at (+0.75,+3.25) {7};
\node at (+0.75,+2.75) {8};
\node at (+0.75,+2.25) {9};
\node at (+0.75,+1.75) {10};
\node at (+0.75,+1.25) {11};
\node at (+0.75,+0.75) {12};
\node at (+0.75,+0.25) {13};
% Column 3
\node at (+1.25,+3.75) {$c_3$};
\node at (+1.25,+3.25) {14};
\node at (+1.25,+2.75) {15};
\node at (+1.25,+2.25) {16};
\node at (+1.25,+1.75) {17};
\node at (+1.25,+1.25) {18};
\node at (+1.25,+0.75) {19};
\node at (+1.25,+0.25) {20};
\end{tikzpicture}
\end{subfigure}
% -- No Swap
\begin{subfigure}[b]{0.3\textwidth}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (0,0) grid (1.5,3.5);
% Row Headers
\node at (-0.25,+3.25) {$r_0$};
\node at (-0.25,+2.75) {$r_1$};
\node at (-0.25,+2.25) {$r_2$};
\node at (-0.25,+1.75) {$r_3$};
\node at (-0.25,+1.25) {$r_4$};
\node at (-0.25,+0.75) {$r_5$};
\node at (-0.25,+0.25) {$r_6$};
% Column 1
\node at (+0.25,+3.75) {$c_1$};
\node at (+0.25,+3.25) {0};
\node at (+0.25,+2.75) {3};
\node at (+0.25,+2.25) {6};
\node at (+0.25,+1.75) {9};
\node at (+0.25,+1.25) {12};
\node at (+0.25,+0.75) {15};
\node at (+0.25,+0.25) {18};
% Column 2
\node at (+0.75,+3.75) {$c_2$};
\node at (+0.75,+3.25) {1};
\node at (+0.75,+2.75) {4};
\node at (+0.75,+2.25) {7};
\node at (+0.75,+1.75) {10};
\node at (+0.75,+1.25) {13};
\node at (+0.75,+0.75) {16};
\node at (+0.75,+0.25) {19};
% Column 3
\node at (+1.25,+3.75) {$c_3$};
\node at (+1.25,+3.25) {2};
\node at (+1.25,+2.75) {5};
\node at (+1.25,+2.25) {8};
\node at (+1.25,+1.75) {11};
\node at (+1.25,+1.25) {14};
\node at (+1.25,+0.75) {17};
\node at (+1.25,+0.25) {20};
\end{tikzpicture}
\end{subfigure}
\end{figure}
\end{document}
我希望这两个网格并排,而不是彼此重叠。该怎么做?
答案1
空行被解释为段落分隔符,因此删除子图之间的空行会将它们放在同一行。
\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.3\textwidth}
\centering
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (0,0) grid (1.5,3.5);
% Row Headers
\node at (-0.25,+3.25) {$r_0$};
\node at (-0.25,+2.75) {$r_1$};
\node at (-0.25,+2.25) {$r_2$};
\node at (-0.25,+1.75) {$r_3$};
\node at (-0.25,+1.25) {$r_4$};
\node at (-0.25,+0.75) {$r_5$};
\node at (-0.25,+0.25) {$r_6$};
% Column 1
\node at (+0.25,+3.75) {$c_1$};
\node at (+0.25,+3.25) {0};
\node at (+0.25,+2.75) {1};
\node at (+0.25,+2.25) {2};
\node at (+0.25,+1.75) {3};
\node at (+0.25,+1.25) {4};
\node at (+0.25,+0.75) {5};
\node at (+0.25,+0.25) {6};
% Column 2
\node at (+0.75,+3.75) {$c_2$};
\node at (+0.75,+3.25) {7};
\node at (+0.75,+2.75) {8};
\node at (+0.75,+2.25) {9};
\node at (+0.75,+1.75) {10};
\node at (+0.75,+1.25) {11};
\node at (+0.75,+0.75) {12};
\node at (+0.75,+0.25) {13};
% Column 3
\node at (+1.25,+3.75) {$c_3$};
\node at (+1.25,+3.25) {14};
\node at (+1.25,+2.75) {15};
\node at (+1.25,+2.25) {16};
\node at (+1.25,+1.75) {17};
\node at (+1.25,+1.25) {18};
\node at (+1.25,+0.75) {19};
\node at (+1.25,+0.25) {20};
\end{tikzpicture}
\caption{My first Ti$k$Z table}
\end{subfigure}
%
\begin{subfigure}[b]{0.3\textwidth}
\centering
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (0,0) grid (1.5,3.5);
% Row Headers
\node at (-0.25,+3.25) {$r_0$};
\node at (-0.25,+2.75) {$r_1$};
\node at (-0.25,+2.25) {$r_2$};
\node at (-0.25,+1.75) {$r_3$};
\node at (-0.25,+1.25) {$r_4$};
\node at (-0.25,+0.75) {$r_5$};
\node at (-0.25,+0.25) {$r_6$};
% Column 1
\node at (+0.25,+3.75) {$c_1$};
\node at (+0.25,+3.25) {0};
\node at (+0.25,+2.75) {3};
\node at (+0.25,+2.25) {6};
\node at (+0.25,+1.75) {9};
\node at (+0.25,+1.25) {12};
\node at (+0.25,+0.75) {15};
\node at (+0.25,+0.25) {18};
% Column 2
\node at (+0.75,+3.75) {$c_2$};
\node at (+0.75,+3.25) {1};
\node at (+0.75,+2.75) {4};
\node at (+0.75,+2.25) {7};
\node at (+0.75,+1.75) {10};
\node at (+0.75,+1.25) {13};
\node at (+0.75,+0.75) {16};
\node at (+0.75,+0.25) {19};
% Column 3
\node at (+1.25,+3.75) {$c_3$};
\node at (+1.25,+3.25) {2};
\node at (+1.25,+2.75) {5};
\node at (+1.25,+2.25) {8};
\node at (+1.25,+1.75) {11};
\node at (+1.25,+1.25) {14};
\node at (+1.25,+0.75) {17};
\node at (+1.25,+0.25) {20};
\end{tikzpicture}
\caption{My second Ti$k$Z table}
\end{subfigure}
\end{figure}
\end{document}
答案2
为什么要使用 TikZ 来实现这一点?
简单tabular
就够了。
这里我使用tabularray
:
\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}
\NewTblrEnviron{mytab}
\SetTblrInner[mytab]{
columns={.5cm, colsep=0pt, mode=math},
rows={.5cm, rowsep=0pt},
cells={c,m},
stretch=0,
hline{2-Z} = {2-Z}{gray},
vline{2-Z} = {2-Z}{gray},
}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.3\textwidth}
\centering
\begin{mytab}{}
& c_1 & c_2 & c_3 \\
r_0 & 0 & 7 & 14 \\
r_1 & 1 & 8 & 15 \\
r_2 & 2 & 9 & 16 \\
r_3 & 3 & 10 & 17 \\
r_4 & 4 & 11 & 18 \\
r_5 & 5 & 12 & 19 \\
r_6 & 6 & 13 & 20 \\
\end{mytab}
\caption{My first Ti\emph{k}Z table}
\end{subfigure}
%
\begin{subfigure}[b]{0.3\textwidth}
\centering
\begin{mytab}{}
& c_1 & c_2 & c_3 \\
r_0 & 0 & 1 & 2 \\
r_1 & 3 & 4 & 5 \\
r_2 & 6 & 7 & 8 \\
r_3 & 9 & 10 & 11 \\
r_4 & 12 & 13 & 14 \\
r_5 & 15 & 16 & 17 \\
r_6 & 18 & 19 & 20 \\
\end{mytab}
\caption{My second Ti\emph{k}Z table}
\end{subfigure}
\end{figure}
\end{document}
题外话:不要使用$...$
斜体字,它只用于数学模式。请改用\emph{...}
。
答案3
LaTeX 将输出文档中的新行转换为空格,将空行转换为实际的新行,或者说是新段落。对于您的情况,删除空行,您将获得彼此相邻的图像。
几点说明。如果您想继续使用tikz
,请考虑使用matrix
库和样式来避免过多的代码和重复。另外,请注意subcaption
包会加载caption
,无需明确加载。
\documentclass{article}
\usepackage{tikz}
\usepackage{subcaption}
\usetikzlibrary{matrix}
\tikzset{
grid/.style = {
draw,
color = gray!50,
text = black,
inner sep=0pt,
outer sep=0pt,
minimum size = 7mm,
anchor = center,
},
mstyle/.style = {
matrix of math nodes,
column sep = -\pgflinewidth,
row sep = -\pgflinewidth,
nodes = {grid},
column 1/.style = {nodes={draw=none}},
row 1/.style = {nodes={draw=none}},
},
}
\subcaptionsetup{format=hang}
\begin{document}
\begin{figure}
\AddToHook{env/tikzpicture/before}{\centering}
\centering
\begin{subfigure}{0.3\textwidth}
\begin{tikzpicture}
\matrix[mstyle] {
& c_1 & c_2 & c_3 \\
r_0 & 0 & 7 & 14 \\
r_1 & 1 & 8 & 15 \\
r_2 & 2 & 9 & 16 \\
r_3 & 3 & 10 & 17 \\
r_4 & 4 & 11 & 18 \\
r_5 & 5 & 12 & 19 \\
r_6 & 6 & 13 & 20 \\
};
\end{tikzpicture}
\caption{Caption for the first image}
\end{subfigure}%
\hfil
\begin{subfigure}{0.3\textwidth}
\begin{tikzpicture}
\matrix[mstyle] {
& c_1 & c_2 & c_3 \\
r_0 & 0 & 1 & 2 \\
r_1 & 3 & 4 & 5 \\
r_2 & 6 & 7 & 8 \\
r_3 & 9 & 10 & 11 \\
r_4 & 12 & 13 & 14 \\
r_5 & 15 & 16 & 17 \\
r_6 & 18 & 18 & 20 \\
};
\end{tikzpicture}
\caption{Caption for the second image}
\end{subfigure}
\end{figure}
\end{document}