我已经使用 tikz 绘制了几个图形。现在我得到的是顶点内部的顶点标签。但我想要的是顶点外部的顶点标签。
另外,我想在一行中绘制两个 tickz 图片。请帮忙。我正在使用以下代码。
\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{caption}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{scope}[every node/.style={fill=white,circle,thick,draw}]
\node(A) at (1,1) {1};
\node(B) at (0,0) {2};
\node(C) at (1,-1) {3};
\end{scope}
\begin{scope}
[every edge/.style={draw=black,very thick}]
\path[-](A)edge node {} (B);
\path[-](B) edge node {} (C);
\end{scope}
\end{tikzpicture}
\caption*{Figure 1}
\end{figure}
\medskip
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{scope}[every node/.style={fill=white,circle,thick,draw}]
\node(A) at (0,0) {2};
\node(B) at (-1,1) {4};
\node(C) at (1,1) {6};
\node(D) at (1,-1) {8};
\node(E) at (-1,-1) {10};
\end{scope}
\begin{scope}
[every edge/.style={draw=black,very thick}]
\path[-](A)edge node {} (B);
\path[-](A) edge node {} (C);
\path[-](A) edge node {} (D);
\path[-](A) edge node {} (E);
\end{scope}
\end{tikzpicture}
\caption*{Figure 2}
\end{figure}
\end{document}
答案1
另一种选择是基于假设,您希望两个图形并排而不是子图形:
\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{caption}
\usepackage{tabularx}
\begin{document}
\begin{figure}[h]
\centering
\begin{tabularx}{0.8\textwidth}{*{2}{>{\centering\arraybackslash}X}}
\begin{tikzpicture}[
every edge/.style = {draw=black,very thick},
vrtx/.style args = {#1/#2}{%
circle, draw, thick, fill=white,
minimum size=5mm, label=#1:#2}
]
\node(A) [vrtx=left/1] at (1, 1) {};
\node(B) [vrtx=left/2] at (0, 0) {};
\node(C) [vrtx=left/3] at (1,-1) {};
%
\path (A) edge (B)
(B) edge (C);
\end{tikzpicture}
\caption*{Figure 1}
&
\begin{tikzpicture}[
every edge/.style = {draw=black,very thick},
vrtx/.style args = {#1/#2}{%
circle, draw, thick, fill=white,
minimum size=5mm, label=#1:#2}
]
\node (A) [vrtx=left/2] at ( 0, 0) {};
\node (B) [vrtx=left/4] at (-1, 1) {};
\node (C) [vrtx=right/6] at ( 1, 1) {};
\node (D) [vrtx=right/8] at ( 1,-1) {};
\node (E) [vrtx=left/10] at (-1,-1) {};
%
\path (A) edge (B)
(A) edge (C)
(A) edge (D)
(A) edge (E);
\end{tikzpicture}
\caption*{Figure 2}
\end{tabularx}
\end{figure}
\end{document}
我还稍微修改了代码,使其更简洁易用(省略了scope
定义的节点,以便简单设置它们的标签,省略了未使用的节点和边。数字放在tabularx
表格中。
附录: 如果你想将一行中的图形组织为子图形:
\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{figure}[h]
\centering
\begin{subfigure}[b]{0.4\textwidth}\centering
\begin{tikzpicture}[
every edge/.style = {draw=black,very thick},
vrtx/.style args = {#1/#2}{%
circle, draw, thick, fill=white,
minimum size=5mm, label=#1:#2}
]
\node(A) [vrtx=left/1] at (1, 1) {};
\node(B) [vrtx=left/2] at (0, 0) {};
\node(C) [vrtx=left/3] at (1,-1) {};
%
\path (A) edge (B)
(B) edge (C);
\end{tikzpicture}
\caption{}
\end{subfigure}
\hfil
\begin{subfigure}[b]{0.4\textwidth}\centering
\begin{tikzpicture}[
every edge/.style = {draw=black,very thick},
vrtx/.style args = {#1/#2}{%
circle, draw, thick, fill=white,
minimum size=5mm, label=#1:#2}
]
\node (A) [vrtx=left/2] at ( 0, 0) {};
\node (B) [vrtx=left/4] at (-1, 1) {};
\node (C) [vrtx=right/6] at ( 1, 1) {};
\node (D) [vrtx=right/8] at ( 1,-1) {};
\node (E) [vrtx=left/10] at (-1,-1) {};
%
\path (A) edge (B)
(A) edge (C)
(A) edge (D)
(A) edge (E);
\end{tikzpicture}
\caption{}
\end{subfigure}
\caption{Common caption}
\label{fig:figure-3}
\end{figure}
\end{document}
在上面 MWE 中使用了subfigure
来自包的环境subcaption
。
答案2
\documentclass[11pt]{article}
\usepackage{fontspec} % lualatex engine
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{figure}[!h]
\centering
\begin{minipage}[t]{.45\linewidth}
\centering
\begin{tikzpicture}[every edge/.style = {draw,very thick},
vertex/.style args = {#1 #2}{circle,
draw, thick,
minimum size=5mm,
label=#1:#2}]
\path node(A) [vertex=left 1] at (1, 1) {}
node(B) [vertex=left 2] at (0, 0) {}
node(C) [vertex=left 3] at (1,-1) {}
(A) edge (B)
(B) edge (C);
\end{tikzpicture}
\caption{graph}
\end{minipage}
\begin{minipage}[t]{.45\linewidth}
\centering
\begin{tikzpicture}[every edge/.style = {draw,very thick},
vertex/.style args = {#1 #2}{circle,
draw,
thick,
minimum size=5mm,
label=#1:#2}]
\path node (A) [vertex=left 2] at ( 0, 0) {}
node (B) [vertex=left 4] at (-1, 1) {}
node (C) [vertex=right 6] at ( 1, 1) {}
node (D) [vertex=right 8] at ( 1,-1) {}
node (E) [vertex =left 10] at (-1,-1) {}
(A) edge (B)
(A) edge (C)
(A) edge (D)
(A) edge (E);
\end{tikzpicture}
\caption{graph bis}
\end{minipage}
\end{figure}
\end{document}