如何使用 TikZ 使标签(或其他元素)随节点旋转?

如何使用 TikZ 使标签(或其他元素)随节点旋转?

有没有办法让标签文本随其所连接的 TikZ 节点一起旋转。我可以旋转节点及其文本;但标签不会随节点一起旋转,而是保持“水平”:

\tikzset{fig/.style={regular polygon, regular polygon sides=3, label=60:A,label=below:B}}
\begin{tikzpicture} 
\foreach \i in {0,1,2,3}{\node[fig,rotate=20*\i,draw] at(\i*4,0) {Text};}
\end{tikzpicture}

在此处输入图片描述

总的来说,我想找到一种方法来旋转“复合”元素,该元素不仅仅由一个简单的节点及其标签组成;例如,一个形状,其中的角落已用符号或其他形状“标记”:

在此处输入图片描述


\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes}
% Not needed for MWE but used in my code:
%\usetikzlibrary{positioning}
%\usetikzlibrary{petri}

\begin{document}

\tikzset{fig/.style={regular polygon, regular polygon sides=3, label=60:A,label=below:B}}
\begin{tikzpicture} 
\foreach \i in {0,1,2,3}{\node[fig,rotate=20*\i,draw] at(\i*4,0) {Text};}
\end{tikzpicture}

% I don't know how to create the images in the second illustration

\end{document}

答案1

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}

\begin{document}

\begin{tikzpicture} [fig/.style={regular polygon, regular polygon sides=3}]
\foreach \i in {0,1,2,3}{%
    \begin{scope} [rotate around={20*\i:(\i*4,0) },every node/.style={transform shape}]
        \node[fig,draw] (t) at (\i*4,0) {Text};
        \node at ([shift={(60:8pt)}]t.60)  {A}; % I prefer more possibilities
        \node at ([yshift=-8pt]t.south)  {B};   % I prefer more possibilities
    \end{scope}

}
\end{tikzpicture}
\end{document} 

在此处输入图片描述

或者使用您的代码并像 Peter 的回答中所说:本地风格更好(这里的 fig/.style 是本地风格)

\begin{tikzpicture}[fig/.style={regular polygon, regular polygon sides=3, label=60:A,label=below:B}] 
\foreach \i in {0,1,2,3}{
    \begin{scope}[rotate around={20*\i:(\i*4,0) }, ultra thick, transform shape]
        \node[fig,draw] at(\i*4,0) {Text};
    \end{scope}
}
\end{tikzpicture} 

答案2

如果将内容包装在内scope并旋转整个范围,则文本也会旋转(并包括transform shape选项):

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\tikzset{fig/.style={regular polygon, regular polygon sides=3, label=60:A,label=below:B}}

\begin{tikzpicture} 
\foreach \i in {0,1,2,3}{
    \begin{scope}[rotate around={20*\i:(\i*4,0) }, ultra thick, transform shape]
        \node[fig,draw] at(\i*4,0) {Text};
    \end{scope}
}
\end{tikzpicture}
\end{document}

相关内容