我想将一个节点相对于 进行定位\pic
。我能够将\pic
相对于 节点进行定位,但我希望以另一种方式进行定位。
\documentclass{standalone}
\usepackage{tikz}
\tikzset{
pics/mp3/.style args={scale #1}{
code={
\begin{scope}[scale=#1,every node/.style={scale=0.8*#1}]
\draw[rounded corners=10*#1] (-3,-1) rectangle (3,1);
\draw[rounded corners=5*#1] (2.8,0.5) rectangle (1.8,-0.5);
\draw[rounded corners=5*#1] (-1.5,0.6) rectangle (1.5,-0.6);
\end{scope}
}
}
}
\begin{document} \begin{tikzpicture}
%% Position a pic relative to a node:
\node (foo) at (0,2) {blubber};
\pic[above of=foo] {mp3={scale .5}};
%% Position a node relative to a pic:
\pic (bar) at (0,-2) {mp3={scale .5}}; %% This doesn't create a named shape.
%\node[below of=bar] {bimbaz}; %% Package pgf Error: No shape named bar is known.
\end{tikzpicture} \end{document}
那么,有没有什么办法可以命名它\pic
?
答案1
您可以命名内部对象并在稍后使用该名称引用它们pic
。
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\tikzset{
pics/mp3/.style={
code={
\node[rounded corners=10*#1,draw,anchor=center,
minimum height=2cm,minimum width=6cm,
transform shape] (-cover) at (0,0) {};
\draw[rounded corners=5*#1] (2.8cm,0.5cm) rectangle (1.8cm,-0.5cm);
\draw[rounded corners=5*#1] (-1.5cm,0.6cm) rectangle (1.5cm,-0.6cm);
}
}
}
\begin{document}
\begin{tikzpicture}
\node (foo) at (0,2) {blubber};
\pic[below left= of foo,scale=0.5] {mp3=.5};
\pic[scale=0.5] (b) at (0,-2) {mp3=.5};
\node[below= 5mm of b-cover] {bimbaz};
\end{tikzpicture}
\end{document}
几点评论;
/.style args
如果您只有一个参数,则无需使用处理程序。常规/.style
处理程序默认有一个参数,因此您可以直接mp3=0.5
使用mp3/.style={..... #1 code ....}
- 放弃
below of
或<direction> of=
语法并切换到更稳定的定位库语法(将移至of
等号的另一侧)可能是一种很好的做法。更多信息PGF/TikZ 中“right of=”和“right=of”之间的区别 - 对象的缩放最好留给 TikZ 本身。所以我举了一个例子来说明你也可以在外部进行缩放。