3

I try to set Up a document with multiple Plots for that i try to use TIKZ and Standalone. But the Problem is with the alignment of the different plots. I use scale only axis to make the axis all the same size to avoid aligment problems because of Y-Axis labels and bigger Numbers. Then i use trim axis left, trim axis right to avoid Problems with outstanding numbers. That would work when i directly include the tikzpicture to the document but when including them with standalone the Boundingbox seems to be calculatet differently, so centering the plots does not result in a proper alignment. Which is the best way to achieved proper alignment. Bad Font is a product of converting the pdf with inkscape to an png and drawing in the red lines. enter image description here

main file

 \documentclass[]{scrbook}
 \usepackage[textwidth=455.24945pt]{geometry} % 455.24945pt=160.00000mm not cm because of accuracy

 % Package for nice Graphs
 \usepackage[pdftex]{graphicx, xcolor}

 \usepackage[]{tikz}
 \usepackage{pgfplots}
 \usetikzlibrary{arrows, positioning, calc, matrix}
 \pgfplotsset{compat=newest}

 \usepackage{standalone}
 \standaloneconfig{mode=tex}
 % tex = use sourcefile, default
 % image = use existing image file produced by the source file
 % build = build image from source, then use it
 % buildmissing= only build image if it does not exist
 % buildnew = only build image if source file is newer 

 \begin{document}

    \begin{figure}[!ht]
        \centering
        \begin{tikzpicture}
        \begin{axis}[
        trim axis left, trim axis right, 
        scale only axis, % the width specifies the width of the axis only not the labels etc.
        width=0.75\textwidth, 
        height=0.1\textheight,
        xlabel={Some Data},
        ylabel={Some Value},
        ]
        \addplot[very thick, red, mark=x] coordinates {(0,0)
            (1,2)(2,3)
            (3,4)(4,6)
            (5,11)(6,20)
            (7,15)(8,12)
            (9,8)(10,7)
            (11,3)(12,0)};
        \end{axis}
        \end{tikzpicture}
        \caption{A plot in the Document} 
    \end{figure}

    \begin{figure}[!ht]
        \centering
        \includestandalone{standalone_plot}
        \caption{Almost same plot but as Standalone}
    \end{figure}
    \begin{figure}[!ht]
        \centering
        \includestandalone{standalone_plot_2}
        \caption{Y-Values are getting bigger}
    \end{figure}        
    \begin{figure}[!ht]
        \centering
        \includestandalone{standalone_plot_3}
        \caption{Bigger Y-values and no Y Label}
    \end{figure}

 \end{document}

first standalone file

\documentclass[ class=scrbook, 
                convert=false,
                border=60pt,  
                ]{standalone}
% if axis option " only scale axis" is used border has to be set manually to avoid cropping of the Y-Axis Label. Including the standalone then has to be set to \standaloneconfig{mode=tex}
% Using border={0pt 0pt 0pt 0pt} seems not to work with scrbook

\usepackage[textwidth=455.24945pt]{geometry} % 455.24945pt=160.00000mm not cm because of accuracy

\usepackage{tikz}
\usepackage{pgfplots} 

\begin{document}    

\begin{tikzpicture}
\begin{axis}[ 
    trim axis left, trim axis right, 
    scale only axis, % the width specifies the width of the axis only not the labels etc.
    width=0.75\textwidth, 
    height=0.1\textheight,
    scale only axis, 
    xmin=0,xmax=12, 
    xlabel=fractions,
    ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,20)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\end{tikzpicture}

\end{document}

Second standalone file

\documentclass[ class=scrbook, 
                convert=false,
                border=60pt,  
                ]{standalone}
% if axis option " only scale axis" is used border has to be set manually to avoid cropping of the Y-Axis Label. Including the standalone then has to be set to \standaloneconfig{mode=tex}
% Using border={0pt 0pt 0pt 0pt} seems not to work with scrbook

\usepackage[textwidth=455.24945pt]{geometry} % 455.24945pt=160.00000mm not cm because of accuracy

\usepackage{tikz}
\usepackage{pgfplots} 

\begin{document}    

\begin{tikzpicture}
\begin{axis}[ 
    trim axis left, trim axis right, 
    scale only axis, % the width specifies the width of the axis only not the labels etc.
    width=0.75\textwidth, 
    height=0.1\textheight,
    scale only axis, 
    xmin=0,xmax=12, 
    xlabel=fractions,
    ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,200)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\end{tikzpicture}

\end{document}

Third standalone file

\documentclass[ class=scrbook, 
                convert=false,
                border=60pt,  
                ]{standalone}
% if axis option " only scale axis" is used border has to be set manually to avoid cropping of the Y-Axis Label. Including the standalone then has to be set to \standaloneconfig{mode=tex}
% Using border={0pt 0pt 0pt 0pt} seems not to work with scrbook

\usepackage[textwidth=455.24945pt]{geometry} % 455.24945pt=160.00000mm not cm because of accuracy

\usepackage{tikz}
\usepackage{pgfplots} 

\begin{document}    

\begin{tikzpicture}
\begin{axis}[ 
    trim axis left, trim axis right, 
    scale only axis, % the width specifies the width of the axis only not the labels etc.
    width=0.75\textwidth, 
    height=0.1\textheight,
    scale only axis, 
    xmin=0,xmax=12, 
    xlabel=fractions,
    %ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,200)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\end{tikzpicture}

\end{document}
2
  • Paease, provide MWE (minimal working example) for your pictures, which shoes your problem. Differences are caused with of different with of images, e.g. position x ticks labels.
    – Zarko
    Jun 1, 2016 at 9:53
  • Did you try putting the includestandalone statements in a fbox{...}? I guess your bounding boxes are different due to the different labels. Due to this, the position of the axes inside the bounding box is different, hence alignment will fail. I suggest putting all plots that belong to one figure in one tikz file, using pgfplots' capability to handle multiple plots
    – Dux
    Jun 1, 2016 at 12:37

3 Answers 3

4

Update2 (with tikz library external and without standalone)

You can also use the tikz library external. Then you have to set trim axis left,trim axis right as an option for the tikzpicture.

\begin{filecontents*}{external_plot_1.tex}
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[ 
    scale only axis, 
    width=.75\textwidth, 
    height=.1\textheight,
    xmin=0,xmax=12, 
    xlabel=fractions,
    ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,20)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\end{tikzpicture}
\end{filecontents*}

\begin{filecontents*}{external_plot_2.tex}
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[ 
    scale only axis,
    width=.75\textwidth, 
    height=.1\textheight,
    xmin=0,xmax=12, 
    xlabel=fractions,
    ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,200)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\end{tikzpicture}
\end{filecontents*}

\begin{filecontents*}{external_plot_3.tex}
\begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[ 
    scale only axis, 
    width=.75\textwidth, 
    height=.1\textheight,
    xmin=0,xmax=12, 
    xlabel=fractions,
    %ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,200)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\end{tikzpicture}
\end{filecontents*}


\documentclass[]{scrbook}
\usepackage[textwidth=455.24945pt]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{arrows, positioning, calc, matrix}
\usetikzlibrary{external}
\tikzexternalize[prefix=figures/]

\begin{document}
\tikzexternaldisable% disable
\begin{figure}[!ht]
    \centering
    \begin{tikzpicture}[trim axis left,trim axis right]
    \begin{axis}[
    scale only axis,
    width=.75\textwidth, 
    height=.1\textheight,
    xlabel={Some Data},
    ylabel={Some Value},
    ]
    \addplot[very thick, red, mark=x] coordinates {(0,0)
        (1,2)(2,3)
        (3,4)(4,6)
        (5,11)(6,20)
        (7,15)(8,12)
        (9,8)(10,7)
        (11,3)(12,0)};
    \end{axis}
    \end{tikzpicture}
    \caption{A plot in the Document} 
\end{figure}
\tikzexternalenable% \enable

\begin{figure}[!ht]
    \centering
    \input{external_plot_1}
    \caption{Almost same plot but as Standalone}
\end{figure}
\begin{figure}[!ht]
    \centering
    \input{external_plot_2}
    \caption{Y-Values are getting bigger}
\end{figure}        
\begin{figure}[!ht]
    \centering
    \input{external_plot_3}
    \caption{Bigger Y-values and no Y Label}
\end{figure}
\end{document}

Result:

enter image description here

One of the externalized plots:

enter image description here


Update (works for mode=tex and mode=build)

Here is another suggestion that calculates in the main document the desired width and height of the plots and write them in a file plotdim.txt. So package geometry can be removed from the standalone files.

Instead of trim axis left and trim axis right you can also use

