Graphics, Figures & TablesDuplicate Tikz images

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
m_d6712
Posts: 3
Joined: Tue Apr 07, 2015 3:33 pm

Duplicate Tikz images

Post by m_d6712 »

I have a tikz image in one of the slides in my beamer.
I need to duplicate this image in many other slides. I want to avoid repeating the whole code again and again. Is there any way to force latex to compile the picture once and use it in many places?

Code: Select all

\usepackage{tikz}
\usepackage{tikz-timing}
\usepackage{tikz-qtree}
\usetikzlibrary{arrows,shapes,positioning,shadows,trees,scopes,snakes}

\begin{figure}[t]
\begin{center}
\begin{tikzpicture}[node distance=1cm,font=\scriptsize,
node/.style={circle,draw=brown!90,fill=orange!60,thick,inner sep=0pt,minimum size=6mm},
dummy/.style={node distance=1cm, rectangle,draw=blue!60,fill=blue!20,inner sep=0pt,minimum size=2mm}
]
\node[dummy] (D1)  {};
\node[node,label={$\mu_i$,$r_{i1}$}] (J1) [left=of D1] {$i$};
\node[node,label={3,1}] (J2) [right=of D1] {2};
\node[node,label={1,4}] (J1) [above=of J2] {1};
\node[node,label={2,2}] (J3) [right=of J2] {3};
\node[dummy] (D2)  [right=of J3]{};
\draw [->] (J2) to (J3);
\draw [->] (D1) to (J1);
\draw [->] (D1) to (J2);
\draw [->] (J3) to (D2);
\draw [->] (J1) to (D2);
\
\end{tikzpicture}
%\caption{\footnotesize An example project in which activity durations follow exponential distributions and resource availability equals five.}
\label{fig:Example}
\end{center}
\end{figure}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

Duplicate Tikz images

Post by Stefan Kottwitz »

Welcome to the forum!

You could simply define a macro via \newcommand, which contains the TikZ-Code. Another possibility would be saving the TikZ picture in a box and printing that on various slides, but I think the macro version is fine. And it's the usual LaTeX way.

Stefan
LaTeX.org admin
m_d6712
Posts: 3
Joined: Tue Apr 07, 2015 3:33 pm

Duplicate Tikz images

Post by m_d6712 »

Stefan_K wrote:Welcome to the forum!

You could simply define a macro via \newcommand, which contains the TikZ-Code. Another possibility would be saving the TikZ picture in a box and printing that on various slides, but I think the macro version is fine. And it's the usual LaTeX way.

Stefan

Thank you for your response.
I know that would be possible.
Mostly, I want to avoid multiple compile of the same image, which is still a problem when we use a macro. I hope a more convenient way other than saving the image exist.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Duplicate Tikz images

Post by Johannes_B »

You could compile the image externally and include the image multiple times. If the image changes, do a fresh compile.

Or use the tikz library externalize. Depending on how many different tikz-pictures are involved, the first approach would be simpler.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
m_d6712
Posts: 3
Joined: Tue Apr 07, 2015 3:33 pm

Duplicate Tikz images

Post by m_d6712 »

Johannes_B wrote:You could compile the image externally and include the image multiple times. If the image changes, do a fresh compile.

Or use the tikz library externalize. Depending on how many different tikz-pictures are involved, the first approach would be simpler.

Thanks,
The library should work.
Post Reply