2

I have a larger document from which I want to extract all tikz/pgfplots figures into standalone documents which I then include in my main document with \includegraphics. However, I usually set the width of my pgfplots figures based on the textwidth. E.g., I put width=\textwidth in the begin{axis} part of a pgfplot. How can I make sure that the standalone document has exactly the same width, such that the figure fits exactly in my main document?

Below is a MWE of my current best guess, including a situation with two side-by-side figures that I frequently encounter. It appears to work reasonably well, but there is still a slight difference in output between the inline and standalone figures. Can this be eliminated as well?

Main file:

\documentclass[a4paper, 11pt, DIV=12, BCOR=2mm]{scrbook}

\usepackage{pgfplots}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{layouts}

\begin{document}
% Figure out textwidth of the main document
\prntlen{\textwidth}
\begin{figure}
    \begin{subfigure}[t]{.5\textwidth}
        \begin{tikzpicture}
            \begin{axis}
                [width=\textwidth]
                    \addplot [domain=0:4]{x^2};
            \end{axis}
        \end{tikzpicture}
    \end{subfigure}
    \begin{subfigure}[t]{.5\textwidth}
        \begin{tikzpicture}
        \begin{axis}
        [width=\textwidth]
            \addplot [domain=0:4]{sqrt(x)};
        \end{axis}
        \end{tikzpicture}
    \end{subfigure} 
    \caption{Inline figures}
\end{figure}

\begin{figure}
    \begin{subfigure}[t]{.5\textwidth}
        \includegraphics{mwe-1}
    \end{subfigure}
    \begin{subfigure}[t]{.5\textwidth}
        \includegraphics{mwe-2}
    \end{subfigure} 
    \caption{Standalone figures}
\end{figure}
\end{document}

mwe-1.tex (mwe-2.tex looks equivalent):

\documentclass[varwidth=443.86305pt]{standalone}  % varwidth set to textwidth of main doc
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
        [width=.5\textwidth]
            \addplot [domain=0:4]{x^2};
        \end{axis}
    \end{tikzpicture}       
\end{document}

Output (the y axes are shifted a bit to the left in the standalone version): enter image description here

4
  • Why do you want to store the figures in standalone documents? If it is to speed up compilation time then you could also investigate the tikzexternalize library, which is intended for that purpose.
    – Marijn
    Jun 25, 2020 at 12:07
  • 4
    @Marijn tikzexternalize does not always work well. So I'd also do this.
    – daleif
    Jun 25, 2020 at 12:17
  • 1
    @daleif, @Marijn I used tikzexternalize for a long time but to be honest it didn't work out really well for me and often created various problems. My hope is that this approach is more robust.
    – Eike P.
    Jun 25, 2020 at 15:08
  • Whenever I create images for a doc like this I create then in the size they need to be in, thus no scaling. Yes annoying if you have to redo do one because you need it in another size, so be it. Here of course standalone have no idea what the relevant text width is.
    – daleif
    Jun 25, 2020 at 15:11

1 Answer 1

0

You have some spaces and newlines. Standalone uses 10pt font as default, and you use 11pt in the main document. This means that the axis tick labels does not take up the same space which moves the whole plot. You could use scale only axis, if you really want to have different font sizes. Instead of \includegraphics consider using \includestandalone.

Working example:

main.tex

\documentclass[a4paper, 11pt]{scrbook}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{subcaption}
\usepackage{layouts}
\usepackage{float}
\begin{document}
\begin{figure}[H]%
\begin{subfigure}[t]{.5\textwidth}
\begin{tikzpicture}[remember picture]
\begin{axis}[width=\textwidth]
\addplot[domain=0:4]{x^2};
\coordinate (a) at (current axis.south west);
\coordinate (b) at (current axis.south east);
\end{axis}
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\coordinate (c) at (current bounding box.south west);
\coordinate (d) at (current bounding box.south east);
\end{tikzpicture}
\end{subfigure}%
\caption{Inline figures}
\end{figure}

\begin{figure}[H]%
\begin{subfigure}[t]{.5\textwidth}
\includegraphics{subfig}
\end{subfigure}%
\caption{Standalone figures}
\end{figure}

\tikz[remember picture, overlay]{\draw[cyan] (a) -- +(0,-10) (b) -- +(0,-10) (c) -- +(0,-10) (d) -- +(0,-10);}
\prntlen{\textwidth}

\end{document}

subfig.tex

\documentclass[varwidth=418.25555pt, 11pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=0.5\textwidth]
\addplot[domain=0:4]{x^2};
\end{axis}
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\end{document}

Two graphs aligned under each other with lines showing the alignment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .