Using pdf image of a standalone .tex in the main document on Overleaf

asymptoteoverleaftikz-pgf

On my computer, in the same folder, I create the main document main.tex and a figure disc.tex – a standalone TikZ/Asymptote code. First run disc.tex to get disc.pdf, then run main.tex (there is \includegraphics{disc.pdf} in it). It works as usual. This way helps increasing compilation time, especially when there are a lot of figures in the main document.

enter image description here

The content of main.tex:

\documentclass{article}
\usepackage{graphics,lipsum}
\begin{document}
\lipsum[1]

\begin{figure}
\centering
\includegraphics{disc.pdf}
\caption{This is my disc}
\label{fig:mydisc}
\end{figure}

\lipsum[2]
\end{document}

The content of disc.tex:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\fill[cyan!50] circle(1) node[red]{unit disc};
\end{tikzpicture}
\end{document}

How can I do a similar thing on Overleaf? In
my example project
Overleaf did not recognize where the disc.pdf is.

enter image description here

I think this situation is quite common for TeX users on Overleaf, but I have not found suitable answers on texSE yet. This answer suggested making 2 or more projects, one is for the main document and each others are for each figure, then importing output.pdfs (with name changing if necessary) to the main project. This is not so convenient: think each time we repair code of a figure, we need to import again. We write a book of 100 figures, we need one project, not 101 projects, right?

Best Answer

You could also try using the standalone package and \includestandalone in main.tex:

\documentclass{article}
\usepackage{graphicx,lipsum}
\usepackage[mode=buildnew]{standalone}
\begin{document}
\lipsum[1]

\begin{figure}
\centering
\includestandalone{disc}
\caption{This is my disc}
\label{fig:mydisc}
\end{figure}

\lipsum[2]
\end{document}

Compiling main.tex will then trigger compiling disc.tex, produce disc.pdf, and use it in main.tex.

enter image description here

You can also download disc.pdf from the generated files list.

Related Question