TikZ:裁剪边界框

TikZ:裁剪边界框

我使用的绘图程序支持 TikZ 输出。它会生成一个\begin{tikzpicture} ... \end{tikzpicture}可以包含在文档中的图像。但问题是它似乎在实际绘图周围插入了大量空白。

目前,我的方法是将图包含在“空” LaTeX 文件中,pdflatex在其上运行,然后释放pdfcrop。然后可以将生成的 PDF 包含在 TikZ 图像的位置。

但是,这根本不够干净,而且我自己也做了不少工作。因此,我想知道我是否可以指示 TikZ 帮我进行裁剪。

更新 这是一份显示该问题的最小文档;抱歉篇幅太长,机器生成的 TikZ(不幸的是)从来都不是那么简洁。我在最后添加了一个块来显示图形的边界框。

\documentclass[11pt]{文章}
\pagestyle{空}
\usepackage{tikz}
\开始{文档}
\begin{tikzpicture}{0pt}{0pt}{518pt}{403pt}
    \clip(0pt,403pt) -- (389.957pt,403pt) -- (389.957pt,99.6166pt) -- (0pt,99.6166pt) -- (0pt,403pt);
    \颜色[rgb]{1,1,1}
    \填充(3.76406pt,399.236pt)--(380.923pt,399.236pt)--(380.923pt,253.19pt)--(3.76406pt,253.19pt)--(3.76406pt,399.236pt);
    \填充(53.4497pt,394.719pt)--(374.901pt,394.719pt)--(374.901pt,289.325pt)--(53.4497pt,289.325pt)--(53.4497pt,394.719pt);
