如何在多边形(五边形,六边形,八边形)内写下数字?

如何在多边形(五边形,六边形,八边形)内写下数字?

有许多节点形状那么除了圆形和矩形之外,是否有可能(以及如何)在任何其他形状内书写数字?

我已经使用 tikz 包创建了新命令。我可以在圆形内写入(并更改半径)和矩形内写入(更改边长),但不确定如何在六边形等多边形内写入。使用 \newcmdd{2} 时出现错误。

\documentclass{article}
\usepackage{tikz}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
    \node[shape=circle,fill=white,draw,minimum size=5.5mm,inner sep=0.0pt,text=black] (char) {#1};}}

\newcommand*\newcmd[1]{\tikz[baseline=(char.base)]{
        \node[shape=rectangle,fill=white,draw,minimum size=5mm,inner sep=0.0pt,text=black] (char) {#1};}}
    
% \newcommand*\newcmdd[1]{\tikz[baseline=(char.base)]{
    %   \node[shape=regular polygon, regular polygon sides=6, ,fill=white,draw, minimum size=5mm,inner sep=0.0pt,text=black] (char) {#1};}}


\begin{document}

\circled{2}
\newcmd{2}
% \newcmdd{2}
 
\end{document}

答案1

代码给出错误:

lalla.tex|19 error| Package pgfkeys Error: I do not know the key '/tikz/regular polygon sides', to which you passed '6', and I am going to ignore it. Perhaps you misspelled it.
lalla.tex|19 error| Package tikz Error: Unknown shape ``regular polygon.'' Using ``rectangle'' instead.

因此“正多边形”无法被识别。问题是,正如你在手册上的示例,您需要添加

 \usetikzlibrary {shapes.geometric}

在序言中,加载 Ti 之后Z。

答案2

在此处输入图片描述

shape border rotate=<angle>如果您想旋转节点(但不旋转其内容),您可以添加到节点选项。

请注意,minimum width影响外接圆,而inner sep影响内接圆。

\documentclass{article}

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

\begin{document}

\begin{tikzpicture}[every node/.style={minimum width=2cm, draw}]
\draw[help lines](-1,-1)grid(11,1);
\node[regular polygon, regular polygon sides=3, red] at (0,0) {3};
\node[regular polygon, regular polygon sides=4, orange] at (2,0) {4};
\node[regular polygon, regular polygon sides=5, yellow] at (4,0) {5};
\node[regular polygon, regular polygon sides=6, green] at (6,0) {6};
\node[regular polygon, regular polygon sides=7, blue] at (8,0) {7};
\node[regular polygon, regular polygon sides=8, violet] at (10,0) {8};
\end{tikzpicture}

\end{document}

相关内容