使用 TikZ 水平放大的六边形

使用 TikZ 水平放大的六边形

我需要为图表绘制一些节点,但无法生成简单的水平扩大的六边形。

如果我使用regular polygon(来自 shapes.geometric),我无法放大它(下图中的第一个图)。我尝试设置,shape aspect但无济于事。

我在 pgfmanual 中找到的一个解决方法是使用chamfered rectangle(来自 shapes.misc),但我们必须谨慎处理xsep矩形内文本之间的平衡(下图中的第二和第三个图)。

这是生成下图的代码:

\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.misc}

\begin{tikzpicture}[node distance=1.5cm]
  \node[regular polygon, regular polygon sides=6, shape aspect=0.5, minimum width=4cm, minimum height=1cm, draw] (reg) {Regular node};
  \node[chamfered rectangle, chamfered rectangle xsep=2cm, draw] (chamf) [right=of reg] {Enlarged node};
  \node[chamfered rectangle, chamfered rectangle xsep=1cm, text width=2cm, draw] (chamfB) [below=of chamf] {Enlarged node with many lines that flow down};
\end{tikzpicture}

人物

答案1

要水平缩放六边形,您可以使用该xscale选项;您可以使用该 label=center:<text>选项将文本添加到六边形节点:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.misc}

\begin{document}

\begin{tikzpicture}
  \node[regular polygon, regular polygon sides=6, minimum width=2cm,draw,label=center:some text] (reg1) at (0,0){};
  \node[regular polygon, regular polygon sides=6, minimum width=1cm, xscale=3,draw,label=center:some text] (reg2) at (3,0) {};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

您可以向每个节点添加scale。以下是带有和的版本,其中应用了scale=1和的默认值scale=1.3

在此处输入图片描述

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.misc}

\begin{document}
\begin{tikzpicture}[node distance=1.5cm]
  \node[regular polygon, regular polygon sides=6, shape aspect=0.5, minimum width=4cm, minimum height=1cm, draw,scale=1] (reg) {Regular node};
\end{tikzpicture}
%
\begin{tikzpicture}[node distance=1.5cm]
  \node[regular polygon, regular polygon sides=6, shape aspect=0.5, minimum width=4cm, minimum height=1cm, draw,scale=1.3] (reg) {Regular node};
\end{tikzpicture}
\end{document}

如果您在向节点添加多行文本时遇到问题,则需要指定文本的宽度和对齐方式:

text width=2.4cm,align=center

这与扩展问题无关,得出:

在此处输入图片描述

\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.misc}

\begin{document}
\begin{tikzpicture}[node distance=1.5cm]
  \node[regular polygon, regular polygon sides=6, shape aspect=0.5, minimum width=4cm, minimum height=1cm, draw,scale=1,text width=2.4cm,align=center] (reg) {Enlarged node with many lines that flow down};
\end{tikzpicture}
%
\begin{tikzpicture}[node distance=1.5cm]
  \node[regular polygon, regular polygon sides=6, shape aspect=0.5, minimum width=4cm, minimum height=1cm, draw,scale=1.3,text width=2.4cm,align=center] (reg) {Enlarged node with many lines that flow down};
\end{tikzpicture}
\end{document}

如果您希望手动决定换行符的位置,请通过添加\\,那么您无需指定text width=2.4cm。因此请使用:

\node ... {Enlarged node \\ with many lines \\ that flow down}

完整代码如下:

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.misc}

\begin{document}
\begin{tikzpicture}[node distance=1.5cm]
  \node[regular polygon, regular polygon sides=6, shape aspect=0.5, minimum width=4cm, minimum height=1cm, draw,scale=1,text width=2.4cm,align=center] (reg) {Enlarged node \\ with many lines \\ that flow down};
\end{tikzpicture}
%
\begin{tikzpicture}[node distance=1.5cm]
  \node[regular polygon, regular polygon sides=6, shape aspect=0.5, minimum width=4cm, minimum height=1cm, draw,scale=1.3,align=center] (reg) {Enlarged node \\ with many lines \\ that flow down};
\end{tikzpicture}
\end{document}

相关内容