我在使用 LaTeX 时遇到了问题。我必须在同一张图片中并排放置 tikzpicture 和表格(在环境表格中),并使用相同的标题。
以下是我的代码:
\begin{figure}
\centering
\begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}[inner sep=0.6mm, placew/.style={circle,draw=black!100,fill=white!100,thick}]
\node (v1) at (-1,-1) [placew,label=270:{\footnotesize$v_1$}] {};
\node (v2) at (1,-1) [placew,label=270:{\footnotesize$v_2$}] {};
\node (v3) at (1,1) [placew,label=90:{\footnotesize$v_3$}] {};
\node (v4) at (-1,1) [placew,label=45:{\footnotesize$v_4$}] {};
\node (v5) at (-1,2) [placew,label=45:{\footnotesize$v_5$}] {};
\draw[thick] (v1) -- (v2);
\draw[thick] (v2) -- (v3);
\draw[thick] (v3) -- (v4);
\draw[thick] (v1) -- (v4);
\draw[thick] (v4) -- (v5);
\end{tikzpicture}
\end{subfigure}%
\begin{subfigure}[b]{0.6\textwidth}
\centering
\begin{tabular}{| l | l | l | l | l | l | p{0.2cm} |}
\hline
& $v_1$ & $v_2$ & $v_3$ & $v_4$ & $v_5$ \\ \hline
$v_1$ & 0 & 1 & 0 & 1 & 0 \\ \hline
$v_2$ & 1 & 0 & 1 & 0 & 0 \\ \hline
$v_3$ & 0 & 1 & 0 & 1 & 0 \\ \hline
$v_4$ & 1 & 0 & 1 & 0 & 1 \\ \hline
$v_5$ & 0 & 0 & 0 & 1 & 0 \\ \hline
\end{tabular}
\end{subfigure}
\caption{The Graph of Example \ref{RemAttr2} and its Adjacence Matrix.}\label{fig:Graph1}
\end{figure}
但是,LaTeX 报告以下错误:
! 缺失数字,视为零。} l.1298 \begin{subfigure}[b]{0.5\textwidth}
如何解决?
答案1
我猜,你关注了以下这些:
\documentclass{article}
\usepackage{caption}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[baseline,
inner sep=0.6mm,
placew/.style={circle,draw=black!100,fill=white!100,thick}
]
\node (v1) at (-1,-1) [placew,label=270:{\footnotesize$v_1$}] {};
\node (v2) at (1,-1) [placew,label=270:{\footnotesize$v_2$}] {};
\node (v3) at (1,1) [placew,label=90:{\footnotesize$v_3$}] {};
\node (v4) at (-1,1) [placew,label=45:{\footnotesize$v_4$}] {};
\node (v5) at (-1,2) [placew,label=45:{\footnotesize$v_5$}] {};
\draw[thick] (v1) -- (v2);
\draw[thick] (v2) -- (v3);
\draw[thick] (v3) -- (v4);
\draw[thick] (v1) -- (v4);
\draw[thick] (v4) -- (v5);
\end{tikzpicture}
\hfil
\begin{tabular}{| l | l | l | l | l | l | p{0.2cm} |}
\hline
& $v_1$ & $v_2$ & $v_3$ & $v_4$ & $v_5$ \\ \hline
$v_1$ & 0 & 1 & 0 & 1 & 0 \\ \hline
$v_2$ & 1 & 0 & 1 & 0 & 0 \\ \hline
$v_3$ & 0 & 1 & 0 & 1 & 0 \\ \hline
$v_4$ & 1 & 0 & 1 & 0 & 1 \\ \hline
$v_5$ & 0 & 0 & 0 & 1 & 0 \\ \hline
\end{tabular}
\caption{The Graph of Example \ref{RemAttr2} and its Adjacence Matrix.}\label{fig:Graph1}
\end{figure}
\end{document}
附录:
使用tblr
表格并重绘tikzpicture
:
\documentclass{article}
\usepackage{caption}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{tabularray}
\UseTblrLibrary{amsmath,booktabs}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[baseline,
node distance = 17mm and 17mm, on grid,
C/.style = {circle, draw, thick, inner sep=1pt,
label = #1,
node contents = {}},
every label/.append style = {font=\footnotesize}
]
\node (v1) [C=270:$v_1$];
\node (v2) [C=270:$v_2$, right=of v1];
\node (v3) [C= 90:$v_3$, above=of v2];
\node (v4) [C= 45:$v_4$, above=of v1];
\node (v5) [C= 45:$v_5$, above=of v4];
\draw[thick] (v1) -- (v2) -- (v3) -- (v4) -- (v5)
(v1) -- (v4);
\end{tikzpicture}
\hfil
\begin{tblr}[b]{hlines, vlines,
colspec = { *{6}{Q[c, wd=2em, mode=math]} }
}
& v_1 & v_2 & v_3 & v_4 & v_5 \\
v_1 & 0 & 1 & 0 & 1 & 0 \\
v_2 & 1 & 0 & 1 & 0 & 0 \\
v_3 & 0 & 1 & 0 & 1 & 0 \\
v_4 & 1 & 0 & 1 & 0 & 1 \\
v_5 & 0 & 0 & 0 & 1 & 0 \\
\end{tblr}
\caption{The Graph of Example \ref{RemAttr2} and its Adjacence Matrix.}
\label{fig:Graph1}
\end{figure}
\end{document}