4

I'm trying to do something to do something very similar to this:

Rotate node text and use relative positioning in TikZ?

Except I would like to rotate multiple nodes, and I end up with one node 'floating' above the other:

example of wrong positioning

Ideally I would like them to just be aligned next to each other.

Here is the example code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.misc, positioning}

\begin{document}

\begin{tikzpicture}
    \tikzstyle{every node}=[font=\scriptsize]

    \node(node1) [draw, rounded rectangle, align=center, rotate=90, fill=gray ]{Small text};
    \node(node2) [draw,  rounded rectangle, right = of node1, align=center, rotate=90, fill=red, anchor=north] {longer text \\ on two lines};
\end{tikzpicture}
\end{document}
0

2 Answers 2

4

Try this:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.misc, positioning}

\begin{document}

\begin{tikzpicture}[every node/.append style={font=\scriptsize}]

    \node(node1) [draw, rounded rectangle, align=center, rotate=90,fill=gray ]{Small text};
    \node(node2) [draw,  rounded rectangle, right = of node1.south, align=center, rotate=90, fill=red, anchor=north] {longer text \\ on two lines};
\end{tikzpicture}
\end{document}

enter image description here

Explanation: positioning uses appropriate anchors to measure distances. So if you say right of, it place the node some distance to the right of the east anchor. However, these rotations are not factored in, so after the rotation the east anchor is on the (now) upper end of the node, and that's why the red node got placed some distance right of the east anchor, i.e. the upper end, of the gray node. In this answer, TikZ gets told to use the south anchor instead, which is on the right end of the node after rotation. There is a slight asymmetry in the treatment because naively you might expect that TikZ would also use the west anchor of the red node, but it does not. I guess it uses \pgfpointshapeborder instead but I did not check this.

EDIT: The only edit I made was to replace \tikzstyle{every node}=[font=\scriptsize] by [every node/.append style={font=\scriptsize}]. See this discussion for a discussion of \tikzstyle vs. \tikzset.

1
  • 2
    I've cleaned up comments here: comments can and often should be removed once they are not relevant.
    – Joseph Wright
    Dec 11, 2018 at 14:27
0

For the nodes to be aligned with the baseline, it is sufficient to rotate around the anchor to the west. So, remove the anchor to the north % anchor=north and move to the right of this anchor right = of node1.west

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.misc, positioning
}

\begin{document}

\begin{tikzpicture}
    \tikzstyle{every node}=[font=\scriptsize]
    \node(node1) [draw, rounded rectangle, align=center, rotate=90, fill=gray ]{Small text};
    \node(node2) [draw,  rounded rectangle, right = of node1.west, align=center, rotate=90, fill=red,% anchor=north
    ] {longer text \\ on two lines};
\end{tikzpicture}
\end{document}

right of

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .