overlay a tikz picture with another

maybe a duplicate of how to overlay a tikz picture with another

but there is no solution

ive been trying to overlay a tikzpicture with another This code does not work

\setbox0=\vbox{\hbox{%
\begin{tikzpicture}
% ...
\end{tizpicture}%
}}
%The width of the following picture is {\the\wd0}.
\box0
\hspace*{-\the\wd0}
\begin{tikzpicture}
% ...
\end{tizpicture}%


maybe it is of interest that the first picture is externalized?

If i used overlay the whole picture is randomly cropped to a 1000th or so.

Thanks guys for your support. Here is the mwe:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{external}
\usepackage{scalefnt}
\tikzexternalize
\begin{document}
\tikzset{external/only named=true} % prevent unnamed graphics from externalizing - that is needed since externalizing strips annotations

{\scalefont{0.25}
\tikzsetnextfilename{test}
\begin{tikzpicture}
\draw (-1,0) -- (1,0);
\draw[blue] (0,-1) -- (0,1);
\node at (5,5) {A};
\end{tikzpicture}
} % scalefont
{\scalefont{0.25}
\llap{
\begin{tikzpicture}
\draw[red] (-1,0) -- (1,0);
\draw (0,-1) -- (0,1);
\node at (-5,-5) {B};
\end{tikzpicture}
} % llap
} % scalefont
\end{document}


As you can see: The pictures are not overlayed I want to achieve that the 'cross' of both pictures is at the exact same coordinate. In the end, only the black/blue cross would be shown (since red/black would be overlayed) And both nodes would be shown as well (since none of them is overlayed)

Second edit:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{external}
\usepackage{scalefnt}
\begin{document}

{\scalefont{0.25}
\begin{tikzpicture}[remember picture]
\draw (-1,0) -- (1,0);
\draw[blue] (0,-1) -- (0,1);
\node at (5,5) {A};
\end{tikzpicture}
} % scalefont
{\scalefont{0.25}
\llap{
\begin{tikzpicture}[remember picture,overlay]
\draw[red] (-1,0) -- (1,0);
\draw (0,-1) -- (0,1);
\node at (-5,-5) {B};
\end{tikzpicture}
} % llap
} % scalefont
\end{document}


overlay and remember does not work either (at least as I intend)

Edit 3: thanks AlexG

\documentclass{standalone}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{external}

\tikzexternalize
\begin{document}
\tikzset{external/only named=true}
\makebox[0pt][l]{
\tikzsetnextfilename{tt}
\begin{tikzpicture}
\useasboundingbox (-10,-10) -- (10,10);
\node[draw=green] at (0,0) {};
\end{tikzpicture}
} % makebox
\begin{tikzpicture}
\useasboundingbox (-10,-10) -- (10,10);
\node[draw=green] at (0,0) {};
\end{tikzpicture}

\end{document}


It yields

• It's very hard to understand exactly what you need, since you provided only a skeleton of a code. A way to do this may be ti use backgrounds library and draw each tikzpicture on a different layer. If you could provide a more specific MWE, we should be able to help. Apr 21 '21 at 19:15
• \box0 first inserts, then purges the box content. The following \wd0 returns zero. Better set box width to zero before inserting it, \wd0=0pt\box0, and remove \hspace.... Apr 21 '21 at 19:51
• Maybe an idea: put the code of each picture into a \begin{scope} \end{scope} environment and move them with option [xshift=..., yshift=...]. Apr 21 '21 at 21:55
• as Alex says \hspace*{-\the\wd0} is \hspace{0pt} but yo don't need teh low level primitives you could have used \sbox and \usebox no need to vbox/hbox etc. or simpler just do \begin{tikzpicture}...\end{tikzpicture}\llap{\begin{tikzpicture}...\end{tikzpicture}} Apr 21 '21 at 21:59
• Thanks for your support, I added an mwe with picture and cleared the problem hopefully (actually its not an mwe, since the scalefont is per defintion not "minimal" (also the external thing does not add to the problem)) - but both is relevant in the product version Apr 22 '21 at 18:40

If you want to exactly overlay both pictures, they should have the same bounding box coordinates.

Command

\useasboundingbox ... ;


can be used for this. It doesn't put any visible marks on the page but forces the object (here: a diagonal line) do be used for calculating the final bounding box.

In general, LaTeX-type boxes, such as \makebox, should be preferred over low-level box commands of the TeX layer.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize % activate!
\tikzset{external/only named=true}

\begin{document}

\tikzsetnextfilename{tt}%
\makebox[0pt][l]{\begin{tikzpicture}[node font=\tiny]
\useasboundingbox (-5.2,-5.2) -- (5.2,5.2);
\draw (-1,0) -- (1,0);
\draw[blue] (0,-1) -- (0,1);
\node at (5,5) {A};
\end{tikzpicture}}%
\begin{tikzpicture}[node font=\tiny]
\useasboundingbox (-5.2,-5.2) -- (5.2,5.2);
\draw[red] (-1,0) -- (1,0);
\draw (0,-1) -- (0,1);
\node at (-5,-5) {B};
\end{tikzpicture}

\end{document}

• thank you - that seems as easy as i expected. But your solution implies that the boundbox size is known. What would I do if I only know that both (0,0) should be at the exact same point? Apr 23 '21 at 8:02
• it also does not work completely: if I used externalize there one picture is shifted a bit - is that a fix shift and intended? without externalize the problem does not occur - see edit Apr 23 '21 at 8:18
• For me, outputs with and without externalisation look the same. Apr 23 '21 at 9:06
• also if you use the example with the green boxes? one difference is that both of your pictures are externalized while only the first on mine is. - that is the reason for this. Why is this? Apr 23 '21 at 9:37
• I edited my code to only have the first one of the pictures externalized, but still, I get them exactly overlayed. Apr 23 '21 at 11:52