[Tex/LaTex] tikz: clipping a node

tikz-pgf

i would like to obtain this effect

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\clip (-1ex,-12pt) rectangle (1ex,12pt);
\node [align=center] (text) {A \\ B \\ C};
\end{tikzpicture}
\end{document}

but i cannot find a way to clip the node that is still to be drawn, something like

\clip ($(text.south west) + (0,6pt)$) rectangle ($(text.north east) - (0,6pt)$);

how can i achieve it without explicit use of the position to clip?

Best Answer

Ah, this looks like a job for \pgfpositionnodelater! If you call this command in a local scope, subsequently defined nodes won't be typeset straight away, but their content will be saved into a box and the edge coordinates can be saved to macros. You can then use the node alias not yet positioned@<node name> to set up your clip path, and then load the edge coordinates and typeset the node using \pgfpositionnodenow.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\newbox\mybox
\def\mysaver{
\global\setbox\mybox=\box\pgfpositionnodelaterbox
\global\let\myname=\pgfpositionnodelatername
\global\let\myminx=\pgfpositionnodelaterminx
\global\let\myminy=\pgfpositionnodelaterminy
\global\let\mymaxx=\pgfpositionnodelatermaxx
\global\let\mymaxy=\pgfpositionnodelatermaxy
}

\newcommand{\loadnode}{
\let\pgfpositionnodelatername=\myname
\let\pgfpositionnodelaterminx=\myminx
\let\pgfpositionnodelaterminy=\myminy
\let\pgfpositionnodelatermaxx=\mymaxx
\let\pgfpositionnodelatermaxy=\mymaxy
\setbox\pgfpositionnodelaterbox=\box\mybox
}

\begin{document}
\begin{tikzpicture}
{
\pgfpositionnodelater{\mysaver}
\node [align=center] (text) {A \\ B \\ C \\ ABCDEF};
}
\clip ($(not yet positioned@text.south west) + (0,6pt)$) rectangle ($(not yet positioned@text.north east) - (0,6pt)$);
\loadnode
\pgfpositionnodenow{\pgfqpoint{0pt}{0pt}
}

\end{tikzpicture}
\end{document}

EDIT by percusse

If you want to use the TikZ frontend for a similar effect, you can use append after command key as follows:

\begin{tikzpicture} 
\clip node [append after command={%
        ($(text.south west) + (0,6pt)$) rectangle ($(text.north east) - (0,6pt)$)%
        },%
    align=center] (text) {TOP TEXT\\ A \\ B \\ C \\ LONGER}; 
\end{tikzpicture}

Roughly speaking, it nests the path construction and node placement in the desired order. The counterpart in terms of the order is given by the prefix after command. This allows us to use the name of the node to-be-created in advance. However, its use is quite limited as stated in the manual. You can not change the structural properties such as shape,color etc of the node.