## The tikz-external solution

You can use the tikz external library to cache tikz drawings:

\usetikzlibrary{external}
\tikzexternalize[prefix=tikz/]


Note that the \tikzexternalize command must be given in the main .tex file of the project, for tikz-externalization to work on Overleaf.

You will then need to create a tikz folder in your project. For this to work on Overleaf, the folder you create needs to have a (dummy) file in it—for example, you can add a blank foo.txt file in the tikz folder.

As the externalised tikz PDFs are considered as output files, you will not see them show up in the file list, but they are indeed present within the project. You can view the list of generated files to see them.

Some commands from other packages may cause errors in externalization mode; for example, \includepdf from the pdfpages. To get around this, you will need to "optimise" such commands away:

\tikzexternalize[prefix=tikz/,optimize command away=\includepdf]


## There are too many of them!

However, if your drawings are too complex and there are too many of them, you will still get a timeout the first time the externalised files are generated. Building your project incrementally (by commenting different chunks of your project) may help, but bear in mind that all generated files are garbage collected at fixed intervals on the servers. Your project may then fail to compile when you next log in, and you'd have to re-generate them all incrementally again.

### Generating the drawings locally

As a workaround, you can generate the files on a local machine first, by downloading your project and compiling it on your own local machine. After the compile completes, you should see the tikz folder containing .pdf, .md5, .dpth and .dpth files for each of your tikz drawings. If you then upload these files to your Overleaf project's tikz folder, your project will then use these files directly, and compilation should be fairly fast. These files won't be deleted by the server, as uploaded file are never garbage-collected.

A caveat: if you change your tikz code in future, you may have to delete the corresponding files in the tikz folder first, so that Overleaf can generate and use a new version. Alternatively, you can re-generate the files on your machine, and re-upload them to Overleaf. Remember that you need to replace all the .pdf, .md5, .dep and .dpth for that particular tikz drawing. On Overleaf v2, these files must be prefixed with output, instead of the filename of your .tex.

### Generating the drawings in a separate project

Alternatively you can create a separate project that just contains the figure(s), you can include the output of the figure in your original project by clicking on the "Upload files" icon above the file list panel, and then choosing From another project. Select the project containing the tikz drawings, and then the select from output files option.

That way, you only have to compile the complicated figure once, and then you can include the resulting pdf as a normal figure with \includegraphics[page=1]{drawings.pdf} in your main project.

The separate project containing the tikz drawings can use the standalone or preview package, to produce output PDFs that are tightly cropped.

Here's some sample code using the standalone package:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node[fill=yellow]{Hello!};
\end{tikzpicture}

\begin{tikzpicture}
\node[fill=pink]{World!};
\end{tikzpicture}
\end{document}


And here's some sample code using the preview package:

\documentclass{article}
\usepackage[active,tightpage]{preview}
\setlength{\PreviewBorder}{0pt}
\usepackage{tikz}
\PreviewEnvironment[{[]}]{tikzpicture}
\begin{document}
\begin{tikzpicture}
\node[fill=yellow]{Hello!};
\end{tikzpicture}

\begin{tikzpicture}
\node[fill=pink]{World!};
\end{tikzpicture}
\end{document}