文档中的 \usetikzlibrary{calligraphy} 将“spath3”添加到输出中

文档中的 \usetikzlibrary{calligraphy} 将“spath3”添加到输出中

这是一个非常简单的 LaTeX 文档:

\documentclass{article}
\usepackage{tikz}
\begin{document}
I'd like to use a tikz library here

\usetikzlibrary{calligraphy}

I used a tikz library
\end{document}

该代码产生如下结果:

I’d like to use a tikz library here
spath3
I used a tikz library

如果我在序言中调用 \usetikzlibrary,它不会这样做。

但是,这是一个包含 TikZ 图片的文件,我正尝试将其包含或输入到其他文档的中间。

对于那些好奇的人来说,书法库有“\RequirePackage{spath3}”

我想要包含的文件的简单版本:

\usetikzlibrary{angles,calc,quotes}
\usetikzlibrary{ decorations.pathmorphing, decorations.pathreplacing, decorations.shapes}
\usetikzlibrary{calligraphy}
\edef\ylow{-1.0}
\edef\xlow{-1.0}
\edef\yhi{1.0}
\edef\xhi{1.0}
\begin{tikzpicture}[scale=4.0]
\draw[decorate, decoration={calligraphic brace,amplitude=10pt, raise=13pt}] (\xlow, \ylow) -- (\xhi, \yhi);
    \node [black, above left=14pt] at (0, 0) {$\theta$};
\end{tikzpicture}

我使用一个简单的方法将其放入主文件中\input{spath-content},假设该文件名为spath-content.tex

答案1

您可能更喜欢使用 ,而不是手动拆分文件的前言standalone。基本上,您想要的每个文件\input都会被设置为

\documentclass{standalone}
<packages,libraries etc.>
\begin{document}
<content>
\end{document}

<content>不一定是图像。可以是任何东西。

然后,您需要在主文件中执行的操作就是standalone使用您喜欢的选项加载包。standalone可以配置为提取所有文件的前言,汇编包、 /库等\input的列表,对列表进行排序,删除重复项并合并任何使用的选项。然后,它将相关选项传递给包和库,这些选项将在您下次运行时在前言末尾加载。tikzpgf

在初始编译期间,您不会从文件中获得任何输出\input。相反,将跳过它们的内容并组装它们的前导以供下次运行使用。这是为了确保仅在加载了必要的包和库后才尝试排版它们的内容。在第一次运行时,这很可能不是真的,因为主文件不需要包含所需的所有内容。

这是一个简单的例子,

\begin{filecontents}{\jobname-1.tex}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{ decorations.pathmorphing, decorations.pathreplacing, decorations.shapes}
\usetikzlibrary{calligraphy}
\begin{document}
\edef\ylow{-1.0}
\edef\xlow{-1.0}
\edef\yhi{1.0}
\edef\xhi{1.0}
\begin{tikzpicture}[scale=4.0]
  \draw[decorate, decoration={calligraphic brace,amplitude=10pt, raise=13pt}] (\xlow, \ylow) -- (\xhi, \yhi);
  \node [black, above left=14pt] at (0, 0) {$\theta$};
\end{tikzpicture}
\end{document}
\end{filecontents}
\begin{filecontents}{\jobname-2.tex}
\documentclass{standalone}
\usepackage{tikz}
\usepackage{adforn}
\begin{document}
\begin{tikzpicture}
  \node [font=\Huge, text=blue, draw=blue!50!green]   {\adforn{60}};
\end{tikzpicture}
\end{document}
\end{filecontents}
\documentclass{article}
\usepackage[subpreambles,sort,]{standalone}
\begin{document}
A comment about the following thing.

\input{\jobname-1}

Another comment.

\input{\jobname-2}

A concluding thought.
\end{document}

第一次运行时,只排版主文件的文本。第二次运行时,\input将加载文件中摘要的前言,并包含其内容。

第二次运行的结果已消化子前导

这是包含子序言的文件\jobname.sta

\standalonepreambles
\subpreamble{prawf-1.tex}
\usepackage {tikz}\usetikzlibrary { decorations.pathmorphing, decorations.pathreplacing, decorations.shapes}\usetikzlibrary {calligraphy}
\endsubpreamble
\subpreamble{prawf-2.tex}
\usepackage {tikz}\usepackage {adforn}
\endsubpreamble
\endstandalonepreambles

如果您还想standalone将摘要后的子前言写入文件,请print在主文件中加载包时添加。对于上述示例,\jobname.stp包含

% Packages required by sub-files:
\usepackage{tikz}
\usepackage{adforn}

% TikZ libraries required by sub-files:
\usetikzlibrary{ decorations.pathmorphing}
\usetikzlibrary{ decorations.pathreplacing}
\usetikzlibrary{ decorations.shapes}
\usetikzlibrary{calligraphy}

% Preamble from file 'prawf-1.tex'

% Preamble from file 'prawf-2.tex'

请注意,通常最好不要包含虚假空格。在这种情况下,这并不重要,但习惯性地包含不必要的空格会使在需要时很容易添加它们。(例外是使用编程层expl3,其中空格总是被丢弃,您可以根据需要添加任意数量的空格。但这种设计部分是出于在非expl3代码中无意中添加空格所导致的问题!)

答案2

问题出在你拉出的那行:\RequirePackage{spath3}。由于calligraphy库依赖于spath3包中的代码,因此它必须加载这些文件。当我编写它时,我选择使用,\RequirePackage因为这样我就不必费心检查spath3文件是否已经加载。

您遇到的问题是\RequirePackage(和\usepackage) 仅设计用于序言中,在正文中使用它们会引发错误。因此,您会遇到同样的问题:

\documentclass{article}
\begin{document}
\usepackage{stuff}
\end{document}

现在,由于\usetikzlibrary保留了已经导入的库的列表,并且不会重新导入列表中已经存在的库,因此避免这种情况的一个简单方法是\usetikzlibrary{calligraphy}序言中。这与评论中的建议不太一样:即移动这句话作为你的序言。我建议此外将它放在包含文件中。关键在于,通过在序言中加载它,当 TeX\usetikzlibrary{calligraphy}在你的包含文件中遇到它时,它就会忽略它。

\documentclass{article}
%\url{https://tex.stackexchange.com/q/698047/86}
\usepackage{tikz}
\usetikzlibrary{calligraphy}
\begin{document}
I'd like to use a tikz library here

\usetikzlibrary{calligraphy}

I used a tikz library
\end{document}

这可能会为您省去一些麻烦,因为在文档中间加载库可能会意外改变其他内容的行为。因此,最好在序言中加载它们,以避免意外。

答案3

下面是一个如何在书法上使用 tikzlibrary 的示例:

结果

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calligraphy}% <<< move it here


\begin{document}
I'd like to use a tikz library here:

\begin{center}%         not really needed to demonstrate it
 \begin{tikzpicture}%   this environment can handle Tikz
    \pen (-135:.25) -- (45:.25);
    \draw[line width=.5cm] (0,0) %      ~~~ normal line for reference
        .. controls +(45:1) and +(-135:1) .. ++(3,0);
    \calligraphy (0,-1)  %              ~~~ same line as calligraphy
        .. controls +(45:1) and +(-135:1) .. ++(3,0);
 \end{tikzpicture}
\end{center}%           not really needed to demonstrate it


I used a tikz library
\end{document}

相关内容