独立和 PGFplot 裁剪

独立和 PGFplot 裁剪

根据要求这里我设置了一个绘图模板,可以单独使用并包含在主文档中。剩下的 1 个问题是裁剪没有裁剪掉 x 标签之外的足够多的空白。这导致 x 标签和标题之间有很大的空白。\setlength{\abovecaptionskip}{0pt}在主文档中设置有助于减少空间,但会导致其他标题与绘图的距离不同。在此处输入图片描述

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

答案1

如果你想删除 x 标签下方的空白,你可以使用

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

请注意,默认值为inner sep.3333em因此到刻度标签的距离将相同。

在此处输入图片描述

代码:

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

相关内容