Graphics, Figures & Tablesinput tikz file when using a tikz library

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
tush
Posts: 11
Joined: Fri Feb 22, 2019 2:54 pm

input tikz file when using a tikz library

Post by tush »

I have the following drawing made with tikz:

Code: Select all

% File myDrawing.tex
\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{math} %needed tikz library
\tikzmath{\x1 = 1; \y1 =1; \x2 = \x1 + 1; \y2 =\y1 +3; } 

% Using the variables for drawing
\begin{tikzpicture}
    \draw[very thick, -stealth] (\x1, \y1)--(\x2, \y2);
\end{tikzpicture}

\end{document}
Of course, I can create a `tikz` file with the name `myDrawing.tikz`, which I want to transplant in my article:

Code: Select all

\documentclass{article}
\begin{document}
Lorem ipsum, and this is a figure:
\begin{figure}
\input{myDrawing.tikz}
\caption{Caption to My drawing}
\end{figure}
\end{document}
where the tikz file is simply the `tikzpicture` environment from the first code snippet above.

But notice that this tikz picture has a call for a tikzlibrary (math) and also defines some coordinates.

What is the correct way to create a tikz file to be included in a document (article, book, beamer) that also has lines of code outside the tikzpicture environment?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Post Reply