如何将文本定位在节点边缘旁边?

如何将文本定位在节点边缘旁边?

我有以下 MWE。我想更改它,以便字母序列文本出现在边缘三角形,就像 abcde 出现在边缘下方一样,其他文本应该旋转 60 度出现在另外两个边缘旁边。

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{positioning}


 \tikzset{
    buffer/.style={
        draw,
        regular polygon,
        regular polygon sides=3,
        fill=yellow,
        node distance=4cm,
        minimum height=10em
    }
}


\usetikzlibrary{shapes.geometric}

\usetikzlibrary{calc,shadings,patterns}


\begin{document}

\begin{tikzpicture}


% 90 degrees rotation
\node[buffer] (a) {middle part};

 \node[below=0pt of a] {abcdef};
         \node[below=10pt of a,left=20pt of a] {ghijkl};
         \node[below=10pt of a,right=20pt of a] {lmnlopq};

\end{tikzpicture}

\end{document}

答案1

这取决于(并且对于其他多边形来说很有趣)但这里有两种相当手动的方法。

代码

\documentclass{article}
\usepackage{tikz}
 \tikzset{
  buffer/.style={
    draw,
    regular polygon,
    regular polygon sides=3,
    fill=yellow,
    node distance=4cm,
    minimum height=10em}}
\usetikzlibrary{shapes.geometric}
\begin{document}
\tikz
\node[buffer,
  label={side 2:abcdef},
  label={[rotate=-60, anchor=south]side 3:ghijkl},
  label={[rotate= 60, anchor=south]side 1:mnopqr},
] (a) {middle part};

\tikz
\node[buffer] (a) {middle part}
  foreach[remember=\c as \prevC (initially 3), count=\c] \t/\s in {ghjikl/, mnopqr/, abcdef/below}{
    (a.corner \prevC) -- node[sloped, above, \s]{\t} (a.corner \c)
  };
\end{document}

输出

在此处输入图片描述

答案2

使用边引号(因此quotes使用库)并sloped在三角形正多边形形状周围的路径上使用选项:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{quotes,
                shapes.geometric}
 \tikzset{
    buffer/.style = {regular polygon, regular polygon sides=3,
                    draw, fill=yellow, 
                    inner sep=0pt, minimum size=6cm}
                    % from geometry follows: border length = <minimum size>*sqrt(3)/2
            }


\begin{document}
    \begin{tikzpicture}[
every edge quotes/.append style = {auto, sloped, text=blue}
                        ]
\node[buffer] (a) {middle part};

\path   (a.corner 1)    to ["abcdef"]   (a.corner 2)
                        to ["ghijkl" '] (a.corner 3)
                        to ["mnopqr"]   (a.corner 1);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

这是一种更直观的方法。请注意,一些锚点处于不寻常的位置。

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{positioning}


 \tikzset{
    buffer/.style={
        draw,
        regular polygon,
        regular polygon sides=3,
        fill=yellow,
        node distance=4cm,
        minimum height=10em
    }
}


\usetikzlibrary{shapes.geometric}

\usetikzlibrary{calc,shadings,patterns}


\begin{document}

\begin{tikzpicture}


% 90 degrees rotation
\node[buffer] (a) {middle part};

\draw (a.north) -- (a.north east) -- (a.east) -- (a.south east) -- (a.south)
 -- (a.south west) -- (a.west) -- (a.north west) -- cycle;

 \node[below=0pt of a] {abcdef};
\path (a.north) -- (a.west) node[pos=0.75, sloped, above] {ghijkl};
\path (a.north) -- (a.east) node[pos=0.75, sloped, above] {lmnlopq};

\end{tikzpicture}

\end{document}

相关内容