# Join graphs tikz + pgfplots

I have two graphics that I would like to join into one but even more I would like to put them inside text, something like this:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{lipsum}

\begin{document}
\lipsum[66]
\begin{figure}[h]
\centering
...
\end{figure}
\lipsum[66]
\end{document}


the codes of my graphs are the following:

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usepackage[spanish,es-noshorthands]{babel}
\begin{document}

\begin{tikzpicture}
\draw[-stealth] (-1,0) -- (2.5,0) node[below]{$u$};;
\draw[-stealth] (0,-1) -- (0,2.5) node[left]{$v$};
\draw[blue] (-0.8,2)--(2.3,2);
\draw[blue] (-0.8,0.5)--(2.3,0.5);
\draw[red] (2,-0.8)--(2,2.3);
\draw[red] (0.5,-0.8)--(0.5,2.3);

\node at (0,2) [blue, font=\footnotesize, below left]{$1$};
\node at (0,0.5) [blue, font=\footnotesize, below left]{$1$};
\node at (2,0) [red, font=\footnotesize, below left]{$1$};
\node at (0.5,0) [red, font=\footnotesize, below left]{$1$};

\end{tikzpicture}
\end{document}


and

\documentclass[tikz,border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16,width=10cm}
\tikzset{declare function={torusx(\u,\v,\R,\r)=cos(\u)*(\R + \r*cos(\v));
torusy(\u,\v,\R,\r)=(\R + \r*cos(\v))*sin(\u);
torusz(\u,\v,\R,\r)=\r*sin(\v);
vcrit1(\u,\th)=atan(tan(\th)*sin(\u));% first critical v value
vcrit2(\u,\th)=180+atan(tan(\th)*sin(\u));% second critical v value
vtest(\u,\v,\az,\el)=sin(-vcrit1(\u-\az,\el)+\v);
disc(\th,\R,\r)=((pow(\r,2)-pow(\R,2))*pow(cot(\th),2)+%
pow(\r,2)*(2+pow(tan(\th),2)))/pow(\R,2);% discriminant
umax(\th,\R,\r)=ifthenelse(disc(\th,\R,\r)>0,asin(sqrt(abs(disc(\th,\R,\r)))),0);
}}
\pgfplotsset{visible stretch/.style={restrict expr to domain={vtest(atan2(rawy,rawx),%
ifthenelse(veclen(rawx,rawy)>\R,asin(rawz/\r),180-asin(rawz/\r)),\pgfkeysvalueof{/pgfplots/view/az},\pgfkeysvalueof{/pgfplots/view/el})}{-0.05:1.1}},
hidden stretch/.style={restrict expr to
domain={vtest(atan2(rawy,rawx),%
ifthenelse(veclen(rawx,rawy)>\R,asin(rawz/\r),180-asin(rawz/\r)),\pgfkeysvalueof{/pgfplots/view/az},\pgfkeysvalueof{/pgfplots/view/el})}{-1.1:0.05}}}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\R}{2}
\pgfmathsetmacro{\r}{1}
\pgfplotsset{view={35}{60},axis lines=none}
\begin{axis}[]
\pgfplotsinvokeforeach{0,10,...,350}
{\addplot3[samples y=0,domain=0:361,smooth,samples=71,ultra thin,gray!30]
({torusx(x,#1+x/12,\R,\r)},
{torusy(x,#1+x/12,\R,\r)},
{torusz(x,#1+x/12,\R,\r)});
\addplot3[samples y=0,domain=0:360,smooth,samples=71,ultra thin,gray!30]
({torusx(#1,x,\R,\r)},
{torusy(#1,x,\R,\r)},
{torusz(#1,x,\R,\r)});
}
\pgfplotsinvokeforeach{0,10,...,350}
{\addplot3[samples y=0,domain=0:361,smooth,samples=71,visible stretch,very
thin,gray!50]
({torusx(x,#1+x/12,\R,\r)},
{torusy(x,#1+x/12,\R,\r)},
{torusz(x,#1+x/12,\R,\r)});
\addplot3[samples y=0,domain=0:360,smooth,samples=71,visible stretch,very
thin,gray!50]
({torusx(#1,x,\R,\r)},
{torusy(#1,x,\R,\r)},
{torusz(#1,x,\R,\r)});}
\addplot3[samples y=0,domain=0:360,smooth,samples=71,opacity=0.6,
red,very thick,dashed]
({torusx(-90,x,\R,\r)},
{torusy(-90,x,\R,\r)},
{torusz(-90,x,\R,\r)});
\addplot3[samples y=0,domain=0:360,smooth,samples=71,visible
stretch,red,very thick]
({torusx(-90,x,\R,\r)},
{torusy(-90,x,\R,\r)},
{torusz(-90,x,\R,\r)});

\addplot3[samples y=0,domain=0:360,smooth,samples=71,opacity=0.6,
red,very thick,dashed]
({torusx(0,x,\R,\r)},
{torusy(0,x,\R,\r)},
{torusz(0,x,\R,\r)});
\addplot3[samples y=0,domain=0:360,smooth,samples=71,visible
stretch,red,very thick]
({torusx(0,x,\R,\r)},
{torusy(0,x,\R,\r)},
{torusz(0,x,\R,\r)});
\addplot3[samples y=0,domain=0:360,smooth,samples=71,opacity=0.6,
blue,very thick,dashed]
({torusx(x,0,\R,\r)},
{torusy(x,0,\R,\r)},
{torusz(x,0,\R,\r)});
\addplot3[samples y=0,domain=0:360,smooth,samples=71,visible
stretch,blue,very thick]
({torusx(x,0,\R,\r)},
{torusy(x,0,\R,\r)},
{torusz(x,0,\R,\r)});
\addplot3[samples y=0,domain=0:360,smooth,samples=71,opacity=0.6,
blue,very thick,dashed]
({torusx(x,80,\R,\r)},
{torusy(x,80,\R,\r)},
{torusz(x,80,\R,\r)});
\addplot3[samples y=0,domain=0:360,smooth,samples=71,visible
stretch,blue,very thick]
({torusx(x,80,\R,\r)},
{torusy(x,80,\R,\r)},
{torusz(x,80,\R,\r)});
\end{axis};
\end{tikzpicture}
\end{document}



I would like the result to be something like this:

## 1 Answer

Not really sure if I understand correctly your question... if you want the two diagram together, just put them together. You can use the baseline option for vertical alignment, or you can look here.

I saved your torus graph (which is quite heavy to compute) in a file called torus.pdf. Then you just put the two graphics on the same line:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16,width=10cm}
\usepackage{lipsum}
\usepackage[spanish,es-noshorthands]{babel}
\begin{document}
\lipsum[66]
\begin{figure}[h]
\centering
\begin{tikzpicture}[baseline=-1cm]
\draw[-stealth] (-1,0) -- (2.5,0) node[below]{$u$};;
\draw[-stealth] (0,-1) -- (0,2.5) node[left]{$v$};
\draw[blue] (-0.8,2)--(2.3,2);
\draw[blue] (-0.8,0.5)--(2.3,0.5);
\draw[red] (2,-0.8)--(2,2.3);
\draw[red] (0.5,-0.8)--(0.5,2.3);

\node at (0,2) [blue, font=\footnotesize, below left]{$1$};
\node at (0,0.5) [blue, font=\footnotesize, below left]{$1$};
\node at (2,0) [red, font=\footnotesize, below left]{$1$};
\node at (0.5,0) [red, font=\footnotesize, below left]{$1$};

% Add the arrow!
\draw[thick, ->] (3, 1.5) to[bend left] ++(2,0);

\end{tikzpicture}
\includegraphics[scale=0.6]{torus.pdf}
\end{figure}
\lipsum[66]
\end{document}


But notice that if you want the diagram always in that position, you should not use figure --- that environment is for floating figures. Just use a center environment in that case, otherwise, if there is no space for the figure it will move around.

• Saving it in pdf does not increase the size of my document? Is it possible to compile the file without the need to save the torus in pdf? May 11 at 13:18
• I do not think --- the code generated is PDF anyways, so I expect very small differences. But you can try --- you just put your tikz code instead of the includegraphics. Just be sure to not add a blank line (which will put the images one on top of the other, because there will be a paragraph). May 11 at 13:25
• Checked: the file with the includegraphics is 236443 bytes. Without, with the tikzpicture embedded, is 236209 bytes. Virtually the same. The difference is that the compilation with the includegraphics is less than 3 seconds, and 1 minute 10 seconds with the plain TikZ included. May 11 at 13:34
• You are very right, thank you very much. If I wanted my chart to not take up so much space, do you have any advice? May 11 at 13:46
• This is another question --- better post it, you have the MWE ready ;-). But you have 2 roads: reduce the resolution (less points per circle, for example) or create a raster image (for example, a 200dpi jpg will be around 30kbyte) and include it with includegraphics. (see for example stackoverflow.com/questions/6605006/…) May 11 at 14:36