# how to obtain exactly same size standalone pgfplots

I am using standalone to make pdfs of my pgfplots to later include as an image into my main document. The y-axis are different for some of the plots (some negative, some positive), thus the size of the pgfplot are cropped differently, so even if I force height and width to be the same in the main document, the plots are not the same size. Is there a way to achieve exactly the same size plots even if the axes are slightly different?

\documentclass[a4paper, twoside, 12pt]{report}
\usepackage[a4paper,showframe,width=150mm,top=25mm,bottom=25mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{fixltx2e}
\usepackage{siunitx}
\usepackage{subfig}
\usepackage{xcolor}
\usepackage[version=4]{mhchem}
\DeclareSIUnit{\molar}{M}

\pagestyle{empty}
\begin{document}

\begin{figure}[h]
\centering
\hspace*{\fill}%
\subfloat[]{%
\includegraphics[width=7cm,height=6cm]{./fittingcurve/ST039LIMD5.pdf}}
\hfill%
\subfloat[]{%
\includegraphics[width=7cm,height=6cm]{./fittingcurve/ST018LIMD100.pdf}}
\hspace*{\fill}%

\caption{}
\end{figure}
\end{document}


Here is the standalone code:

\documentclass[tikz,border=1pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{fixltx2e}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage[version=4]{mhchem}
\DeclareSIUnit{\molar}{M}
\pgfplotsset{compat=newest,width=10cm,height=8cm,xtick pos=left,ytick pos=left,scaled x ticks=real:1e-6,xtick scale label code/.code={},ylabel={$\Delta$ Absorbance}}
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
xlabel={$[\text{L\textsubscript{PYR}}]_0$ / \si{\micro\molar}},]
\addplot [only marks, mark=o] table [col sep=comma, x=Lo, y=A1] {./new micromath fit/ST041-1 24 lg 5 mM deriv.csv};
\addplot [no marks, smooth] table [col sep=comma, x=Lo, y=A1Calc] {./new micromath fit/ST041-1 24 lg 5 mM deriv.csv};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
xlabel={$[\text{L\textsubscript{PYR}}]_0$ / \si{\micro\molar}},]
\addplot [only marks, mark=o] table [col sep=comma, x=Lo, y=A1] {./new micromath fit/ST061 24 lg 100 mM 1to1.csv}; \label{raw}
\addplot [no marks, smooth] table [col sep=comma, x=Lo, y=A1Calc] {./new micromath fit/ST061 24 lg 100 mM 1to1.csv}; \label{fit}
\end{axis}
\end{tikzpicture}
\end{document}


EDIT: here are the data files for compiling the plots.num1 and num2

• Your MWE nothing tell us about your images generated by pgfplots package in standalone document class. Your main document can be a bit simplified, but in it is no cause of your problem. Using ˙pgfplots you can determine size of image (width, height). – Zarko Aug 23 '15 at 12:26
• Take a look at package tikzscale. – Heiko Oberdiek Aug 23 '15 at 12:33
• hi, i tried your suggestion in forcing the width and height in pgfplots, but it still looks the same – Spencer Trinh Aug 23 '15 at 13:57
• @SpencerTrinh, your images is to wide to be placed in one line. With simple function drawn with two coordinates (I haven't your data, however, they aren't essential) I receive equal height images. – Zarko Aug 23 '15 at 14:51
• Try [scale only axis=true] (page. 238). – John Kormylo Aug 23 '15 at 15:35

