# how to save the final box at the end of tikzpicture?

Perhaps the question is unclear. I know how to put a tikzpicture inside a box. I made it in this question like this

\newsavebox{\mycandle}
\savebox{\mycandle}{%
\begin{tikzpicture}[scale=.1]
...
\end{tikzpicture}
}


But in this case I put a box (\pgfpicperhaps it's the final box) into a box and I would like to know if it's possible to save the final box when \endtikzpicture arrives.

I'm not sure but I think the good box is \pgfpic. In the file pgfcorescopes.code.tex and at the end of the macro \endpgfpicture, I found \pgfsys@endpicture and then \pgfsys@typesetpicturebox\pgfpic. Th first seems to depend of the driver and I don't understand the code and for the second macro \pgfsys@typesetpicturebox the definition is inside the file pgfsys.code.tex

Here the code of this macro :

\def\pgfsys@typesetpicturebox#1{%
\pgf@ya=\pgf@shift@baseline\relax%
%
%
\advance\pgf@picmaxy by-\pgf@picminy\relax% maxy is now the height
\advance\pgf@picmaxx by-\pgf@picminx\relax% maxx is now the width
\setbox#1=\hbox{\hskip-\pgf@picminx\lower\pgf@picminy\box#1}%
\ht#1=\pgf@picmaxy%
\wd#1=\pgf@picmaxx%
\dp#1=0pt%
\leavevmode%
\pgf@xa=\pgf@trimleft@final\relax  \ifdim\pgf@xa=0pt \else\kern\pgf@xa\fi
\raise-\pgf@ya\box#1%
\pgf@xa=\pgf@trimright@final\relax \ifdim\pgf@xa=0pt \else\kern\pgf@xa\fi
}


% Called after a picture has been typeset in box #1. This
% command should insert the box into the normal TeX code. The box #1
% will still be a raw'' box that contains only the \special's. The
% job of this command is to resize and shift this box according to the
% baseline shift and the size of the box.
%
% This command has a default implementation.


Well I'm lost in all these codes and I'm not sure if what I want is possible. Perhaps it's a bad idea.

-
The code of the spy library has a box \copy application which may be inspiring for you. –  percusse May 12 '12 at 17:36
Interesting and fine idea ! I need to give a look at this code but at first glance, the code is short but ... without comments. –  Alain Matthes May 12 '12 at 18:06
Yes, it's a little bit difficult to make your way around it but the general idea is to use the box content in the path picture option. If you look at the manual for path picture it's a little bit easier to understand the motivation. –  percusse May 12 '12 at 18:13

If I say

\newsavebox{\mycandle}
\savebox{\mycandle}{%
\begin{tikzpicture}[scale=.1]
\draw (0,0) -- (1,1) ;
\end{tikzpicture}}


then the log from \showbox\mycandle (with high values of \showboxbreadth and \showboxdepth) is

> \box36=
\hbox(3.24544+0.0)x3.24544
.\pdfcolorstack 0 push {0 g 0 G}
.\hbox(3.24544+0.0)x3.24544
..\glue 0.2
..\hbox(0.0+0.0)x0.0, shifted -0.2
...\pdfliteral{q }
...\pdfliteral{0 G }
...\pdfliteral{0 g }
...\pdfliteral{0.3985 w }
...\hbox(0.0+0.0)x0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\glue 0.0
....\pdfliteral{q }
....\glue 0.0
....\glue 0.0
....\pdfliteral{0.0 0.0 m }
....\pdfliteral{2.83484 2.83484 l }
....\pdfliteral{S }
....\pdfliteral{Q }
....\glue 0.0 plus 1.0fil minus 1.0fil
...\pdfliteral{n }
...\pdfliteral{Q }
...\glue 0.0 plus 1.0fil minus 1.0fil
.\pdfcolorstack 0 pop


So you've not the final box, which is surrounded by suitable \pdfcolorstack instruction (in pdflatex). They become \special{color push gray 0} and \special{color pop} with latex or xelatex. I don't think it's really safe trying to get the inner box before those paired commands are issued.

-
Finally "The box #1 will still be a raw'' box that contains only the \special's." is the problem ? I think you are right when you say "it's not really safe trying ...". So it was a bad idea! –  Alain Matthes May 12 '12 at 17:33