标记矩形和三角形的边

标记矩形和三角形的边

在 TikZ 中标记矩形和三角形边的最简单方法是什么?

答案1

我通常使用沿着绘制多边形的路径放置的节点。

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
% Rectangle with labeled edges
\draw (0,0)  -| (5,3) 
    node[pos=0.25,below] {$\ell$} 
    node[pos=0.75,right] {$w$}
    -| (0,0);
\end{tikzpicture}

\begin{tikzpicture}
\draw[rotate=30] (0,0)  -| (5,3) 
    node[pos=0.25,below] {$\ell$} 
    node[pos=0.75,right] {$w$}
    -| (0,0);   
\end{tikzpicture}

\begin{tikzpicture}
\draw (0,0) -- (4,0) node[midway,below] {$a$}
   -- (2,4) node[midway,right] {$b$}
   -- (0,0) node[midway,left] {$c$};
\end{tikzpicture}

\end{document}

示例代码输出

答案2

Hotschke 描述的解决方案也可以应用于所有geometric形状,其优点是我们可以使用专门定义的锚点而不是特定的(通常是未知的)angle锚点。

  • regular polygons: side 1, side 2, ... 锚点
  • isosceles triangleleft side,,lower sideright side
  • starinner point 1,,outer point 1...
  • ...

以下是一些示例:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning, shapes.geometric}

\begin{document}
\begin{tikzpicture}

\node[draw, minimum size=2cm, regular polygon, 
    regular polygon sides=3, 
    label=side 1:a, label=side 2:b, label=side 3:c] (A) {};

\node[right = 15mm of A, 
    draw, minimum size=2cm, regular polygon, 
    regular polygon sides=6,
    label=side 1:a, label=side 2:b, label=side 3:c,
    label=side 4:d, label=side 5:e, label=side 6:f] (B) {};

\node[below = 15mm of A,
    draw, minimum size=2cm, star, 
    regular polygon sides=6,
    label=inner point 1:a, label=inner point 2:b, label=inner point 3:c,
    label=inner point 4:d, label=inner point 5:e] (C) {};

\node[right = 15mm of C,
    draw, minimum size=2cm, star, 
    regular polygon sides=6,
    label=outer point 1:a, label=outer point 2:b, label=outer point 3:c,
    label=outer point 4:d, label=outer point 5:e] (D) {};

\node[below= 10mm of C,
    draw, minimum size=2cm, isosceles triangle, shape border rotate=90,
    label=left side:a, label=lower side:b, label=right side:c] (E) {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

矩形所有边上的标签(可选旋转)

此答案基于在矩形上方标记矩形另外居中旋转的 tikz 标签在 TikZ 中将文本放置在矩形旁边

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \coordinate (origin) at (0,0);

  % INITIAL RECTANGLE WITHOUT LABELS AT THE EDGES
  \draw[rotate around={240:(origin)}] (origin) rectangle +(2.5,3.6)
    node[pos=.5, align=center] {CENTER TEXT};

  \begin{scope}[yshift=-5cm]
    \coordinate (origin) at (0,0);

    % REWRITE RECTANGLE AS NODE WITH LABELS AT THE EDGES
    \node [
        draw, rectangle,
        anchor=south west,
        rotate around={240:(origin)},
        minimum width=2.5cm, minimum height=3.6cm,
        % For some reason a unit has to be given for minimum width and minimum height
        label=LABEL NORTH (DEFAULT),
        label={[rotate=-30,anchor=north]east:LABEL EAST},
        label=west:LABEL WEST,
        label=south:LABEL SOUTH,
        label=center:CENTER TEXT,
      ] at (origin) {};

  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

笔记:

  • shape border rotate不适用于形状rectangle

相关内容