4

I am trying to solve How to rotate a composition of Tikz nodes correctly, without rotating the text of their labels?; and I found tikz pgf - Problem with "append after command" and "insert path".

This gave me the following idea - define a style, such that it simply places another node on top of the "current" one, with a colored background so the new node obscures the old, and with the same contents of the "current" node, but rotated. This is how far I got:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}

% https://tex.stackexchange.com/questions/55722/problem-with-append-after-command-and-insert-path
\makeatletter
\def\mymacro{
\begin{scope}
% { % MUST have this group! or scope!
  \setbox\tikz@figbox=\box\pgfutil@voidb@x
  \node at (\tikzlastnode) [fill=yellow] {\rotatebox{180}{YY}}; %
% }
\end{scope}
}
\makeatother

\tikzstyle{drc} = [draw, rectangle, line width=1pt, align=center,
append after command={\pgfextra{\mymacro}},
% insert path={\pgfextra{\mymacro}}, % no \tikzlastnode
]

\begin{document}

\begin{tikzpicture}[line width=3mm]
\node[drc] at (0,0) {\ \ A};
\node[drc] at (1,1) {B\ \ };
\end{tikzpicture}
\end{document}

This is the output:

test61.png

The problems/questions are:

  • The appended node is behind the original node, so its fill cannot obscure the original node - any way to get it in front of the original node?
  • I'm just having the letters YY for a test - but I'd rather "extract" the text of \tikzlastnode; however, it seems that \tikzlastnode is basically just the name of the last node, not an "object reference". In any case, is there a way to extract and reuse the text of \tikzlastnode?
  • 1
    For your last question, node contents are not objects. They are plain TeX boxes that TikZ forgets immediately as soon as it measures the size. – percusse Mar 27 '14 at 19:15
7

I don't think there is an built in method way to save the box of the last node, so here's a quick and dirty way (and probably not robust):

\documentclass[tikz, border=5]{standalone}
\newbox\lastnodebox
\begin{document}
\begin{tikzpicture}[every node/.style={draw,
  execute at begin node=\global\setbox\lastnodebox\hbox\bgroup,
  execute at end node=\egroup\copy\lastnodebox}]

\foreach \i [count=\y] in {A,...,F}
  \node at (0,-\y/2) {\box\lastnodebox \i};

\end{tikzpicture}
\end{document}

enter image description here


EDIT: In respect to the OP example, even if this approach is used to add an additional node through append after command={\pgfextra{\mymacro}}, still the node appended is "behind" (and so will not "cover" the previous node). However, this approach can be used in a style directly, together with the turn environment from the rotating package to issue a rotation "on time", and as such "appending" or "overlaying" a second rotated node is unnecessary. So we can use:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usepackage{rotating} %tlmgr install rotating
\newbox\lastnodebox
\tikzstyle{drc} = [draw, rectangle, line width=1pt, align=center,]
\tikzstyle{drcr} = [drc,
execute at begin node=\begin{turn}{180}\global\setbox\lastnodebox\hbox\bgroup,
execute at end node=\egroup\copy\lastnodebox\end{turn},
]

\begin{document}
\begin{tikzpicture}[line width=3mm]
\node[drc] at (0,0) {\ \ A\\A\ \ };
\node[drcr] at (1,1) {B\ \ \\\ \ B};
\end{tikzpicture}
\end{document}

... to obtain:

test62.png

  • Many thanks for the answer, @MarkWibrow - I have added an edit relating to the OP MWE, I hope that's OK. Cheers! – sdaau Mar 29 '14 at 8:39

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.