No text=none in TikZ?
If you don't need the text then it shouldn't be there anyway. Either you enlarge the node with minimum width,height,inner sep
etc. or you can use the text opacity
key comfortably.
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[text opacity=0,draw=red,fill=yellow!25] (a) {Text};
\end{tikzpicture}
\end{document}
If you also need some placeholder for a text, you can go slightly esoteric and get the width depth height info beforehand e.g. just for the width;
\begin{tikzpicture}
\pgfmathparse{width{"Text"}}
\edef\mywidth{\pgfmathresult}
\node[minimum width=\mywidth pt,draw=red,fill=yellow!25] (a) {};
\end{tikzpicture}
you can use \phantom{Text}
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[draw=red,fill=yellow!25] (a) {\phantom{Text}};
\end{tikzpicture}
\begin{tikzpicture}
\node[draw=red,fill=yellow!25] (a) {{Text}};
\end{tikzpicture}
\end{document}
If one wants a true “phantom” text node (i.e. no selectable text in the resulting PDF) and doesn’t want to manually provide the \phantom
macro, one can use one of the following solutions:
Using the undocumented keys
execute at begin node
andexecute at end node
one can use thelrbox
environment (or manually with\setbox
) a box which is then used inside\phantom
:execute at begin node = \begin{lrbox}{\pgfutil@tempboxa}, execute at end node = \end{lrbox}\phantom{\usebox\pgfutil@tempboxa}
This works very well for one-liner nodes and also (I assume) for multiparted nodes. Problems arise when the
text width
key is used because the neededminipage
environment is setup outside of the stuff that is given toexecute at begin/end node
. (Obviously, we could just mimic this ourselves with aminipage
and things from TikZ’ original setup).After TikZ puts the text between
{
and}
in a box it checks the values of the keystext width
,text depth
andtext height
and corrects the box measures accordingly. After transformation, the node is created via the\pgfnode
macro. We can intercept this here with the following setup (and a little help frometoolbox
):\usepackage{etoolbox} \patchcmd\tikz@fig@continue{\tikz@node@transformations}{% \iftikz@node@phantom \setbox\pgfnodeparttextbox\hbox{% \vrule height\ht\pgfnodeparttextbox depth\dp\pgfnodeparttextbox width0pt% \vrule height0ptdepth0ptwidth\wd\pgfnodeparttextbox}% \fi\tikz@node@transformations}{}{}
This would simply overwrite the
\pgfnodeparttextbox
with rules that are exactly the width, height and depth of the original\pgfnodeparttextbox
(just as\phantom
would do).Personally, I prefer this solution, combined with the following keys/conditional, that also provides (besides the
text=none
syntax) a keyphantom
.\newif\iftikz@node@phantom \tikzset{ phantom/.is if=tikz@node@phantom, text/.code=% \edef\tikz@temp{#1}% \ifx\tikz@temp\tikz@nonetext \tikz@node@phantomtrue \else \tikz@node@phantomfalse \let\tikz@textcolor\tikz@temp \fi}
Another option can be used with the upcoming version of TikZ which provides a
node contents
key. We can create a key that takes its argument and passes it tonode contents
encompassed in a\phantom
. This will only work for one-liners.Phantom/.style={node contents=\phantom{#1}}
Code
\documentclass[tikz]{standalone}
\makeatletter
\newif\iftikz@node@phantom
\tikzset{
phantom/.is if=tikz@node@phantom,
text/.code=%
\edef\tikz@temp{#1}%
\ifx\tikz@temp\tikz@nonetext
\tikz@node@phantomtrue
\else
\tikz@node@phantomfalse
\let\tikz@textcolor\tikz@temp
\fi,
Phantom/.style={node contents=\phantom{#1}},% only CVS, only single line
PHantom/.style={
execute at begin node=\setbox\pgfutil@tempboxa\hbox\bgroup,
execute at end node=\egroup\phantom{\usebox\pgfutil@tempboxa}}
}
\usepackage{etoolbox}
\patchcmd\tikz@fig@continue{\tikz@node@transformations}{%
\iftikz@node@phantom
\setbox\pgfnodeparttextbox\hbox{%
\vrule height\ht\pgfnodeparttextbox depth\dp\pgfnodeparttextbox width0pt%
\vrule height0ptdepth0ptwidth\wd\pgfnodeparttextbox}%
\fi\tikz@node@transformations}{}{}
\makeatother
\begin{document}
\tikz[nodes={draw, text width=2cm}]\path
node[green] {Text text text text text text text}
node[PHantom, red] {Text text text text text text text};
\foreach \iframe[evaluate={\iframe=\iframe/10}] in {0,...,10}{
\begin{tikzpicture}[nodes=draw]
\node[text=none] (source) {Text};
\node[phantom] (target) at (2,2) {Text};
\node[gray] at (1,1) [Phantom=Text];
\path (source.center) -- node[pos=\iframe] {Text} (target.center);
\end{tikzpicture}}
\end{document}