10

I've just found out that the command

\includestandalone[<options>]{<file>}

exists.

Which are its advantages/disadvantages with respect to the \includegraphics{<file>} of the output image?

For example, if I have myfile.tex:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[fill=red]{A};
\end{tikzpicture}
\end{document}

with \includestandalone I have to put in the preamble of the main file all the packages present in the standalone preamble:

\documentclass{article}
\usepackage{tikz}
\usepackage{standalone}
\begin{document}
\includestandalone{myfile}
\end{document}

whereas if I only include the output, of course, I don't need them:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{myfile}
\end{document}
  • 5
    Looking at the package code of standalone it looks like it checks the modification time of the .tex file and if it is newer than that of the .pdf file then it starts a standalone job to update the .pdf file. It looks like the externalisation thing in TikZ. So I'd say that an advantage is that it updates the file if necessary. There's probably much more than that, but this is what I saw by quickly looking into the code. – Phelype Oleinik May 19 at 18:18
  • 1
    @PhelypeOleinik Thank you, you could also write an answer if you like. – CarLaTeX May 19 at 18:34
  • 5
    As I understand it it is in effect a variation of the principles of subfiles. That is the subfiles are potentially compilable standalone OR can be included in which case they are not only physically cropped area but also tex is cropped from begin{document} to end{document} thus why it needs the preambles to be included for those imported commands – KJO May 19 at 18:36
  • @KJO Thanks, you could write an answer, too, if you like. – CarLaTeX May 19 at 18:41
7

This is one advantage, I do not claim it is the most important one. It goes in the same direction as the comments by Phelype and KJO, and is essentially copied from here. Consider this subfile sub.tex.

\documentclass[tikz]{standalone}
\begin{document}
\ifstandalone
\tikzset{my style/.style={line width=3pt}}
\fi
\begin{tikzpicture} 
\draw[->,my style] (0,1cm)--++(4,-5);
\end{tikzpicture}
\end{document}

If you compile it, you get

enter image description here

However, you can also embed and modify it in a main document.

\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\begin{document}

\begin{figure}[h]
\tikzset{my style/.style={line width=15pt}}
\includestandalone[mode=tex]{sub}
\caption{Version 1.}
\end{figure}

\begin{figure}[h]
\tikzset{my style/.style={red, line width=7pt}}
\includestandalone[mode=tex]{sub}\caption{Version 2.}
\end{figure}

\end{document}

enter image description here

As you can see, you can manipulate it from outside which you can't if you use \includegraphics.

Of course, you can generalize this example in many ways. Within TikZ, you can play with every node and the like, but this discussion is certainly restricted to TikZ. With

\ifstandalone
<some defintions>
\fi

you can create many examples where you define a macro in one way if it is compiled as standalone and in other ways depending on which document embeds the stuff. I started using these things for figures that get embedded in either notes or beamer presentations, but do not have used them extensively. So far these tricks work great.

  • 1
    +1, interesting! – CarLaTeX May 19 at 19:57
  • @CarLaTeX I think Marmot has it covered but I was playing with \begin{document} \input{carlatex} \scalebox{2}{\input{Marmot}} Hats off to you M'am \end{document} :-} edit PS my Marmot was a variation on Issue#1 in GitHub issues with a 40^ jaunty hat e.g. \begin{tikzpicture} \marmot \thing[hat,xshift=1.2cm,yshift=-0.9cm,rotate=40] – KJO May 19 at 22:33

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.