11

The contents of a node are drawn in text mode. Is there a way, short of introducing a tikzpicture environment, to draw a picture inside a node?

Nesting tikzpicture environments is problematic, also because the inner environment inherits all TikZ attributes from the surrounding tikzpicture environment.

krlmlr
  • 12,530

1 Answers1

9

You need to take precautions to use nesting tikzpicture but but it is doable.

You need to also take certain precautions to work with tikzpicture inside tikzpicture or with scope inside tikzpicture. Everything depends on circumstances and what you want to do but I think there is always a way to deal.

I work on the next document for another question. The problem here is to place automatically the heart and I need to use an extern node '(txt)' to place a node inside the tikzpicture inside a node. Well Tikz is magic and do that without problem

enter image description here

\documentclass{scrartcl}
\usepackage[paperwidth=8cm,
            paperheight=8cm,
            margin=0cm,
            left=0cm,
            bottom=0mm]{geometry} 
%\PassOptionsToPackage{dvipsnames,svgnames}{xcolor}   
\usepackage{tikz}

 \begin{document} 
 \pagestyle{empty}   
 \setlength{\parindent}{0em} 

 \tikzset{text style/.style={align      = center,
                             text width = \textwidth}}    

\newcommand\mytext{The life that I have \\
Is all that I have \\
And the life that I have \\
Is yours. \\
The love that I have  \\
Of the life that I have \\
Is yours and yours and yours. \\

A sleep I shall have \\
A rest I shall have \\
Yet death will be but a pause.\\

For the peace of my years \\
In the long green grass \\
Will be yours and yours and yours.}

\null\vfill
\begin{tikzpicture}[remember picture]
\node[text style,color=red] (txt) {\mytext};

\node at (txt.center)     
{\begin{tikzpicture}[fill  = red]
\pgftransformscale{.025}
\pgfpathmoveto{\pgfqpoint{3600 pt}{3350 pt}}
\pgfpathcurveto{\pgfqpoint{3580 pt}{4270 pt}}
               {\pgfqpoint{870 pt}{6400 pt}}
               {\pgfqpoint{990 pt}{7860 pt}}%     
\pgfpathcurveto{\pgfqpoint{1040 pt}{8430 pt}}
               {\pgfqpoint{1590 pt}{9060 pt}}
               {\pgfqpoint{2200 pt}{9060 pt}}%
\pgfpathcurveto{\pgfqpoint{3610 pt}{9050 pt}}
               {\pgfqpoint{3600 pt}{7720 pt}}
               {\pgfqpoint{3600 pt}{7720 pt}}%
\pgfpathcurveto{\pgfqpoint{3600 pt}{7720 pt}}
               {\pgfqpoint{3580 pt}{9050 pt}}
               {\pgfqpoint{4990 pt}{9060 pt}}%
\pgfpathcurveto{\pgfqpoint{5600 pt}{9060 pt}}
               {\pgfqpoint{6150 pt}{8430 pt}}
               {\pgfqpoint{6200 pt}{7860 pt}}%
\pgfpathcurveto{\pgfqpoint{6320 pt}{6400 pt}}
               {\pgfqpoint{3610 pt}{4270 pt}}
               {\pgfqpoint{3600 pt}{3350 pt}}%
\pgfpathclose            
\pgfusepath{clip,fill,stroke}%
%
\node[white,
     anchor = north west,
     text style] at (txt.north west) {\mytext};  
\end{tikzpicture}};
\end{tikzpicture} 
\vfill
\end{document}

Update I added inside a new tikpicture inside a node inside a tikzpicture inside a node inside the first tikzpicture with opacity , filling.

I created the ant.pgf from the ant of the pst-fun with some difficulties because original picture use rlineto and rcurveto and I don't know if it's possible with pgf.

enter image description here

 %  http://altermundus.com/SandBox/ant1.pgf       
 \documentclass{scrartcl}
\usepackage[paperwidth=8cm,
            paperheight=8cm,
            margin=0cm,
            left=0cm,
            bottom=0mm]{geometry} 
