1

Here's a very simple LaTeX document:

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

\usetikzlibrary{calligraphy}

I used a tikz library \end{document}

That code produces this:

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

If I have the \usetikzlibrary call in the preamble, it doesn't do that.

However, this is a file with a TikZ picture in it that I'm trying to include or input in the middle of other documents.

For those wondering, the calligraphy library has "\RequirePackage{spath3}"

A simple version of the file I want to include:

\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}

I get this into the main file with a simple \input{spath-content}, assuming the file is named spath-content.tex.

Hack Saw
  • 113

3 Answers3

3

The issue is with the line that you pull out: \RequirePackage{spath3}. Since the calligraphy library depends on code from the spath3 package, it has to load in those files. When I wrote it, I chose to use \RequirePackage because then I didn't have to mess around with checking whether the spath3 files had already been loaded.

The issue that you are encountering is that \RequirePackage (and \usepackage) are only designed to be used in the preamble, and using them in the body throws an error. So you get the same issue with:

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

Now, since \usetikzlibrary keeps a list of the libraries that it has already imported and doesn't re-import one that's already on the list then a simple way to avoid this is to put \usetikzlibrary{calligraphy} in your preamble. This isn't quite the same as the suggestion in the comments: that was to move that line to your preamble. I'm suggesting this in addition to having it in the included files. The point then is that by loading it in the preamble then when TeX encounters \usetikzlibrary{calligraphy} in your included file then it simply ignores it.

\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}

This will probably save you a few headaches elsewhere, as loading a library mid-document can change the behaviour of other things unexpectedly. So it is good practice to load them all in the preamble to avoid surprises.

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • See, the problem is that that means I'd have to scan through inclusions to see if they happen to include so I can insert it into the preamble. At that point I might as well conclude that it's not worth it. – Hack Saw Oct 09 '23 at 20:20
  • For now, I have discovered a workaround: I put the call into a \phantom call, a thing I just found in the TeXBook. – Hack Saw Oct 09 '23 at 20:22
  • Urgh, nope that doesn't actually work. – Hack Saw Oct 09 '23 at 20:53
  • grep '\\usetikzlibrary' *.tex – Andrew Stacey Oct 09 '23 at 21:55
  • What does the file you are trying to include look like? Where does it come from? If it has \usetikzlibrary in it then it's a bit of an odd file to be including in another document. I think that to know how best to help then we need a bit more detail on the setup. – Andrew Stacey Oct 09 '23 at 21:59
  • The structure I was using had the \usetikzlibrary immediately before \begin{tikzpicture}. I'll add a simplified section to the question. – Hack Saw Oct 09 '23 at 22:27
  • 2
    I can see what you're trying to do, but given that \usepackage and \RequirePackage are preamble-only, and I doubt I'm the only tikzlibrary author to use them, then this is prone to difficulty - and that's leaving aside the issue of loading packages mid-document which could have unforeseen effects on the rest of that document. A few different solutions spring to mind, which is right depends on you. One is to split each picture into its "preamble" and its "picture", so in the document preamble you load in all the sub-preambles and then use the pictures in the document itself. – Andrew Stacey Oct 09 '23 at 22:53
  • A more complicated solution would be to load in each file twice, once in the preamble where only \usetikzlibrary commands get executed, and once in the document with \usetikzlibrary set to a no-op. – Andrew Stacey Oct 09 '23 at 22:54
  • The preamble/body split idea is one I shall investigate, tomorrow. Thanks for your help. – Hack Saw Oct 09 '23 at 23:22
3

Rather than splitting the preambles of your files out manually, you might prefer to use standalone. Basically, each of the files you want to \input gets set up with

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

<content> need not be an image. It could be anything.

All you then need to do in your main file is load the standalone package with the options you prefer. standalone can be configured to extract the preambles of all the files you \input, assemble a list of packages, tikz/pgf libraries etc., sort the list, remove duplicates and combine any used options. It then passes the relevant options to the packages and libraries, which are loaded at the end of the preamble on your next run.

During the initial compilation, you won't get any output from the \input files. Instead, their contents will be skipped and their preambles assembled for use on the next run. This is to ensure an attempt to typeset their content is only made when the necessary packages and libraries have been loaded. On the first run, this may well not be true because the main file need not include everything required.

Here's a simple example,

\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}

On the first run, only the text of the main file is typeset. Following a second run, the digested preambles from the \input files is loaded and their contents included.

result with digested subpreambles on second run

Here's the file containing the subpreambles, \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

If you'd like standalone to write the digested subpreambles to file, too, add print when loading the package in your main file. For the example above, \jobname.stp contains

% 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'

Note that it's generally better not to include spurious spaces. In this case, it doesn't matter, but habitually including unnecessary spaces makes it easy to add them when it does. (The exception is use of the expl3 programming layer, where spaces are always discarded and you can include as many as you wish. But this design is partly motivated by the problems caused by inadvertent additions of spaces in non-expl3 code!)

cfr
  • 198,882
1

Here's an example how to use the tikzlibrary on calligraphy:

result

\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}

MS-SPO
  • 11,519