6

I have two TikZ drawings I would like to combine. One I have drawn 'in 2D', the other I have drawn 'in 3D'. My real drawings are very complicated, but I have created a minimal example to illustrate what I'm trying to do.

Minimal 2D example

\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\usepackage{graphicx}

\begin{document}
\begin{tikzpicture}

\draw[fill={rgb:blue,1;white,5}] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;

\node at (1,1) (ImageA) {\includegraphics[width=1.5cm]{example-image-a}};

\coordinate (Origin) at (0,0);
\draw[fill=red] (Origin) circle (0.05cm);

\node at (3,2) (Label) {Hello!}; 
\draw[->] (Label) -- (ImageA);

\end{tikzpicture}
\end{document}

enter image description here

Minimal 3D example

\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\usepackage{graphicx}

\begin{document}
\begin{tikzpicture}[y={(-1cm,0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)}]

\draw[fill={rgb:red,1;white,5}] (0,0,0) -- (3,0,0) -- (3,2,0) -- (0,2,0) -- cycle;

\end{tikzpicture}
\end{document}

enter image description here

What I want the combined drawing to look like:

enter image description here

Is there an easy way to achieve this? My real 2D drawing is really complicated and having to manually re-code every element to make it work in my 3D drawing would be very hard work I feel. Similarly, my 3D drawing is really complicated and I don't want to have to re-code that.

I was hoping there is some easy way to do this. For example, could I select a new origin for my 2D drawing in the 3D drawing, and it re-adjusts my existing 2D code automatically?

Milo
  • 9,440

1 Answers1

11

It is rather easy with the 3d library, using this answer and that follow-up answer. Notice that the image gets reflected because you chose the y-direction negative.

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{3d}

\begin{document}

\begin{tikzpicture}[y={(-1cm,0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)}]
\draw[fill={rgb:red,1;white,5}] (0,0,0) -- (3,0,0) -- (3,2,0) -- (0,2,0) -- cycle;
\begin{scope}[canvas is yz plane at x=1.25]
\draw[fill={rgb:blue,1;white,5}] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
\node[transform shape] at (1,1) (ImageA)  {\includegraphics[width=1.5cm]{example-image.pdf}};
\coordinate (Origin) at (0,0);
\draw[fill=red] (Origin) circle (0.05cm);
\node at (3,2) (Label) {Hello!}; 
\draw[->] (Label) -- (ImageA);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

If you want the picture not getting reflected, you only need to multiply the y-direction by -1.

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{3d}

\begin{document}

\begin{tikzpicture}[y={(1cm,-0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)}]
\draw[fill={rgb:red,1;white,5}] (0,0,0) -- (3,0,0) -- (3,2,0) -- (0,2,0) -- cycle;
\begin{scope}[canvas is yz plane at x=1.25]
\draw[fill={rgb:blue,1;white,5}] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
\node[transform shape] at (1,1) (ImageA)  {\includegraphics[width=1.5cm]{example-image.pdf}};
\coordinate (Origin) at (0,0);
\draw[fill=red] (Origin) circle (0.05cm);
\node at (3,2) (Label) {Hello!}; 
\draw[->] (Label) -- (ImageA);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

ADDENDUM: Using the original definition of y, flipping the image and the hello label (xscale=-1 appears odd but the y-direction is the x-direction in the scope), and enhancing the 3D feel.

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{3d}

\begin{document}

\begin{tikzpicture}[y={(-1cm,0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)}]
\draw[fill=red,opacity=0.3] (1.5,0,0) -- (3,0,0) -- (3,2,0) --
(1.5,2,0) -- cycle;
\begin{scope}[canvas is yz plane at x=1.5]
\draw[fill=blue,opacity=0.3] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
\node[transform shape,opacity=0.5,xscale=-1] at (1,1) (ImageA)  {\includegraphics[width=1.5cm]{example-image.pdf}};
\node[transform shape,xscale=-1] at (3,2) (Label) {Hello!}; 
\draw[->] (Label) -- (ImageA);
\end{scope}
\draw[fill=red,opacity=0.3] (0,0,0) -- (1.5,0,0) -- (1.5,2,0) -- (0,2,0) -- cycle;
\begin{scope}[canvas is yz plane at x=1.5]
\coordinate (Origin) at (0,0);
\shade[ball color=red] (Origin) circle (0.05cm);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

EDIT: Had a screwed-up example-image in my path and calc was not needed.