\PassOptionsToPackage{dvipsnames,svgnames}{xcolor}   
\usepackage{tikz}
\makeatletter  
\newcommand{\callornament}[1]{%
\begingroup
\def\i{\pgfusepath{clip}}%
\let\o\pgfpathclose
\let\s\pgfusepathqfillstroke
\def\p ##1##2{\pgfqpoint{##1bp}{##2bp}}%
\def\m ##1 ##2 {\pgfpathmoveto{\p{##1}{##2}}}%
\def\l ##1 ##2 {\pgfpathlineto{\p{##1}{##2}}}%
\def\r ##1 ##2 ##3 ##4 {\pgfpathrectangle{\p{##1}{##2}}{\p{##3}{##4}}}%
\def\c ##1 ##2 ##3 ##4 ##5 ##6 {%
\pgfpathcurveto{\p{##1}{##2}}{\p{##3}{##4}}{\p{##5}{##6}}}%   
\@@input #1\relax
\endgroup}   
\makeatother

 \begin{document} 
 \pagestyle{empty}   
 \setlength{\parindent}{0em} 

 \tikzset{text style/.style={align      = center,
                             text width = \textwidth}}    

\newcommand\mytext{The life that I have \\
Is all that I have \\
And the life that I have \\
Is yours. \\
The love that I have  \\
Of the life that I have \\
Is yours and yours and yours. \\

A sleep I shall have \\
A rest I shall have \\
Yet death will be but a pause.\\

For the peace of my years \\
In the long green grass \\
Will be yours and yours and yours.}

\null\vfill
\begin{tikzpicture}[remember picture]
\node[text style,color=red] (txt) {\mytext};

\node (h) at (txt.center)     
{\begin{tikzpicture}[fill  = red]
\pgftransformscale{.025}
\pgfpathmoveto{\pgfqpoint{3600 pt}{3350 pt}}
\pgfpathcurveto{\pgfqpoint{3580 pt}{4270 pt}}
               {\pgfqpoint{870 pt}{6400 pt}}
               {\pgfqpoint{990 pt}{7860 pt}}%     
\pgfpathcurveto{\pgfqpoint{1040 pt}{8430 pt}}
               {\pgfqpoint{1590 pt}{9060 pt}}
               {\pgfqpoint{2200 pt}{9060 pt}}%
\pgfpathcurveto{\pgfqpoint{3610 pt}{9050 pt}}
               {\pgfqpoint{3600 pt}{7720 pt}}
               {\pgfqpoint{3600 pt}{7720 pt}}%
\pgfpathcurveto{\pgfqpoint{3600 pt}{7720 pt}}
               {\pgfqpoint{3580 pt}{9050 pt}}
               {\pgfqpoint{4990 pt}{9060 pt}}%
\pgfpathcurveto{\pgfqpoint{5600 pt}{9060 pt}}
               {\pgfqpoint{6150 pt}{8430 pt}}
               {\pgfqpoint{6200 pt}{7860 pt}}%
\pgfpathcurveto{\pgfqpoint{6320 pt}{6400 pt}}
               {\pgfqpoint{3610 pt}{4270 pt}}
               {\pgfqpoint{3600 pt}{3350 pt}}%
\pgfpathclose            
\pgfusepath{clip,fill,stroke}%
%
\node[white,
     anchor = north west,
     text style] at (txt.north west) {\mytext}; 
\node at (txt.center){\tikz \pgftransformscale{.0125}%
                           \pgfsetfillcolor{MidnightBlue}%
                           \pgfsetfillopacity{.5}%
                           \callornament{ant1.pgf} ;};    
\end{tikzpicture}};

\end{tikzpicture} 
\vfill
\end{document}
Moriambar
  • 11,466
Alain Matthes
  • 95,075
  • Which question do you refer to? Could you please provide a link? -- Also, the question is more about how to avoid it, and not if it's doable or not... – krlmlr Mar 09 '12 at 10:43
  • As I was writing my comment, I noticed that my question was formulated badly. I'll start a new one. Thanks for the nice example, though. – krlmlr Mar 09 '12 at 10:49
  • Yes the question is badly formulated if you want to avoid the tikzpicture inside a node but why to you want to avoid this ? Sometimes it's very useful to do this ! A node is useful to place something ( text, picture or tikzpicture) inside a tikzpicture! – Alain Matthes Mar 09 '12 at 10:52
  • I just don't want to bother with resetting attributes to default values. – krlmlr Mar 09 '12 at 10:58
  • It's the aim of my question http://tex.stackexchange.com/questions/46818/ We need to answer this question before to give a correct answer to yours – Alain Matthes Mar 09 '12 at 11:02
  • Well, I'm not asking for which attributes to save/restore. I'm asking if there's a way to save/restore everything. This could be possible if there were a way to iterate over all known TikZ/PGF attributes, and would be more generic/general that what you have asked for. Just another way of looking at the problem, I guess -- not intending to compete. – krlmlr Mar 09 '12 at 11:04
  • Which technique would you suggest here with nesting Tikz pics side-by-side where four pictures meant to be placed 2x2 grid? Instead of scope, I defined local variables for each node. – hhh Jun 11 '16 at 21:09