\开始{范围}
    \颜色[rgb]{0,0,0}
    \pgftext[中心,基点,位于={\pgfpoint{13.5506pt}{343.151pt}},rotate=90]{\sffamily\fontsize{9}{0}\selectfont{$\mathsf{\theta}$ / rad}}
    \颜色[rgb]{0.0784314,0.0745098,0.0705882}
    \pgftext[中心,基点,位于={\pgfpoint{30.1125pt}{285.561pt}}]{\sffamily\fontsize{9}{0}\selectfont{-0.10}}
    \pgftext[中心,基点,位于={\pgfpoint{30.1125pt}{311.91pt}}]{\sffamily\fontsize{9}{0}\selectfont{-0.05}}
    \pgftext[中心,基点,位于={\pgfpoint{31.9945pt}{338.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{0.00}}
    \pgftext[中心,基点,位于={\pgfpoint{31.9945pt}{365.359pt}}]{\sffamily\fontsize{9}{0}\selectfont{0.05}}
    \pgftext[中心,基点,位于={\pgfpoint{31.9945pt}{391.708pt}}]{\sffamily\fontsize{9}{0}\selectfont{0.10}}
    \颜色[rgb]{0,0,0}
    \draw[线连接=斜面,线帽=矩形](52.6969pt,294.595pt) -- (48.9328pt,294.595pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,299.865pt) -- (48.9328pt,299.865pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,305.134pt) -- (48.9328pt,305.134pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,310.404pt) -- (48.9328pt,310.404pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,320.943pt) -- (48.9328pt,320.943pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,326.213pt) -- (48.9328pt,326.213pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,331.483pt) -- (48.9328pt,331.483pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,336.752pt) -- (48.9328pt,336.752pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,348.045pt) -- (48.9328pt,348.045pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,353.314pt) -- (48.9328pt,353.314pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,358.584pt) -- (48.9328pt,358.584pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,363.854pt) -- (48.9328pt,363.854pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,374.393pt) -- (48.9328pt,374.393pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,379.663pt) -- (48.9328pt,379.663pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,384.933pt) -- (48.9328pt,384.933pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,390.202pt) -- (48.9328pt,390.202pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,289.325pt) -- (45.9216pt,289.325pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,315.674pt) -- (45.9216pt,315.674pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,342.022pt) -- (45.9216pt,342.022pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,369.123pt) -- (45.9216pt,369.123pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,395.472pt) -- (45.9216pt,395.472pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,395.472pt) -- (52.6969pt,289.325pt);
    \pgftext[中心,基点,位于={\pgfpoint{215.304pt}{256.202pt}}]{\sffamily\fontsize{9}{0}\selectfont{\textit{t}}}
    \颜色[rgb]{0.0784314,0.0745098,0.0705882}
    \pgftext[中心,基点,位于={\pgfpoint{52.6969pt}{271.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{0}}
    \pgftext[中心,基点,位于={\pgfpoint{93.3487pt}{271.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{5}}
    \pgftext[中心,基点,位于={\pgfpoint{133.248pt}{271.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{10}}
    \pgftext[中心,基点,位于={\pgfpoint{173.9pt}{271.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{15}}
    \pgftext[中心,基点,位于={\pgfpoint{213.799pt}{271.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{20}}
    \pgftext[中心,基点,位于={\pgfpoint{254.451pt}{271.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{25}}
    \pgftext[中心,基点,位于={\pgfpoint{294.35pt}{271.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{30}}
    \pgftext[中心,基点,位于={\pgfpoint{335.002pt}{271.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{35}}
    \pgftext[中心,基点,位于={\pgfpoint{374.901pt}{271.258pt}}]{\sffamily\fontsize{9}{0}\selectfont{40}}
    \颜色[rgb]{0,0,0}
    \draw[线连接=斜面,线帽=矩形](60.9778pt,289.325pt) -- (60.9778pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](68.5059pt,289.325pt) -- (68.5059pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](76.7869pt,289.325pt) -- (76.7869pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](85.0678pt,289.325pt) -- (85.0678pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](100.877pt,289.325pt) -- (100.877pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](109.158pt,289.325pt) -- (109.158pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](117.439pt,289.325pt) -- (117.439pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](124.967pt,289.325pt) -- (124.967pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](141.529pt,289.325pt) -- (141.529pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](149.057pt,289.325pt) -- (149.057pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](157.338pt,289.325pt) -- (157.338pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](165.619pt,289.325pt) -- (165.619pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](181.428pt,289.325pt) -- (181.428pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](189.709pt,289.325pt) -- (189.709pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](197.99pt,289.325pt) -- (197.99pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](205.518pt,289.325pt) -- (205.518pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](222.08pt,289.325pt) -- (222.08pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](229.608pt,289.325pt) -- (229.608pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](237.889pt,289.325pt) -- (237.889pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](246.17pt,289.325pt) -- (246.17pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](261.979pt,289.325pt) -- (261.979pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](270.26pt,289.325pt) -- (270.26pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](278.541pt,289.325pt) -- (278.541pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](286.069pt,289.325pt) -- (286.069pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](302.631pt,289.325pt) -- (302.631pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](310.159pt,289.325pt) -- (310.159pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](318.44pt,289.325pt) -- (318.44pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](326.721pt,289.325pt) -- (326.721pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](342.53pt,289.325pt) -- (342.53pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](350.811pt,289.325pt) -- (350.811pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](359.092pt,289.325pt) -- (359.092pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](366.62pt,289.325pt) -- (366.62pt,285.561pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,289.325pt) -- (52.6969pt,282.55pt);
    \draw[线连接=斜面,线帽=矩形](93.3487pt,289.325pt) -- (93.3487pt,282.55pt);
    \draw[线连接=斜面,线帽=矩形](133.248pt,289.325pt) -- (133.248pt,282.55pt);
    \draw[线连接=斜面,线帽=矩形](173.9pt,289.325pt) -- (173.9pt,282.55pt);
    \draw[线连接=斜面,线帽=矩形](213.799pt,289.325pt) -- (213.799pt,282.55pt);
    \draw[线连接=斜面,线帽=矩形](254.451pt,289.325pt) -- (254.451pt,282.55pt);
    \draw[线连接=斜面,线帽=矩形](294.35pt,289.325pt) -- (294.35pt,282.55pt);
    \draw[线连接=斜面,线帽=矩形](335.002pt,289.325pt) -- (335.002pt,282.55pt);
    \draw[线连接=斜面,线帽=矩形](374.901pt,289.325pt) -- (374.901pt,282.55pt);
    \draw[线连接=斜面,线帽=矩形](52.6969pt,289.325pt) -- (374.901pt,289.325pt);
    \颜色[rgb]{1,1,0}
    \fill[opacity=0.392157](271.012pt,348.045pt) -- (362.103pt,348.045pt) -- (362.103pt,337.505pt) -- (271.012pt,337.505pt) -- (271.012pt,348.045pt);
    \颜色[rgb]{0,0,0}
    \draw[线连接=斜接,线帽=rect](271.012pt,348.045pt) -- (362.103pt,348.045pt) -- (362.103pt,337.505pt) -- (271.012pt,337.505pt) -- (271.012pt,348.045pt);
    %\color[rgb]{1,1,1}
    %\fill[opacity=0.607843](278.541pt,396.225pt) -- (361.35pt,396.225pt) -- (361.35pt,351.809pt) -- (278.541pt,351.809pt) -- (278.541pt,396.225pt);
    \颜色[rgb]{1,0,0}
    \draw[线宽=1pt, 线连接=斜接, 线帽=矩形](287.574pt,385.685pt) -- (302.631pt,385.685pt);
    \颜色[rgb]{0,0,0}
    \pgftext[左,基,位于={\pgfpoint{310.159pt}{381.921pt}}]{\sffamily\fontsize{9}{0}\selectfont{欧拉}}
    \颜色[rgb]{0,0.501961,0}
    \draw[线宽=1pt, 线连接=斜接, 线帽=矩形](287.574pt,374.393pt) -- (302.631pt,374.393pt);
    \颜色[rgb]{0,0,0}
    \pgftext[左,基,位于={\pgfpoint{310.159pt}{370.629pt}}]{\sffamily\fontsize{9}{0}\selectfont{Leapfrog}}
    \draw[线宽=1pt, 线连接=斜接, 线帽=矩形](287.574pt,363.101pt) -- (302.631pt,363.101pt);
    \pgftext[左,基,位于={\pgfpoint{310.159pt}{359.337pt}}]{\sffamily\fontsize{9}{0}\selectfont{分析}}
    \颜色[rgb]{1,1,1}
    \填充(3.76406pt,249.426pt)--(386.193pt,249.426pt)--(386.193pt,103.381pt)--(3.76406pt,103.381pt)--(3.76406pt,249.426pt);
    \颜色[rgb]{1,1,0}
    \fill[opacity=0.392157](57.2137pt,246.415pt) -- (386.193pt,246.415pt) -- (386.193pt,133.493pt) -- (57.2137pt,133.493pt) -- (57.2137pt,246.415pt);
\结束{范围}
\开始{范围}
    \颜色[rgb]{0,0,0}
    \draw[线连接=斜接,线帽=rect](56.4609pt,247.168pt) -- (385.44pt,247.168pt) -- (385.44pt,134.246pt) -- (56.4609pt,134.246pt) -- (56.4609pt,247.168pt);
    \颜色[rgb]{0,0,0}
    \pgftext[中心,基点,位于={\pgfpoint{13.5506pt}{191.46pt}},旋转=90]{\sffamily\fontsize{9}{0}\selectfont{\textbf{ }}}
    \颜色[rgb]{0.0784314,0.0745098,0.0705882}
    \pgftext[中心,基点,位于={\pgfpoint{30.8653pt}{131.988pt}}]{\sffamily\fontsize{9}{0}\selectfont{-0.010}}
    \pgftext[中心,基点,位于={\pgfpoint{30.8653pt}{159.842pt}}]{\sffamily\fontsize{9}{0}\selectfont{-0.005}}
    \pgftext[中心,基点,位于={\pgfpoint{32.7473pt}{186.943pt}}]{\sffamily\fontsize{9}{0}\selectfont{0.000}}
    \pgftext[中心,基点,位于={\pgfpoint{32.7473pt}{214.044pt}}]{\sffamily\fontsize{9}{0}\selectfont{0.005}}
    \pgftext[中心,基点,位于={\pgfpoint{32.7473pt}{241.898pt}}]{\sffamily\fontsize{9}{0}\selectfont{0.010}}
    \颜色[rgb]{0,0,0}
    \draw[线连接=斜面,线帽=矩形](56.4609pt,141.774pt) -- (52.6969pt,141.774pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,147.044pt) -- (52.6969pt,147.044pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,152.313pt) -- (52.6969pt,152.313pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,157.583pt) -- (52.6969pt,157.583pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,168.875pt) -- (52.6969pt,168.875pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,174.145pt) -- (52.6969pt,174.145pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,179.415pt) -- (52.6969pt,179.415pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,185.437pt) -- (52.6969pt,185.437pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,195.977pt) -- (52.6969pt,195.977pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,201.999pt) -- (52.6969pt,201.999pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,207.269pt) -- (52.6969pt,207.269pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,212.538pt) -- (52.6969pt,212.538pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,223.831pt) -- (52.6969pt,223.831pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,229.1pt) -- (52.6969pt,229.1pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,234.37pt) -- (52.6969pt,234.37pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,239.64pt) -- (52.6969pt,239.64pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,135.752pt) -- (49.6856pt,135.752pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,163.606pt) -- (49.6856pt,163.606pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,190.707pt) -- (49.6856pt,190.707pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,217.808pt) -- (49.6856pt,217.808pt);
    \draw[线连接=斜面,线帽=矩形](56.4609pt,245.662pt) -- (49.6856pt,245.662pt);
    \pgftext[中心,基点,位于={\pgfpoint{220.95pt}{106.392pt}}]{\sffamily\fontsize{9}{0}\selectfont{\textbf{ }}}
    \颜色[rgb]{0.0784314,0.0745098,0.0705882}
    \pgftext[中心,基点,位于={\pgfpoint{83.5622pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{28}}
    \pgftext[中心,基点,位于={\pgfpoint{112.169pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{29}}
    \pgftext[中心,基点,位于={\pgfpoint{140.776pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{30}}
    \pgftext[中心,基点,位于={\pgfpoint{169.383pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{31}}
    \pgftext[中心,基点,位于={\pgfpoint{197.99pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{32}}
    \pgftext[中心,基点,位于={\pgfpoint{226.597pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{33}}
    \pgftext[中心,基点,位于={\pgfpoint{255.58pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{34}}
    \pgftext[中心,基点,位于={\pgfpoint{283.81pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{35}}
    \pgftext[中心,基点,位于={\pgfpoint{313.17pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{36}}
    \pgftext[中心,基点,位于={\pgfpoint{341.777pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{37}}
    \pgftext[中心,基点,位于={\pgfpoint{370.384pt}{116.178pt}}]{\sffamily\fontsize{9}{0}\selectfont{38}}
\结束{范围}

% 我的东西--显示bb
\path[draw=black] (当前边界框.西北) 矩形 (当前边界框.东南);
\结束{tikzpicture}
\结束{文档}

答案1

代码中添加了一些完全无用的不可见(或者说是白色)的东西。以下几行

\clip(0pt,403pt) -- (389.957pt,403pt) -- (389.957pt,99.6166pt) -- (0pt,99.6166pt) -- (0pt,403pt);
\color[rgb]{1,1,1}
\fill(3.76406pt,399.236pt) -- (380.923pt,399.236pt) -- (380.923pt,253.19pt) -- (3.76406pt,253.19pt) -- (3.76406pt,399.236pt);
\fill(53.4497pt,394.719pt) -- (374.901pt,394.719pt) -- (374.901pt,289.325pt) -- (53.4497pt,289.325pt) -- (53.4497pt,394.719pt);

绘制比实际图片更大的白色背景。TikZ 看到后会认为它是图片的一部分。只需删除/取消注释这些行即可删除大部分空白。

在第一个范围的末尾,

\color[rgb]{1,1,1}
\fill(3.76406pt,249.426pt) -- (386.193pt,249.426pt) -- (386.193pt,103.381pt) -- (3.76406pt,103.381pt) -- (3.76406pt,249.426pt);

做同样的事情。此外(在第二个结尾附近scope),

\pgftext[center, base, at={\pgfpoint{220.95pt}{106.392pt}}]{\sffamily\fontsize{9}{0}\selectfont{\textbf{ }}}

在图片下方添加一个空白节点,再次扩大边界框。

删除所有这些线会给出一个紧密的边界框。

据我所知,TikZ 无法为您进行裁剪,因为它无法知道白色的东西是否是故意的(例如,图像后面可能有深色背景,因此可以看到白色)。

答案2

\useasboundingbox <some rectangle>当然,只需在 之后插入 即可\begin{tikzpicture}。这样可以让边界框只围绕您想要的内容。查看 PGF 手册中的该命令,您就可以看到它的实际作用。

相关内容