如果经常发现自己在写:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node (refNode) {\includegraphics[width=5em]{example-image-a}};
% Boring way to define intensive coordinates relatively to it:
\def\x{.2} % e.g: "somewhat left"
\def\y{.54} % e.g.: "slightly more than midway up"
\coordinate (x) at ($(refNode.west)!\x!(refNode.east)$); % boilerplate
\coordinate (y) at ($(refNode.south)!\y!(refNode.north)$); % boilerplate
\node[red] (intensively located) at (x |- y) {.};
\end{tikzpicture}
\end{document}
有没有更简单的方法来实现这一点?比如
\node[relatively to aNode=(.2, .54), red] (cool) {.};
此外,上述样板可以正确扩展到 [0,1] 之外的坐标,但如果refNode
碰巧是,这是不够的[rotate=34]
:它需要变得更加复杂。
我很惊讶我发现没有 tikz 内置实用程序可以执行这种相对定位..我错过了什么吗?
答案1
您基本上在参考节点上安装了一个本地坐标系。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node (refNode) {\includegraphics[width=5em]{example-image-a}};
\begin{scope}[shift={(refNode.south west)},x={(refNode.south
east)-(refNode.south west)},y={(refNode.north west)-(refNode.south west)}]
\node[red] at (0.2,0.54) {.};
\end{scope}
\end{tikzpicture}
\end{document}
你可以用以下风格来表达:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[local cs/.style={shift={(#1.south west)},x={(#1.south
east)-(#1.south west)},y={(#1.north west)-(#1.south west)}}]
\node (refNode) {\includegraphics[width=5em]{example-image-a}};
\path[local cs=refNode] (0.2,0.54) node[red] {.};
\end{tikzpicture}
\end{document}