Standalone and PGFplot cropping

1,109

If you want to remove the white space below the x-label you can use

xlabel style={yshift=-.3333em,inner ysep=0pt}

Note that the default inner sep is .3333em. So the distance to the tick labels will be the same.

enter image description here

Code:

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

 \usepackage{pgfplots} % loads tikz which loads xcolor and graphicx
 \pgfplotsset{compat=newest}

 \newcommand{\originalTextWidth}{455.24945pt}
 \newcommand{\originalTextHeight}{591.53027pt}

 % 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}

    \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},
        xlabel style={yshift=-.333em,inner ysep=0pt}
        ]
        \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}
                \draw [brown] (current bounding box.south west) rectangle (current bounding box.north east);%
        \end{tikzpicture}
        \caption{A plot in the Document} 
    \end{figure}
 \end{document}
Share:
1,109

Related videos on Youtube

josarn
Author by

josarn

"Success is dependent on effort." - Sophocles

Updated on June 03, 2020

Comments

  • josarn
    josarn over 3 years

    As asked here I set up a template for plots that can be used with standalone and included into the main document. 1 remaining problems is that the cropping does not crop enough of the whitespace beyond the x-label. which results in big spaces between x-label and caption. Setting \setlength{\abovecaptionskip}{0pt} in the main document helps reducing the space, but will result in other captions beeing not in the same distance as that of the plots.enter image description here

    \begin{filecontents*}{standalone_plot_1.tex}
    \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
    
    % This KOMA options are needed to calculate the Textwitdh and Height
    \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);%
            %\clip[use as bounding box] (0,0.5)++(current bounding box.south west) rectangle (current bounding box.north east);%
            %\clip[use as bounding box] (0,0)++(.01\textwidth,.01\textwidth) rectangle ++(.45\textwidth,.45\textwidth);
            \end{tikzpicture}% Never Remove this comment to avoid missplacing plot shifts to left side a little bit
        }%End \makebox
    \end{document}
    \end{filecontents*}
    
    \begin{filecontents*}{standalone_plot_2.tex}
        \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
    
        % This KOMA options are needed to calculate the Textwitdh and Height
        \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= text,%\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);%
                %\clip[use as bounding box] (0,0.5)++(current bounding box.south west) rectangle (current bounding box.north east);%
                %\clip[use as bounding box] (0,0)++(.01\textwidth,.01\textwidth) rectangle ++(.45\textwidth,.45\textwidth);
                \end{tikzpicture}% Never Remove this comment to avoid missplacing plot shifts to left side a little bit
            %}%End \makebox
        \end{document}
    \end{filecontents*}
    
    \documentclass[]{scrbook}
     \usepackage{showframe}% to show the page layout
     \usepackage[textwidth=455.24945pt]{geometry} % 455.24945pt=160.00000mm not cm because of accuracy
    
    
     \usepackage{pgfplots} % loads tikz which loads xcolor and graphicx
     \pgfplotsset{compat=newest}
    
     \newcommand{\originalTextWidth}{455.24945pt}
     \newcommand{\originalTextHeight}{591.53027pt}
    
     % 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.
     %\setlength{\abovecaptionskip}{0pt}
    
     \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 
    
    \setlength{\abovecaptionskip}{0pt} % just to show the difference
    
     \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}
                    \draw [brown] (current bounding box.south west) rectangle (current bounding box.north east);%
            \end{tikzpicture}
            \caption{A plot in the Document} 
        \end{figure}
    
        \begin{figure}[!ht]
            \centering
            \includestandalone{standalone_plot_2}
            \caption{Plot with no Box and label but quite good cropping of baseline}
        \end{figure}
        \begin{figure}[!ht]
            \centering
            \includestandalone{standalone_plot_1}
            \caption{With Box and x-label but too much whitspace}
        \end{figure}
    
    
     \end{document}