I was dissatisfied with the implicit degree anchors TikZ provides for nodes (i.e., (node.30)
). I often want to start an path at 30 percent from the left at the lower border of a node. This is of course possible with calc
, but this feels a little clumsy.
Therefore, I wrote a snippet that defines a node relative coordinate system and hooked it into the parser to provide a convenient syntax. After some usage, I found it so useful that I would like to show it and request some feedback, before sending it to the PGF/TikZ maintainers.
The resulting syntax for 20 percent at the bottom is (NODE:20,0)
. What do you think?
\documentclass[crop,tikz]{standalone}
\makeatletter
% Declare a relative coordinate system, that is relative to the edges
% of a given node.
\tikzdeclarecoordinatesystem{rel}{%
\tikzset{cs/.cd,x=0pt,y=0pt,#1}%
\pgfpointlineattime{(\tikz@cs@x / 100)}%
{\pgfpointanchor{\tikz@pp@name{\tikz@cs@node}}{south west}}%
{\pgfpointanchor{\tikz@pp@name{\tikz@cs@node}}{south east}}%
\edef\tikz@cs@x{\the\pgf@x}%
\pgfpointlineattime{(\tikz@cs@y / 100)}%
{\pgfpointanchor{\tikz@pp@name{\tikz@cs@node}}{south west}}%
{\pgfpointanchor{\tikz@pp@name{\tikz@cs@node}}{north west}}%
\pgfpoint{\tikz@cs@x}{\pgf@y}%
}
% Hook into Tikz coordinate parse to provide (<name>:<rel x>x<rel y>)
\def\tikz@parse@relcs#1(#2:#3,#4){%
\tikz@parse@coordinatesystem#1(rel cs:name={#2},x={#3},y={#4})%
}
\let\tikz@parse@relcs@polar@saved=\tikz@parse@polar
\def\tikz@parse@polar#1(#2:#3){%
\pgfutil@in@{,}{#3}%
\ifpgfutil@in@%
\let\@next\tikz@parse@relcs%
\else%
\let\@next\tikz@parse@relcs@polar@saved%
\fi%
\@next#1(#2:#3)%
}
\makeatother
\begin{document}
\begin{tikzpicture}
% First we define a border node, that we use as coordinate system
\node[draw, text width=100mm, align=center, text height=40mm, text depth=40mm](border){};
% Just some coordinate system
\foreach \x in {10,20,30,40,50,60,70,80,90} {
\draw[black!20] (border:\x,100) -- (border:\x,0) node[black,anchor=north] {\x};
\draw[black!20] (border:0,\x) -- (border:100,\x) node[black,anchor=west] {\x};
}
% Use the explicit coordinate syntax
\draw (rel cs:x=50,y=0,name=border) -- (rel cs:x=75,y=100,name=border);
% Use the shorthand version that is hooked into the TiKZ coordinate
% parser.
\node at (border:20,30) (A) {A};
\node at (border:50,75) (B) {B};
% Some more nice demonstrations
\node[draw] at (border:25,75) (text) {Long-Text};
\draw (text:20,0) edge[->,bend right] (A);
\draw (text:60,0) edge[->,bend right] (B.south);
\foreach \x in {10,20,30,40,50,60,70,80,90} {
\draw (text:\x,100) -- ++(up:5pt);
}
% Polar Coordinates still work
\draw (0,0) -- (30:2cm);
\end{tikzpicture}
\end{document}
RFC
means in this context. To me, it meansRugby Football Club
, but I'm struggling to make sense of that in this context.x
, as is very common. Try changing2cm
to2ex
, for example.(30:\myxyz)
. This works OK in simple cases, but I've no idea if it is generally safe with your code.