使用 tikzexternalize 时生成的图形似乎被剪切了

使用 tikzexternalize 时生成的图形似乎被剪切了

当我使用以下代码时,图形似乎被切割了,就像从一个太紧的边界框中切割出来一样。

    \documentclass{article}

    \usepackage{pgf}
    \usepackage{tikz}
    \usepackage{pgfplots}

    \usetikzlibrary{shapes,arrows}
    \usetikzlibrary{calc}
    \usetikzlibrary{external}
    \tikzexternalize

    \usetikzlibrary{positioning} 
    \tikzstyle{seed}=[-latex,red, thick]
    \tikzstyle{ray}=[green!50!black,dotted,thick,-latex]


    \begin{document}
    \newcommand{\door}[1] {
      \begin{scope}[shift={#1}]
      \draw[red] (-0.5,0) arc (180:270:1cm);
      \draw[black] (0.5,-1) -- (0.5,-0) node[right,  near start]{};
      \draw[black,dotted] (-0.5,0) -- (0.5,-0)node(doorstepcenter)[pos=0.5]{} ;
      \end{scope}
    }

    \newcommand{\room}{
     \coordinate (a1) at  (4.5,0.5);
     \coordinate (a2) at  (4.5,4);
     \coordinate (a3) at  (0,4);
     \coordinate (a4) at  (1,3);
     \coordinate (a5) at  (1.4,3)  ;
     \coordinate (a6) at  (1.4,2.6)  ;
     \coordinate (a7) at  (1,2.6)  ;
     \coordinate (a8) at  (1,3);
     \coordinate (a9) at  (0,4);
     \coordinate (a10) at  (0,0.5) ;
     \coordinate (a11) at  (4.5,0.5);
    }

    \begin{tikzpicture}%[node distance =4.5cm,thick] 
      \room; %just get the door
      \door{ ($ (a10)!0.5! (a1) -(0,0.1)  $) };

      \coordinate (r1) at (a6);
      \coordinate (r2) at (a7);
      \coordinate (ray) at  ($ (r1)!.3!(r2) $);
      \draw[ray] (doorstepcenter.center) -- (ray)  node [below=10pt, midway]{ray};
      \path (a1)--(a2) node[pos=0.5,left,text width = 3cm, text centered]{Polygon cannot be closed };
      \draw[seed] (r1) -- (r2); 
      \draw[thick] (a4) -- (a5);
      \draw[thick] (a5) -- (a6);
      \draw[seed] (a7) -- (a8);
      \draw[seed] (a8) -- (a9);
      \draw[seed] (a9) -- (a10);
      \draw[seed] (a10) -- (a11);
      \draw[seed] (a11) -- (a2);
      \draw[seed] (a2) -- (a3);
    \end{tikzpicture}
   \end{document}

左右的箭头似乎被切断了

左右箭头似乎被切断了。这是怎么回事?如何避免?

答案1

发生这种情况是因为您正在使用external库(顺便说一句,您应该更清楚地说明这一点),该库会生成图片的 PDF 图像并将其包含在内,以减少进一步编译器运行的编译时间(图片仅处理一次)。箭头不被视为官方路径的一部分,因此 PGF/TikZ 不会扩展官方边界框来保存它们。相反,它将大小限制为行。也许这个问题将在 PGF/TikZ 的未来版本中得到修复,或者也许有原因导致它没有完成。

您忘记了\tikzexternalize示例代码前言中的宏。没有这个宏,就不会进行外部化,因此您的图片不会被剪切。这就是为什么其他用户一开始可能会重现您的问题的原因。虽然在这种情况下边界框仍然相同,但箭头只是从框中向外看,但仍会显示。

解决这个问题的一种方法是稍微扩展边界框。我找不到可以实现这一点的边距或边框选项external。您可以通过在图片末尾添加使用特殊current bounding box节点的虚拟路径来自己完成此操作。可能还有另一种更好的方法。

\documentclass{article}

\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{external}
\usetikzlibrary{positioning} 
\tikzstyle{seed}=[-latex,red, thick]
\tikzstyle{ray}=[green!50!black,dotted,thick,-latex]
\tikzexternalize


\begin{document}
\newcommand{\door}[1] {
  \begin{scope}[shift={#1}]
  \draw[red] (-0.5,0) arc (180:270:1cm);
  \draw[black] (0.5,-1) -- (0.5,-0) node[right,  near start]{};
  \draw[black,dotted] (-0.5,0) -- (0.5,-0)node(doorstepcenter)[pos=0.5]{} ;
  \end{scope}
}

\newcommand{\room}{
 \coordinate (a1) at  (4.5,0.5);
 \coordinate (a2) at  (4.5,4);
 \coordinate (a3) at  (0,4);
 \coordinate (a4) at  (1,3);
 \coordinate (a5) at  (1.4,3)  ;
 \coordinate (a6) at  (1.4,2.6)  ;
 \coordinate (a7) at  (1,2.6)  ;
 \coordinate (a8) at  (1,3);
 \coordinate (a9) at  (0,4);
 \coordinate (a10) at  (0,0.5) ;
 \coordinate (a11) at  (4.5,0.5);
}

\begin{tikzpicture}%[node distance =4.5cm,thick] 
  \room; %just get the door
  \door{ ($ (a10)!0.5! (a1) -(0,0.1)  $) };

  \coordinate (r1) at (a6);
  \coordinate (r2) at (a7);
  \coordinate (ray) at  ($ (r1)!.3!(r2) $);
  \draw[ray] (doorstepcenter.center) -- (ray)  node [below=10pt, midway]{ray};
  \path (a1)--(a2) node[pos=0.5,left,text width = 3cm, text centered]{Polygon cannot be closed };
  \draw[seed] (r1) -- (r2); 
  \draw[thick] (a4) -- (a5);
  \draw[thick] (a5) -- (a6);
  \draw[seed] (a7) -- (a8);
  \draw[seed] (a8) -- (a9);
  \draw[seed] (a9) -- (a10);
  \draw[seed] (a10) -- (a11);
  \draw[seed] (a11) -- (a2);
  \draw[seed] (a2) -- (a3);
  \path
    ([shift={(-5\pgflinewidth,-5\pgflinewidth)}]current bounding box.south west)
    ([shift={( 5\pgflinewidth, 5\pgflinewidth)}]current bounding box.north east);
\end{tikzpicture}
\end{document}

请注意,类和包external是的替代方案。类提供了一个选项,为您增加了一些余地。这看起来如下所示,并且还需要aka编译器选项。standalonestandaloneborder-shell-escape--enabled-write18

% Main document
\documentclass{article}

\usepackage[subpreambles,mode=buildnew]{standalone}
\begin{document}
    \includestandalone{mytikzpic} % or \input, but here you can use [<options>]
\end{document}
\documentclass[border=1pt]{standalone}

\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\tikzstyle{seed}=[-latex,red, thick]
\tikzstyle{ray}=[green!50!black,dotted,thick,-latex]

\begin{document}
\newcommand{\door}[1] {
  \begin{scope}[shift={#1}]
  \draw[red] (-0.5,0) arc (180:270:1cm);
  \draw[black] (0.5,-1) -- (0.5,-0) node[right,  near start]{};
  \draw[black,dotted] (-0.5,0) -- (0.5,-0)node(doorstepcenter)[pos=0.5]{} ;
  \end{scope}
}

\newcommand{\room}{
 \coordinate (a1) at  (4.5,0.5);
 \coordinate (a2) at  (4.5,4);
 \coordinate (a3) at  (0,4);
 \coordinate (a4) at  (1,3);
 \coordinate (a5) at  (1.4,3)  ;
 \coordinate (a6) at  (1.4,2.6)  ;
 \coordinate (a7) at  (1,2.6)  ;
 \coordinate (a8) at  (1,3);
 \coordinate (a9) at  (0,4);
 \coordinate (a10) at  (0,0.5) ;
 \coordinate (a11) at  (4.5,0.5);
}

\begin{tikzpicture}%[node distance =4.5cm,thick] 
  \room; %just get the door
  \door{ ($ (a10)!0.5! (a1) -(0,0.1)  $) };

  \coordinate (r1) at (a6);
  \coordinate (r2) at (a7);
  \coordinate (ray) at  ($ (r1)!.3!(r2) $);
  \draw[ray] (doorstepcenter.center) -- (ray)  node [below=10pt, midway]{ray};
  \path (a1)--(a2) node[pos=0.5,left,text width = 3cm, text centered]{Polygon cannot be closed };
  \draw[seed] (r1) -- (r2); 
  \draw[thick] (a4) -- (a5);
  \draw[thick] (a5) -- (a6);
  \draw[seed] (a7) -- (a8);
  \draw[seed] (a8) -- (a9);
  \draw[seed] (a9) -- (a10);
  \draw[seed] (a10) -- (a11);
  \draw[seed] (a11) -- (a2);
  \draw[seed] (a2) -- (a3);
  \path
    ([shift={(-5\pgflinewidth,-5\pgflinewidth)}]current bounding box.south west)
    ([shift={( 5\pgflinewidth, 5\pgflinewidth)}]current bounding box.north east);
\end{tikzpicture}
\end{document}

相关内容