问题:
在我的图表中,我想用小圆圈代替每个点 a、b、c、d、e、f 和 g 的黑点。此外,我想在圆圈内标记所有这些点。
梅威瑟:
\documentclass[10pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
\draw [line width=0.5pt] (0,4)-- (0,0);
\draw [line width=0.5pt] (0,0)-- (4,0);
\draw [line width=0.5pt] (4,4)-- (4,0);
\draw [line width=0.5pt] (4,4)-- (0,4);
\draw [line width=0.5pt] (4,4)-- (7,2);
\draw [line width=0.5pt] (7,2)-- (4,0);
\draw [line width=0.5pt] (0,4)-- (-3,2);
\draw [line width=0.5pt] (-3,2)-- (0,0);
\draw [line width=0.5pt] (0,4)-- (2,2);
\draw [line width=0.5pt] (2,2)-- (4,4);
\draw [line width=0.5pt] (2,2)-- (4,0);
\draw [line width=0.5pt] (2,2)-- (0,0);
\draw [fill=black] (0,0) circle (1.5pt);
\draw[color=black] (0,-0.35) node {$c$};
\draw [fill=black] (0,4) circle (1.5pt);
\draw[color=black] (-0.02,4.35) node {$b$};
\draw [fill=black] (4,4) circle (1.5pt);
\draw[color=black] (3.98,4.35) node {$e$};
\draw [fill=black] (4,0) circle (1.5pt);
\draw[color=black] (4.0,-0.35) node {$f$};
\draw [fill=black] (7,2) circle (1.5pt);
\draw[color=black] (7.32,2.0) node {$g$};
\draw [fill=black] (-3,2) circle (1.5pt);
\draw[color=black] (-3.32,2.0) node {$a$};
\draw [fill=black] (2,2) circle (1.5pt);
\draw[color=black] (2.0,2.54) node {$d$};
\draw[color=black] (-0.24,2.0) node {$4$};
\draw[color=black] (2,-0.26) node {$2$};
\draw[color=black] (4.26,2.0) node {$3$};
\draw[color=black] (2.0,4.250) node {$7$};
\draw[color=black] (5.56,3.35) node {$10$};
\draw[color=black] (5.56,0.65) node {$12$};
\draw[color=black] (-1.46,3.40) node {$1$};
\draw[color=black] (-1.46,0.65) node {$5$};
\draw[color=black] (0.78,2.9) node {$8$};
\draw[color=black] (3.3,2.9) node {$11$};
\draw[color=black] (3.3,1.0) node {$9$};
\draw[color=black] (0.78,1.10) node {$6$};
\end{tikzpicture}
\end{document}
答案1
您的代码可以简化很多。一种方法是先绘制顶点。这是第一个路径命令所做的。然后,您可能会意识到,绘制所有连接而无需绘制两次的一条可能路径是循环
(c) -- (f) -- (e) -- (g) -- (f) -- (d) -- (b) -- (a) -- (c) -- (b) -- (e) -- (d) -- (c)
可以通过node
在 后面添加 来插入边缘标签--
。这就是\draw
命令的作用。有一些小帮手,例如math
style(改编自matrix of math nodes
style),可以让我们避免$
疯狂地打字。
\documentclass[10pt]{article}
\usepackage{tikz}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[circ/.style={circle,draw,inner sep=1pt,minimum
size=3.4ex},math/.style={execute at begin node=$,execute at end node=$}]
\path[nodes={circ,math}] (0,0) node(c){c}
(4,0) node(f){f}
(4,4) node(e){e}
(7,2) node(g){g}
(0,4) node(b){b}
(-3,2) node(a){a}
(2,2) node(d){d};
\draw[line width=0.5pt,nodes={auto,math}]
(c) -- node{2} (f) -- node{3} (e) -- node{10} (g) -- node{12} (f)
-- node{9} (d) -- node{8} (b) -- node{1} (a) -- node{5} (c)
-- node{4} (b) -- node{7} (e) -- node{11} (d) -- node{6} (c);
\end{tikzpicture}
\end{document}
答案2
这是使用纯 TikZ 的另一种方法。它不像薛定谔的猫的答案那么简洁,但更容易理解。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
[n/.style={circle,draw,minimum size=8mm,inner sep=0}]
\path
(0,0) node[n] (d) {$d$}
(2,2) node[n] (e) {$e$}
(2,-2) node[n] (f) {$f$}
(-2,2) node[n] (b) {$b$}
(-2,-2) node[n] (c) {$c$}
(-5,0) node[n] (a) {$a$}
(5,0) node[n] (g) {$g$};
\draw (a)--(b) node[pos=.5,below right]{$1$};
\draw (b)--(c) node[pos=.5,left]{$4$};
\draw (c)--(d) node[pos=.5,below right]{$6$};
\draw (d)--(e) node[pos=.5,above left]{$11$};
\draw (e)--(f) node[pos=.5,left]{$3$};
\draw (f)--(g) node[pos=.5,below right]{$12$};
\draw (g)--(e) node[pos=.5,above right]{$10$};
\draw (e)--(b) node[pos=.5,above]{$7$};
\draw (a)--(c) node[pos=.5,above right]{$5$};
\draw (c)--(f) node[pos=.5,above]{$2$};
\draw (b)--(d) node[pos=.5,above right]{$8$};
\draw (d)--(f) node[pos=.5,below left]{$9$};
\end{tikzpicture}
\end{document}