对齐 tikz 节点标签

对齐 tikz 节点标签

我使用 tikz 创建了以下图片:

\tikzset{
    graphnode/.style={
      draw,fill,
      circle,
      minimum size=0.7mm,
      inner sep=0
    },
}

\begin{tikzpicture}[every node/.style={graphnode}]

  \node (b)[label=left:$b$] at (0,0) {};
  \node (g)[label=right:$g$] at (4,0) {};
  \node (r)[label=above:$r$] at (2,3.4641) {};

  \node[label=below:$v$] (n1) at (1.5,0.8) {};
  \node[label=below:$u$] (n2) at (3,0.5) {};
  \node[label=above right:$w$] (n3) at (2.1,2) {};

  \draw(r.center) -- (g.center) -- (b.center) -- cycle;

  \draw (n1.center) -- (b);
  \draw (n2.center) -- (b);
  \draw (n3.center) -- (b);

  \draw (n1.center) -- (n2.center);
  \draw (n2.center) -- (g);
  \draw (n3) -- (g);

  \draw (n1.center) -- (n3.center);
  \draw (n2.center) -- (n3.center);
  \draw (n3.center) -- (r);

\end{tikzpicture}

问题是这两个标签bG未正确对齐。如何在不改变节点位置的情况下对齐它们?

答案1

一种可能性是使用数学支柱:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\tikzset{
    graphnode/.style={
      draw,fill,
      circle,
      minimum size=0.7mm,
      inner sep=0
    },
}

\begin{tikzpicture}[every node/.style={graphnode}]

  \node (b)[label=left:$\mathstrut b$] at (0,0) {};
  \node (g)[label=right:$\mathstrut g$] at (4,0) {};
  \node (r)[label=above:$r$] at (2,3.4641) {};

  \node[label=below:$v$] (n1) at (1.5,0.8) {};
  \node[label=below:$u$] (n2) at (3,0.5) {};
  \node[label=above right:$w$] (n3) at (2.1,2) {};

  \draw(r.center) -- (g.center) -- (b.center) -- cycle;

  \draw (n1.center) -- (b);
  \draw (n2.center) -- (b);
  \draw (n3.center) -- (b);

  \draw (n1.center) -- (n2.center);
  \draw (n2.center) -- (g);
  \draw (n3) -- (g);

  \draw (n1.center) -- (n3.center);
  \draw (n2.center) -- (n3.center);
  \draw (n3.center) -- (r);

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容