1

This script

\documentclass\[tikz,border=5pt\]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\begin{document}
\begin{tikzpicture}
    \node\[draw, align =    center,
    minimum height = 1.5cm,
    text width = 1.5cm\]
  (a) at (0, 0){a};
  \node\[draw\](b) at (a.north east)\[anchor= north west\]{b};
\end{tikzpicture}
\end{document}

delivers the expected output where node b is positioned relative no node a:

enter image description here

Nice!

However, adding a shape (rounded rectangle) to node a disrupts this harmony:

\node[draw, align = center,
    minimum height = 1.5cm,
    text width = 1.5cm]
  (a) at (0, 0)[rounded rectangle]{a};

enter image description here

The expected output is:

enter image description here

where the red line shows an imagined border around node a.

How to solve this problem?

Viesturs
  • 7,895

1 Answers1

2

Depending on what you want, you could do

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\begin{document}
\begin{tikzpicture}
  \node[draw, align = center,
    minimum height = 1.5cm,
    text width = 1.5cm]
  (a) at (0, 0)[rounded rectangle]{a};
  \node[draw](b) at (a.east |- a.north)[anchor=north west]{b};
\end{tikzpicture}
\end{document}

enter image description here

The main question is what you want to achieve. It is obvious that the anchor north east is not at a corner, simply because a rounded rectangle has no corners. So I "invented" a corner. (a.east |- a.north) is precisely an imaginary corner of a rectangle around the a node,

It is of course also possible to let the b node touch the boundary of a.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc,intersections}
\begin{document}
\begin{tikzpicture}
  \node[draw, align = center,name path=rr,
    minimum height = 1.5cm,
    text width = 1.5cm]
  (a) at (0, 0)[rounded rectangle]{a};
  \node[opacity=0,overlay](phantomb) at (a.east |- a.north)[anchor=north west]{b};
  \path[name path=aux] (phantomb.south west) -- ++(-1,0);
  \draw[name intersections={of=rr and aux}] node[draw](b) at (intersection-1)
  [anchor=south west]{b};
\end{tikzpicture}
\end{document}

enter image description here

There are other possibilities, really depends on what you precisely want to achieve.

Addendum: relative positioning of two such shapes. I do not know what precisely you are after, but here is a possibility.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc,intersections,calc}
\begin{document}
\begin{tikzpicture}
  \node[draw, align = center,name path=rr,
    minimum height = 1.5cm,
    text width = 1.5cm]
  (a) at (0, 0)[rounded rectangle]{a};
  \node[opacity=0,rounded rectangle,overlay](phantomb) at (a.east |- a.north)[anchor=north west]{b};
  \path[name path=aux] (phantomb.south west) -- ++(-1,0);
  \draw[name intersections={of=rr and aux}] 
  let \p1=($(phantomb.south west)-(phantomb.south -| phantomb.west)$) in
  node[draw,rounded rectangle](b) at ([xshift=\x1]intersection-1)
  [anchor=south west]{b};
\end{tikzpicture}
\end{document}

enter image description here

EDIT: Added overlay to prevent the phantom nodes from enlarging the bounding box.

  • This is not what I want. node b should remain in the same location as in the first figure, imagining a rectangular border drawn enclosing the rounded rectangle. – Viesturs Oct 04 '18 at 18:54
  • 1
    @Viesturs Yes, this is what my updated answer does. (a.east |- a.north) is precisely an imaginary corner of a rectangle around a. –  Oct 04 '18 at 18:54
  • |- signifies an intersection point? – Viesturs Oct 04 '18 at 19:10
  • 1
    @Viesturs Sort of. It says: take the x coordinate of a.east and the y coordinate of a.north. This is explained in detail in this nice answer. –  Oct 04 '18 at 19:12
  • What if node b itself is drawn with a rounded rectangle? How to set the anchor? – Viesturs Oct 04 '18 at 19:29
  • 1
    @Viesturs I added something. The idea is to measure the mismatch between (phantomb.south west) and (phantomb.south -| phantomb.west) and use the distance to shift the node. Of course, here are tons of possibilities, touch vs. not touch etc., but all of them can be addressed this way, I think –  Oct 04 '18 at 19:47
  • I love the phantom box trick. I'll have to remember that. – A.Ellett Oct 04 '18 at 20:15
  • 1
    @A.Ellett Thanks but I certainly do not deserve any credit for that. This is essentially how the visible on from aobs (texdoc aobs) works. –  Oct 04 '18 at 20:17
  • Thanks for point that out. I didn't know about aobs. Looks very interesting. Thank you for sharing that. – A.Ellett Oct 04 '18 at 20:22