将 tikz 图形保存在外部文件中

将 tikz 图形保存在外部文件中

我有几个 tikz 图形,其中一些有超过 400 行代码。这使我在主文件上的工作变得复杂,因为我必须经常滚动浏览所有代码,所以我想把它放在一个外部文件中,然后我可以简单地将其包含在我的主文档中。现在我不知道该怎么做,我看到两种方法:

  • 将每张图片放在单独的文件中,编译为 PDF,并包含 PDF 图表
  • 将每张图片放在单独的文件中,包含文件,编译主文档

哪种方法最好?我该怎么做?例如,在选项二中,我不知道什么子文件中需要代码:我应该开始定义\documentclass并像处理独立文档一样进行操作吗,或者我是否可以将“剪切”出主文档的代码放进去?

那我该怎么办呢include???input

平均能量损失

\documentclass{scrreprt}
\usepackage{tikz}
\usepackage{lipsum}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        

\begin{document}
\chapter{Foo}
\lipsum[1]

\begin{figure}[h!]
    \centering
    \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]

    %Shape: Rectangle [id:dp6874714272670446] 
    \draw   (600.5,167) -- (608.5,167) -- (608.5,181.5) -- (600.5,181.5) -- cycle ;
    %Shape: Rectangle [id:dp25216565311879857] 
    \draw   (569,127) -- (639,127) -- (639,167) -- (569,167) -- cycle ;
    %Shape: Rectangle [id:dp10652138678304235] 
    \draw   (593.5,181.5) -- (614.5,181.5) -- (614.5,188.5) -- (593.5,188.5) -- cycle ;
    %Shape: Rectangle [id:dp27982392636707765] 
    \draw   (572.5,130.71) -- (635.5,130.71) -- (635.5,163.29) -- (572.5,163.29) -- cycle ;
    %Shape: Rectangle [id:dp29031818189807423] 
    \draw   (572.75,193.2) -- (637.25,193.2) -- (637.25,212.43) -- (572.75,212.43) -- cycle ;
    %Shape: Rectangle [id:dp6960940497473749] 
    \draw   (577.35,196.4) -- (581,196.4) -- (581,198.41) -- (577.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp012575111125552318] 
    \draw   (583.35,196.4) -- (587,196.4) -- (587,198.41) -- (583.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp7462575500863122] 
    \draw   (589.35,196.4) -- (593,196.4) -- (593,198.41) -- (589.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp74377024282381] 
    \draw   (595.35,196.4) -- (599,196.4) -- (599,198.41) -- (595.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp5681813434227054] 
    \draw   (577.35,200.41) -- (581,200.41) -- (581,202.41) -- (577.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp45670728197953436] 
    \draw   (583.35,200.41) -- (587,200.41) -- (587,202.41) -- (583.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp4491046180017293] 
    \draw   (589.35,200.41) -- (593,200.41) -- (593,202.41) -- (589.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp6731288033777918] 
    \draw   (595.35,200.41) -- (599,200.41) -- (599,202.41) -- (595.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp7160498396948172] 
    \draw   (601.35,196.4) -- (605,196.4) -- (605,198.41) -- (601.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp13325729640208062] 
    \draw   (607.35,196.4) -- (611,196.4) -- (611,198.41) -- (607.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp2394144125721973] 
    \draw   (601.35,200.41) -- (605,200.41) -- (605,202.41) -- (601.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp19114186601978278] 
    \draw   (607.35,200.41) -- (611,200.41) -- (611,202.41) -- (607.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp6393250197036928] 
    \draw   (616.35,196.4) -- (620,196.4) -- (620,198.41) -- (616.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp5875707462557691] 
    \draw   (616.35,200.41) -- (620,200.41) -- (620,202.41) -- (616.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp2632343129417658] 
    \draw   (622.35,196.4) -- (626,196.4) -- (626,198.41) -- (622.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp16732048982102876] 
    \draw   (628.35,196.4) -- (632,196.4) -- (632,198.41) -- (628.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp994069766161821] 
    \draw   (622.35,200.41) -- (626,200.41) -- (626,202.41) -- (622.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp22742437505582824] 
    \draw   (628.35,200.41) -- (632,200.41) -- (632,202.41) -- (628.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp7394772484697936] 
    \draw   (577.35,204.42) -- (581,204.42) -- (581,206.42) -- (577.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp22090387903101294] 
    \draw   (583.35,204.42) -- (587,204.42) -- (587,206.42) -- (583.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp3094625226147294] 
    \draw   (589.35,204.42) -- (593,204.42) -- (593,206.42) -- (589.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp2693883419330956] 
    \draw   (595.35,204.42) -- (599,204.42) -- (599,206.42) -- (595.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp879541864109227] 
    \draw   (577.35,208.42) -- (581,208.42) -- (581,210.43) -- (577.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp005070405348506002] 
    \draw   (583,208.42) -- (599.35,208.42) -- (599.35,210.43) -- (583,210.43) -- cycle ;
    %Shape: Rectangle [id:dp7109131109721036] 
    \draw   (601.35,204.42) -- (605,204.42) -- (605,206.42) -- (601.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp34805202583561967] 
    \draw   (607.35,204.42) -- (611,204.42) -- (611,206.42) -- (607.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp88037563000339] 
    \draw   (601.35,208.42) -- (605,208.42) -- (605,210.43) -- (601.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp199400132263712] 
    \draw   (607.35,208.42) -- (611,208.42) -- (611,210.43) -- (607.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp4342588107618768] 
    \draw   (616.35,208.42) -- (620,208.42) -- (620,210.43) -- (616.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp9519101603214133] 
    \draw   (622.35,204.42) -- (626,204.42) -- (626,206.42) -- (622.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp4542360869287658] 
    \draw   (622.35,208.42) -- (626,208.42) -- (626,210.43) -- (622.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp5417437553938629] 
    \draw   (628.35,208.42) -- (632,208.42) -- (632,210.43) -- (628.35,210.43) -- cycle ;

    \end{tikzpicture}
