If I want to do something with the node’s content (which, to my recollection, cannot be accessed by a key), I collect it in a savebox via the lrbox
environment.
In the following example, the text is saved in the savebox \qrrTikzNodeContent
and then, still in the execute at end node
key, used.
Firstly it is typeset in \phantom
so that the node gets re-sized appropriately (as if the content would be typeset as usual). The following \usebox
command is firstly \llap
ed to occupy the same horizontal space like its phantom-ed version, and than \smash
ed so that it doesn’t occupy any vertical space.
I added some hopefully useful keys, that should help how \raisebox
may affect the node:
phantom
re-sizes the node as if the content would be typeset in the usual way;
smash
deactivates re-sizing of the node as forced by \raisebox
.
This does not work with the keys text width
and align
as \tikz@atbegin@node
is executed after the minipage is applied that is used to realize those keys.
For this special use-case I want to point out that there’s also the text depth
key.
For <content>
without a depth setting this to <value>
is like a \raisebox{<value>}{<depth>}
.
The following code procudes a matrix of nodes:
normal raise raise + phantom
text depth raise + smash raise + phantom + smash
Additionally it shows the baseline of the node.
Code
\documentclass[tikz]{standalone}
\newsavebox\qrrTikzNodeContent
\newif\ifphantom% remove for 'clean' version
\newif\ifsmash % remove for 'clean' version
\tikzset{
nodes={draw,circle},
bl/.style={append after command={\pgfextra{\draw[very thin] (\tikzlastnode.base west) -- (\tikzlastnode.base east);}}},% debug, remove for 'clean' version
phantom/.is if=phantom,% remove for 'clean' version
smash/.is if=smash, % remove for 'clean' version
raise/.style={
execute at begin node={\begingroup\begin{lrbox}{\qrrTikzNodeContent}},
execute at end node={\end{lrbox}% remove for 'clean' version
\ifphantom
\phantom{\usebox\qrrTikzNodeContent}%
\fi
\ifsmash\smash{\fi
\ifphantom\llap{\fi
\raisebox{#1}{\usebox\qrrTikzNodeContent}%
\ifphantom}\fi
\ifsmash}\fi
\endgroup
},
% execute at end node={\end{lrbox}% 'clean' version
% \phantom{\usebox\qrrTikzNodeContent}%
% \smash{%
% \llap{%
% \raisebox{#1}{\usebox\qrrTikzNodeContent}%
% }%
% }%
% \endgroup
% }
},
}
\begin{document}
\begin{tikzpicture}
\matrix[draw=none, append after command={}, row sep=1ex, column sep=1ex] {
\node[bl, ] {g}; &
\node[bl, raise=-.2cm, ] {g}; & % only raisebox
\node[bl, raise=-.2cm, phantom] {g}; \\ % phantom
\node[bl, text depth=-.2cm ] {g}; & % text depth
\node[bl, raise=-.2cm, smash, ] {g}; & % smash
\node[bl, raise=-.2cm, smash, phantom] {g}; \\ % smash + phantom
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix[draw=none, append after command={}, row sep=1ex, column sep=1ex] {
\node[bl, ] {h}; &
\node[bl, raise=-.2cm, ] {h}; & % only raisebox
\node[bl, raise=-.2cm, phantom] {h}; \\ % phantom
\node[bl, text depth=-.2cm ] {h}; & % text depth
\node[bl, raise=-.2cm, smash, ] {h}; & % smash
\node[bl, raise=-.2cm, smash, phantom] {h}; \\ % smash + phantom
};
\end{tikzpicture}
\end{document}
Output
\tikzset{test/.style={yshift=-0.2cm}}
. The problem here is not the escaping of the characters but the fact that what you are trying to execute at the begin and end is not code, but rather a code fragment.yshift
moves the whole node, I just want to move the text inside the node.