As mentioned @JohnKormylo, the exact same size of images you can obtain with group plot. Since this is inconvenient solution for you, there is not much other option as I mentioned in my comments. With declaring width and height of images, where you should care that they are narrower than text width you obtain almost the same their size. Another care should give to height and depth of xlabels and xticks as well that have the same position. Since the plots are similar (I suspect this, however I'm not sure if this is a case) this should not be big deal. If they differ, you can help yourself with use of vphantom{...} construct or with

\rule[depth]{width}{height}


where you select zero width and appropriate depth and height.

Another advice is to draw images in size in which they will appear in document. All scaling can introduce discrepancy in image size.

I do not know why you prefer to include images as pdf files. Package standalone allows to included their files directly. With this you escape borders as you use in your files. In such approach I generated figure below:

I do not see visible differences between size of this images. Code for above example is:

\documentclass[a4paper, twoside, 12pt]{report}
\usepackage{standalone}
\usepackage[a4paper,showframe,width=150mm,top=25mm,bottom=25mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{fixltx2e}
\usepackage{siunitx}
\usepackage{subfig}
\usepackage{xcolor}
\usepackage[version=4]{mhchem}

\DeclareSIUnit{\molar}{M}
width=7cm, height=6cm,
xtick pos=left, ytick pos=left,
%            scaled x ticks=real:1e-6,
xtick scale label code/.code={},
ylabel={$\Delta$ Absorbance}
}

\begin{document}
\pagestyle{empty}
\begin{figure}[h]
\centering
\subfloat[]{%
\input{test-image-1}
}%end of 1st subfloat
\hfil%
\subfloat[]{%
\input{test images-2
}%end of 2nd subfloat
\caption{}
\end{figure}
\end{document}


Test images were generated with following files:

\documentclass[12pt,
border=1pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{fixltx2e}
\usepackage{siunitx}
\usepackage{subfig}
\usepackage{xcolor}
\usepackage[version=4]{mhchem}

\DeclareSIUnit{\molar}{M}
width=7cm, height=6cm,
xtick pos=left, ytick pos=left,
%            scaled x ticks=real:1e-6,
xtick scale label code/.code={},
ylabel={$\Delta$ Absorbance}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$[\text{L}_{\text{PYR}}]_0 /\si{\micro\molar}$}
]
(5, 5)
(1, 1)
};
\end{axis}
\end{tikzpicture}
\end{document}


The file for the second image differ from the above only in

\addplot coordinates {
(5, 5)
(1, 1)
};


I do not test above example with including images as pdf files. It should give the same result. As you can observe I place images slightly differently (more simply) into figure as you do in your MWE.

• Hi Zarko, thanks for your help. I included two links (googledrive) to the actual data files to make my plots. If you have time you can try to reproduce the problem I am experiencing. In your plot, the y-axis are the same, so there wouldn't be the issue I am experiencing. I think because on one y-axis the numbers are i.e. 0.10 and on another axis, it is 0.010, so it is essentially wider, and thus when standalone crops it it makes a different size. I am not integrating standalone directly into my main document because the compile time would be too long; I have over 30 figures – Spencer Trinh Aug 23 '15 at 21:57
• @SpencerTrinh, in your question you emphasize diferent heighs of images ... Anyway, in my answer I explain what and how can be done for making images with approximately the same size. You should decide for compromise between image quality, size and necessary effort for obtain the desired result. And by the way, for today PC/laptop 30 Tikz pictures is not a lot – Zarko Aug 24 '15 at 7:42

I found that to have two figures exactly the same height, I needed to set the bounding box manually.

First I set the axis area to be 7 by 6 cm, and add the scale only axis=true.

\pgfplotsset{compat=newest,
width=7cm, height=6cm,
scale only axis=true,
[...]
}


Then I add an invisible path in the figure. In this case it adds a 1 cm border all round the axis, which should be enough to cover all labels and title so that TikZ will make this the new bounding box. You probably make it smaller.

\begin{tikzpicture}
\path (-1,-1) rectangle (8cm, 7cm);
[...]


Since I am using tikzplotlib for all my figures, adding this manually isn't really an option, so I add this after the fact.

sed -i -e '3i \\\path (-1.2,-1.1) rectangle (7.1cm, 3.6cm);' figure.pgf


From a longer writeup on how to use tikzplotlib: Publication ready figures.

You can try something like:

\begin{figure}[h]
\centering
\hspace*{\fill}%
\subfloat[]{%
\resizebox{0.45\linewidth}{!}{\includegraphics{./fittingcurve/ST039LIMD5.pdf}}}
\hfill%
\subfloat[]{%
\resizebox{0.45\linewidth}{!}{\includegraphics{./fittingcurve/ST018LIMD100.pdf}}}
\hspace*{\fill}%
\caption{}
\end{figure}
`
• Hi thanks for your suggestion, unfortunately, it did not work. – Spencer Trinh Aug 23 '15 at 18:08