\pgfresetboundingbox
\path
  ([xshift=-50pt]current axis.below south west)
  rectangle
  ([xshift=50pt]current axis.above north east)
;

enter image description here

Code:

\begin{filecontents*}{standalone_plot_1.tex}
\documentclass[convert=false,class=scrbook]{standalone}
\input{plotdim.txt}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ 
    scale only axis, 
    width=\myplotwidth, 
    height=\myplotheight,
    xmin=0,xmax=12, 
    xlabel=fractions,
    ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,20)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\pgfresetboundingbox
\path
  ([xshift=-50pt]current axis.below south west)
  rectangle
  ([xshift=50pt]current axis.above north east)
;
\end{tikzpicture}%
\end{document}
\end{filecontents*}

\begin{filecontents*}{standalone_plot_2.tex}
\documentclass[convert=false,class=scrbook]{standalone}
\input{plotdim.txt}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ 
    scale only axis,
    width=\myplotwidth, 
    height=\myplotheight,
    xmin=0,xmax=12, 
    xlabel=fractions,
    ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,200)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\pgfresetboundingbox
\path
  ([xshift=-50pt]current axis.below south west)
  rectangle
  ([xshift=50pt]current axis.above north east)
;
\end{tikzpicture}%
\end{document}
\end{filecontents*}

\begin{filecontents*}{standalone_plot_3.tex}
\documentclass[convert=false,class=scrbook]{standalone}
\input{plotdim.txt}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ 
    scale only axis, 
    width=\myplotwidth, 
    height=\myplotheight,
    xmin=0,xmax=12, 
    xlabel=fractions,
    %ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,200)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\pgfresetboundingbox
\path
  ([xshift=-50pt]current axis.below south west)
  rectangle
  ([xshift=50pt]current axis.above north east)
;
\end{tikzpicture}%
\end{document}
\end{filecontents*}


\documentclass[]{scrbook}
\usepackage{showframe}% to show the page layout
\usepackage[textwidth=455.24945pt]{geometry}
\newlength\myplotwidth
\newlength\myplotheight
\AtBeginDocument{%
  \setlength\myplotwidth{\dimexpr\textwidth*3/4\relax}
  \setlength\myplotheight{\dimexpr\textheight/10\relax}
  \newwrite\textdim
  \immediate\openout\textdim=plotdim.txt
  \immediate\write\textdim{\string\newcommand\string\myplotwidth\string{\the\myplotwidth\string}}%
  \immediate\write\textdim{\string\newcommand\string\myplotheight\string{\the\myplotheight\string}}%
  \immediate\closeout\textdim
}

\usepackage{pgfplots}
\usetikzlibrary{arrows, positioning, calc, matrix}
\pgfplotsset{compat=newest}

\usepackage{standalone}
\standaloneconfig{mode=build}
% tex = use sourcefile, default
% image = use existing image file produced by the source file
% build = build image from source, then use it
% buildmissing= only build image if it does not exist
% buildnew = only build image if source file is newer 
 \begin{document}
\begin{figure}[!ht]
    \centering
    \begin{tikzpicture}[trim axis left,trim axis right]
    \begin{axis}[
    scale only axis,
    width=\myplotwidth, 
    height=\myplotheight,
    xlabel={Some Data},
    ylabel={Some Value},
    ]
    \addplot[very thick, red, mark=x] coordinates {(0,0)
        (1,2)(2,3)
        (3,4)(4,6)
        (5,11)(6,20)
        (7,15)(8,12)
        (9,8)(10,7)
        (11,3)(12,0)};
    \end{axis}
    \end{tikzpicture}
    \caption{A plot in the Document} 
\end{figure}

\begin{figure}[!ht]
    \centering
    \includestandalone{standalone_plot_1}
    \caption{Almost same plot but as Standalone}
\end{figure}
\begin{figure}[!ht]
    \centering
    \includestandalone{standalone_plot_2}
    \caption{Y-Values are getting bigger}
\end{figure}        
\begin{figure}[!ht]
    \centering
    \includestandalone{standalone_plot_3}
    \caption{Bigger Y-values and no Y Label}
\end{figure}
\end{document}

Original answer (works only for mode=tex)

trim axis left and trim axis right must be provided as option of the tikzpicture environment and not the axis environment.

To ensure that the tick and axis labels are not clipped away I would use \makebox.

