5

Using both calc and |- would be a convenient way to refer to "inner, relative coordinates" of a node, with

  • (0, 0) meaning node.center,
  • (0, 1) meaning node.east and
  • (.3, -.5) meaning somewhere precise (but relative) in the south east quarter.

However, as described here and there, the following coordinate is not parsed:

($(node.center)!.3!(node.east)$ |- $(node.center)!-.5!(node.north)$)

So the following convenience macro does not work:

\newcommand{\relativeToNode}[3]{($(#1.center)!#2!(#1.east)$ |- $(#1.center)!#3!(#1.north)$)}

How nice would it be to write:

\node (inner) at (\relativeToNode{outer}{.3}{-.5}) {hey!};

Is there any chance to get this working?
Any workaround that would not involve defining intermediate macros or coordinates (whose names would have to be picked etc.)?

  • 1
    tikz doesn't enable calculation of coordinates on the way as you desired. you should defined one auxiliary coordinate: \coordinate (aux) at ($(node1.south) + (1,0)$); \draw ($(node1.south) + (1,0)$) to (aux |- node2.north); – Zarko Aug 27 '18 at 9:52
  • @Zarko Well, it seems that it is finally possible like this :) – iago-lito Aug 27 '18 at 12:15
6

For your use case you don't even need the calc library. You can define style relative to node=<node name> that shifts and scales the coordinate system accordingly:

\tikzset{
    relative to node/.style={
        shift={(#1.center)},
        x={(#1.east)},
        y={(#1.north)},
    }
}

Then you can place your node with

\path[relative to node=outer] (-0.4,-0.5) node {Hello};

MWE:

\documentclass[tikz,margin=2mm]{standalone}

\tikzset{
    relative to node/.style={
        shift={(#1.center)},
        x={(#1.east)},
        y={(#1.north)},
    }
}

\begin{document}
    \begin{tikzpicture}
        \node[minimum width=2cm,minimum height=3cm,draw=red] (outer) at (3,3) {};

        % Only for the grid
        \begin{scope}[relative to node=outer]
            \foreach \ratio in {-1,-0.8,...,1}{
                \draw[help lines] (-1,\ratio) -- (1,\ratio);
                \draw[help lines] (\ratio,-1) -- (\ratio,1);
            }
            \draw (-1,0) -- (1,0);
            \draw (0,-1) -- (0,1);
        \end{scope}

        \path[relative to node=outer] (-0.4,-0.5) node {Hello};

    \end{tikzpicture}
\end{document}

Resulting in

enter image description here

  • 1
    Awesome, thanks :) Since this is not my core problem, but the core problem has no solution according to Zarko, I'll edit the OP so it matches your answer best. – iago-lito Aug 27 '18 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.