\caption{This is just a part of the total picture.}
\end{figure}
\lipsum[2]
\end{document}

答案1

答案很简单,两者都可以。但是,仔细观察就会发现,每种方法都有各自的优点和缺点。

方法 1

%&pdflatex
% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]
\documentclass{scrreprt}
\usepackage{tikz}
\usepackage{lipsum}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        
\usetikzlibrary{external}
\tikzexternalize[prefix=tikzexfig/]
\usepackage{standalone}
\begin{document}
\chapter{Foo}
\lipsum[1]
\begin{figure*}[h]
    \centering{
        \input{ex.tex}
        \caption{This is just a part of the total picture.}
        \label{f1}
    }
\end{figure*}
\lipsum[2]
\end{document}

注意:tikzexfig是主目录内的文件夹。

更新 1:您必须退出 shell 才能编译该文档。

ex.tex

 \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
    %Shape: Rectangle [id:dp6874714272670446] 
    \draw   (600.5,167) -- (608.5,167) -- (608.5,181.5) -- (600.5,181.5) -- cycle ;
    \end{tikzpicture}

优点:

  1. 由于 pdf 中图形的智能缓存,在线编译变得更加容易。

缺点:

  1. 当用于出版物时,您肯定必须使用standalone包来单独编译它们。

  2. 将达到内存限制(pdftex/LaTeX)。

  3. 编辑时,必须pdf's手动删除已生成的。(我目前还不知道有没有更好的自动化方法)。我与@DavidCarlisle 进行了很好的讨论,讨论如何在其周围放置一个包装器,类似于psfrag。然而,结果却是惨败;-)

  4. 第一次编译所有图形需要一些hell-a-lot时间。后续编译会快得多。

方法 2

我不会讨论它,因为它是标准standalone包实现。

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
 \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
    %Shape: Rectangle [id:dp6874714272670446] 
    \draw   (600.5,167) -- (608.5,167) -- (608.5,181.5) -- (600.5,181.5) -- cycle ;
    \end{tikzpicture}
\end{document}

优点:

  1. 更容易实现类似文件的外部化。

  2. 以不同的格式提供输出。有关更多信息,请访问软件包文档。

  3. 我们可以在更短的时间内获取输出并进行编译。

缺点:

  1. 不同的子文件。至少,我还不知道如何将其包含在方法 1

免责声明1:我记得在某处看到过tikzexternalstandalone不应一起使用。但我不记得为什么,也不确定其影响。

免责声明2:对于pdftex/LaTeX这两种方法来说,内存限制本身是一个常见问题。

相关内容