enter image description here

\begin{filecontents*}{standalone_plot.tex}
\documentclass[ class=scrbook, 
                convert=false,
                margin=0pt
                ]{standalone}
\usepackage[textwidth=455.24945pt]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}    
\makebox[\dimexpr.75\textwidth+100pt\relax]{%
    \begin{tikzpicture}[trim axis left,trim axis right]
    \begin{axis}[ 
        scale only axis, 
        width=0.75\textwidth, 
        height=0.1\textheight,
        xmin=0,xmax=12, 
        xlabel=fractions,
        ylabel=Small Axis,
     ]
    \addplot[blue, mark=x] coordinates {(0,0)
        (1,2)(2,3)
        (3,4)(4,6)
        (5,11)(6,20)
        (7,15)(8,12)
        (9,8)(10,7)
        (11,3)(12,0)};
    \end{axis}
    \end{tikzpicture}%
}
\end{document}
\end{filecontents*}

\begin{filecontents*}{standalone_plot_2.tex}
\documentclass[ class=scrbook, 
                convert=false,
                margin=0pt
                ]{standalone}
\usepackage[textwidth=455.24945pt]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}    
\makebox[\dimexpr.75\textwidth+100pt\relax]{%
    \begin{tikzpicture}[trim axis left,trim axis right]
    \begin{axis}[ 
        scale only axis,
        width=0.75\textwidth, 
        height=0.1\textheight,
        xmin=0,xmax=12, 
        xlabel=fractions,
        ylabel=Small Axis,
     ]
    \addplot[blue, mark=x] coordinates {(0,0)
        (1,2)(2,3)
        (3,4)(4,6)
        (5,11)(6,200)
        (7,15)(8,12)
        (9,8)(10,7)
        (11,3)(12,0)};
    \end{axis}
    \end{tikzpicture}%
}
\end{document}
\end{filecontents*}

\begin{filecontents*}{standalone_plot_3.tex}
\documentclass[ class=scrbook, 
                convert=false,
                margin=0pt
                ]{standalone}
\usepackage[textwidth=455.24945pt]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}    
\makebox[\dimexpr.75\textwidth+100pt\relax]{%
    \begin{tikzpicture}[trim axis left,trim axis right]
    \begin{axis}[ 
        scale only axis, 
        width=0.75\textwidth, 
        height=0.1\textheight,
        xmin=0,xmax=12, 
        xlabel=fractions,
        %ylabel=Small Axis,
     ]
    \addplot[blue, mark=x] coordinates {(0,0)
        (1,2)(2,3)
        (3,4)(4,6)
        (5,11)(6,200)
        (7,15)(8,12)
        (9,8)(10,7)
        (11,3)(12,0)};
    \end{axis}
    \end{tikzpicture}%
}
\end{document}
\end{filecontents*}


\documentclass[]{scrbook}
 \usepackage{showframe}% to show the page layout
 \usepackage[textwidth=455.24945pt]{geometry}
 \usepackage{pgfplots}
 \usetikzlibrary{arrows, positioning, calc, matrix}
 \pgfplotsset{compat=newest}

 \usepackage{standalone}
 \standaloneconfig{mode=tex}
 % tex = use sourcefile, default
 % image = use existing image file produced by the source file
 % build = build image from source, then use it
 % buildmissing= only build image if it does not exist
 % buildnew = only build image if source file is newer 
 \begin{document}

    \begin{figure}[!ht]
        \centering
        \begin{tikzpicture}[trim axis left,trim axis right]
        \begin{axis}[
        scale only axis,
        width=0.75\textwidth, 
        height=0.1\textheight,
        xlabel={Some Data},
        ylabel={Some Value},
        ]
        \addplot[very thick, red, mark=x] coordinates {(0,0)
            (1,2)(2,3)
            (3,4)(4,6)
            (5,11)(6,20)
            (7,15)(8,12)
            (9,8)(10,7)
            (11,3)(12,0)};
        \end{axis}
        \end{tikzpicture}
        \caption{A plot in the Document} 
    \end{figure}

    \begin{figure}[!ht]
        \centering
        \includestandalone{standalone_plot}
        \caption{Almost same plot but as Standalone}
    \end{figure}
    \begin{figure}[!ht]
        \centering
        \includestandalone{standalone_plot_2}
        \caption{Y-Values are getting bigger}
    \end{figure}        
    \begin{figure}[!ht]
        \centering
        \includestandalone{standalone_plot_3}
        \caption{Bigger Y-values and no Y Label}
    \end{figure}

 \end{document}

