tikzdevice 生成的 latex 文件在图像和标题之间留有很大空白

tikzdevice 生成的 latex 文件在图像和标题之间留有很大空白

我一直在使用 R 中的 tikzdevice 包来生成我的图表的乳胶代码,我注意到它总是在标题和图形本身之间留下很大的空白,下面是我生成的一个示例:

首先,这是我将其包含在我的文件中的方式:

\documentclass{article}

\usepackage{tikz}

\usepackage[active,tightpage,psfixbb]{preview}

\begin{document}
\begin{figure}
\centering
\include{example}
\caption{apples}
\end{figure}
\end{document}

这里是 \include{example} (来自 tikzdevice 的输出)引用的文件:

\begin{tikzpicture}[x=1pt,y=1pt,scale=0.5]
\definecolor{fillColor}{RGB}{255,255,255}
\path[use as bounding box,fill=fillColor,fill opacity=0.00] (0,0) rectangle (505.89,505.89);
\begin{scope}
\path[clip] ( 49.20, 61.20) rectangle (480.69,456.69);
\definecolor{drawColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 65.18, 75.85) --
    (109.57, 86.94) --
    (153.97,105.44) --
    (198.36,131.33) --
    (242.75,164.62) --
    (287.14,205.31) --
    (331.53,253.40) --
    (375.92,308.88) --
    (420.32,371.76) --
    (464.71,442.04);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (505.89,505.89);
\definecolor{drawColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (109.57, 61.20) -- (464.71, 61.20);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (109.57, 61.20) -- (109.57, 55.20);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (198.36, 61.20) -- (198.36, 55.20);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (287.14, 61.20) -- (287.14, 55.20);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (375.92, 61.20) -- (375.92, 55.20);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (464.71, 61.20) -- (464.71, 55.20);

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (109.57, 39.60) {2};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (198.36, 39.60) {4};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (287.14, 39.60) {6};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (375.92, 39.60) {8};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (464.71, 39.60) {10};

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20, 72.15) -- ( 49.20,442.04);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20, 72.15) -- ( 43.20, 72.15);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,146.13) -- ( 43.20,146.13);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,220.11) -- ( 43.20,220.11);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,294.08) -- ( 43.20,294.08);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,368.06) -- ( 43.20,368.06);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,442.04) -- ( 43.20,442.04);

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 34.80, 72.15) {0};

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 34.80,146.13) {20};

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 34.80,220.11) {40};

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 34.80,294.08) {60};

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 34.80,368.06) {80};

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 34.80,442.04) {100};

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20, 61.20) --
    (480.69, 61.20) --
    (480.69,456.69) --
    ( 49.20,456.69) --
    ( 49.20, 61.20);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (505.89,505.89);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (264.94, 15.60) {x};
\end{scope}
\end{tikzpicture}

有人知道如何去除这些多余的空间吗?我试过使用 caption 包,但它似乎不适用于 tikzdevice 生成的文件

答案1

空白的主要来源是使用\include而不是\input。比较:

比较

如果你认为间距仍然太大,你可以编辑example.tex(tikzdevice 输出)。它以以下内容开头:

\begin{tikzpicture}[x=1pt,y=1pt,scale=0.5]
\definecolor{fillColor}{RGB}{255,255,255}
\path[use as bounding box,fill=fillColor,fill opacity=0.00] (0,0) rectangle (505.89,505.89);

这个初始矩形定义了整个图形的边界框,即为其保留的大小。如果您“作弊”并使用较小的边界框,则标题将更接近。例如,要将标题升高 6ex,您必须定义一个底部短 6ex 的边界框,这很容易:

\path[use as bounding box,fill=fillColor,fill opacity=0.00] (0,6ex) rectangle (505.89,505.89);

本例的结果:

结果

相关内容