Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

I have a TikZ picture

\begin{tikzpicture}[scale=0.5]
\draw (-1,0) arc (180:360:1cm and 0.5cm);
\draw[dashed] (-1,0) arc (180:0:1cm and 0.5cm);
\draw (0,1) arc (90:270:0.5cm and 1cm);
\draw[dashed] (0,1) arc (90:-90:0.5cm and 1cm);
\draw (0,0) circle (1cm);
\shade[ball color=blue!10!white,opacity=0.20] (0,0) circle (1cm);
\end{tikzpicture}

which makes a small ball.

Is it possible to reuse this shape in another tikz picture?

I have another tikz picture in which I use

\draw[fill=yellow!30, fill opacity=0.5,text opacity=1] (B) circle (0.7cm) node[right] {$A$};

Instead of circle I want my small custom ball. How is that possible?

share|improve this question

marked as duplicate by Malipivo, Heiko Oberdiek, Guido, Andrew Swann, Claudio Fiandrino Apr 13 at 12:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Are you using the latest version of TikZ (3.0)? If do, this has a new way of being done. –  Loop Space Apr 13 at 11:32
    
I think this can be a duplicate of How can I draw a TikZ element multiple times against a shaded background? –  Claudio Fiandrino Apr 13 at 11:40
add comment

1 Answer 1

Unrecommended way

There are several ways, this one puts tikzpicture environment to another tikzpicture environment with help of a \node.

%! *latex mal-tikz-tikz.tex
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{tikz}
\begin{document}
\def\malpic{%
\begin{tikzpicture}[scale=0.5]
  \draw (-1,0) arc (180:360:1cm and 0.5cm);
  \draw[dashed] (-1,0) arc (180:0:1cm and 0.5cm);
  \draw (0,1) arc (90:270:0.5cm and 1cm);
  \draw[dashed] (0,1) arc (90:-90:0.5cm and 1cm);
  \draw (0,0) circle (1cm);
  \shade[ball color=blue!10!white,opacity=0.20] (0,0) circle (1cm);
  \end{tikzpicture}%
  }%End of \malpic...
\begin{tikzpicture}
\coordinate (B) at (0,0);
\node[fill=yellow!30, fill opacity=0.5,text opacity=1] at (B) {\malpic} node[right] {$A$};
\end{tikzpicture}
\end{document}

mwe

share|improve this answer
    
Embedding pictures inside pictures is generally not a great idea in TikZ. –  Loop Space Apr 13 at 11:33
1  
I agree with Andrew: this should not be encouraged. There are several alternatives, all listed in How can I draw a TikZ element multiple times against a shaded background? –  Claudio Fiandrino Apr 13 at 11:39
    
I was quite happy when I found this solution in older versions of TikZ. It is fair enough when dealing with TikZ v3 where pic and external library have been introduced. I'm marking my answer as unrecommended. –  Malipivo Apr 13 at 12:25
1  
Have a look at this thread. I answered using a new scope with custom options for the embedding picture. The thread also covers the usage of the pic. Link –  moospit Apr 13 at 12:41
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.