包含独立 TikZ 图像时如何合并子前导码?

包含独立 TikZ 图像时如何合并子前导码?

我想将 TikZ 图像包含到我的文档中。我希望它是自包含的,这样我就不必用它的所有前言来污染我的主文档。根据standalone手动的(第 22 页) 可以使用选项包含这些前导码subpreambles。但是当我设置时,subpreambles=true我收到错误,并且前导码无论如何都会被忽略。我如何合并 TikZ 代码的前导码?

这是我的主文档的代码:

\documentclass{book}
\usepackage{tikz}
\usepackage{standalone}
\begin{document}
Text ...
\begin{figure}
    \includestandalone[subpreambles=true]{mytikz}
    \caption{My TikZ picture}
    \label{fig:tikz:my}
\end{figure}
\end{document}

这是我的图片的代码mytikz.tex

\documentclass[tikz]{standalone}
\usepackage{tikz}
\tikzstyle{object_representation} = [
    align = center,
    draw = black,
    fill = white,
    minimum width = 2cm,
    minimum height = 2cm,
    rectangle,
    rounded corners,
]
\begin{document}
\begin{tikzpicture}[node distance = 3cm]
    \node (foo) [object_representation] {Foo};
\end{tikzpicture}
\end{document}

这是我收到的错误:

ABD: EveryShipout initializing macros

Package xkeyval Warning: key `subpreambles' has been disabled on input line 7.

(mytikz.tex

! Package pgfkeys Error: I do not know the key '/tikz/object_representation' an
d I am going to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.18         \node (foo) [object_representation]
                                                 {Foo};
No pages of output.
Transcript written on main.log.

答案1

请尝试以下操作:

\documentclass{book}
\usepackage{tikz}
\usepackage[subpreambles=true]{standalone} % <--- option should be here

\begin{document}
Text ...
\begin{figure}
    \includestandalone{mytikz}
    \caption{My TikZ picture}
    \label{fig:tikz:my}
\end{figure}
\end{document}

编辑:我会mytikz很快编写您的文件并使用最新的语法来确定形状样式:

\documentclass[tikz]{standalone}% <-- it is sufficient to has `tikz` only here
\tikzset{object_representation/.style = {% <---
    align = center,
    draw = black,
    fill = white,
    minimum width = 2cm,
    minimum height = 2cm,
    rectangle,
    rounded corners}
        }

\begin{document}
\begin{tikzpicture}
    \node (foo) [object_representation] {Foo};
\end{tikzpicture}
\end{document}

相关内容