Note that pgfplots loads tikz which loads xcolor and graphicx.

3
  • Very nice. Works well used with \standaloneconfig{mode=tex} changing that to buildnew , to use to real function of Standalone separating the plots in different pdfs and reduce compile time, gives a bad result. The x-Axis values are missing. The seperated pdf plots have a big white space at the right side which than results in a left shift of the plots to the in text plot. Maybe you know why.
    – josarn
    Jun 1, 2016 at 15:10
  • See my updated answer. The problem seems to be the geometry package in the standalone files. Its usage in a standalone file does not make sense (as @JohnKormylo has already said).
    – esdd
    Jun 1, 2016 at 18:25
  • Thanks a lot, i reduced the standalone template to the neccessary. Now set the textwidth manually, use a box to avoid cropping problems. See my next Answer. There are now to things remaining which i will post as new questions. 1. Setting Tex or Build still results in different layout, the y-labels are nearer or farer. 2. cropping let a big whitspace after the x-label, which than results in to big whitspace from x-label to caption.
    – josarn
    Jun 2, 2016 at 12:42
2

Wouldn't it be easier just to find a good size for your plots (possibly using \rule) in terms of absolute coordinates, or use \the\textwidth to record the width in points, than try to combine standalone and geometry in any sort of meaningful way?

Anyway, a simple solution to your alignment problem is to place a standard box around each pgfplot inside the tikzpicture. The origin is located at the lower left corner of the axis box. Use \draw to verify the fit and \path to reserve the space.

For example:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots} 

\begin{document}    

\begin{tikzpicture}
\begin{axis}[ 
    scale only axis, % the width specifies the width of the axis only not the labels etc.
    width=8cm, 
    height=2cm,
    xmin=0,xmax=12, 
    xlabel=fractions,
    ylabel=Small Axis,
 ]
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,20)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\path (-1.5,-1) rectangle (8.5,2.5);% standard box
\end{tikzpicture}

\end{document}
0

I removed all uneccessary things as you mentioned, added the with as fixed with and did some other little changes, that seemed to help. Here is my solution now. See the comment above that refers to the remaining Problems.

\documentclass[ class=scrbook, 
                convert=false,
                border=0pt,% no borders
                ]{standalone}
% if axis option " only scale axis" is used border has to be set manually to avoid cropping of the Y-Axis Label. Including the standalone then has to be set to \standaloneconfig{mode=tex}
% Using border={0pt 0pt 0pt 0pt} seems not to work with scrbook

% set KOMA options
\KOMAoptions{fontsize=12pt} % Fontsize

% the textwidth of the main docement is 16cm=455.24945pt
\newcommand{\originalTextWidth}{455.24945pt}
\newcommand{\originalTextHeight}{591.53027pt}

\usepackage{pgfplots} 

% Option that should not be change by the user, this are set here so it is obvious and makes the code more readable
\pgfplotsset{
    every axis/.append style={
        width=0.75*\originalTextWidth, 
        height=0.1*\originalTextHeight,
        scale only axis, % the width and height specifies only axis not the labels etc. to avoid missplacing of the different plots when Labels are used or not.
    },
}
\tikzset{every picture/.style={trim axis left, trim axis right}}% used to avoid missplacing of different plots when the numbers on the x-axis stands out left or right.

\begin{document}%   
\makebox[\dimexpr\originalTextWidth\relax]{% Box to avoid problem of standalone cropping too much of the plot.  
\begin{tikzpicture}
\begin{axis}[
    xmin=0,xmax=12,%
        xlabel= \fbox{fractions q y Pp f g},
    %ylabel=Small Axis,
 ]%
\addplot[blue, mark=x] coordinates {(0,0)
    (1,2)(2,3)
    (3,4)(4,6)
    (5,11)(6,200)
    (7,15)(8,12)
    (9,8)(10,7)
    (11,3)(12,0)};
\end{axis}
\draw [brown] (current bounding box.south west) rectangle (current bounding box.north east);%
\end{tikzpicture}% Never Remove this comment to avoid missplacing plot shifts to left side a little bit
}%End \makebox
\end{document}

You must log in to answer this question.

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