1

I have a main document which imports a TikZ figure contained in a standalone document. The TikZ figure uses \includegraphics inside one of the nodes, to add a PNG figure in the same folder. It compiles properly by itself, but if I have not pre-compiled it and then try to compile the main document, the PNG image is not included and the node appears empty.

Here is the file structure of the MWE:

|- main.tex
|- images
   |- standalone-image.tex
   |- image.png

main.tex

% !TeX document-id = {e9c1e577-28e1-405c-8571-6a9245078427}
% !TeX TXS-program:compile = txs:///pdflatex/[-shell-escape]
\documentclass{article}

\usepackage[mode=buildnew,subpreambles=true]{standalone}

\begin{document}

\begin{figure}
    \includestandalone[width=\linewidth]{images/standalone-image}
\end{figure}

\end{document}

standalone-image.tex

\documentclass[tikz,beamer,preview]{standalone}

\begin{document}
\begin{standaloneframe}[fragile]
\begin{tikzpicture}

    \node (n) {\includegraphics{image}};

\end{tikzpicture}
\end{standaloneframe}
\end{document}

Any idea how I can get it to compile properly in both cases? I have already played around with the import package and the \ifstandalone macro, with no success. Note that the -shell-escape flag is included using TeXstudio's "magic comments", so that cannot be the problem either.

1

To successfully compile your Tikz picture while compiling your main.tex file, you will need to put

\node (n) {\includegraphics{images/image}};

instead of

\node (n) {\includegraphics{image}};

which is the correct path of your image relatively to your main.tex file.

Using a Makefile would be a great solution to ensure that the standalone document is well compiled.

Here is an example of a Makefile that could work for you (I did not execute it).

 INPUT_DIR      := .
 INPUT          := $(INPUT_DIR)main.tex
 FIGURE_DIR     := $(INPUT_DIR)images/
 TARGET_DIR     := .
 TARGET         := $(TARGET_DIR)main.pdf

 all: figure $(TARGET)

 $(TARGET): $(INPUT_DIR)*.tex $(FIGURE_DIR)
    rubber -m pdftex --into $(TARGET_DIR) $(INPUT)

 figure:
    for file in $$(ls $(FIGURE_DIR)*.tex | cut -d \. -f 1); do \
        make -s $$file.pdf; \
    done;

%.pdf: %.tex
    rubber -m pdftex --inplace $<

.SILENT: figure $(TARGET)

(in that particular case, because rubber option --inplace change your location, you won't need to change your image path).

  • The first solution is not ideal because it goes against standalone's purpose of making the file compile both by itself or as part of a document seamlessly. About the second, it is not what I was looking for, since it means that I will have to change the way I compile the files. It is annoying for the people I cooperate with if they cannot simply open the document and build normally, and that has been problematic in the past. I was looking for a solution which uses some "pure LaTeX" macros or packages and requires no special ways of compiling, no matter the case. – Fernando Jun 27 '18 at 8:28
0

You could make tex look into the images folder to automatically find the image by using \graphicspath{{images}}

\documentclass{article}

\usepackage[mode=buildnew,subpreambles=true]{standalone}
\usepackage{tikz}

\graphicspath{{images}}

\begin{document}

\begin{figure}
    \includestandalone[width=\linewidth]{images/standalone-image}
\end{figure}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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