# About saving a drawing as a tikz file

This answer describes how to include a tikz file in your document, whether it is an article, a book or a beamer presentation.

It works fine, up until the point that you call for some other libraries.

Suppose I have the following graphics:

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

As you can see it consists of mainly a tikzpicture environment, but it also calls for the math tikz library, and declares variables \x1 and \y1 outside the tikzpicture environment.

As yet another example, consider this pgfplots part of code:

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}

It makes use of the arrows library and sets the pgfplots version, outside the tikzpicture environment, too.

Is it possible to create a file myDrawing.tikz that I can transplant in my main document, and also make use of the tikzscale package? If the answer is yes, then how? Can you post an example?

I am having difficulties with correctly and nicely (in)putting a drawing inside my document. Particularly with the height-to-width aspect ratio.

Until now I saved the drawing as a tex file, under a standalone document class and worked with includestandalone[width=...]{path/to/file} as described here. But sometimes I want the figure to be inside a \begin{columns} \column{0.4\textwidth} \end{columns} environment in a beamer presentation, and sometimes in the side margin of a book document, with a width=\marginparwidth option.

The tikzscale package looks very promising, but its documentation instructs its use only when working with tikz files.

• This sounds a bit complicated :) do you use the same drawings in many different documents? If not then adding the full code of the drawings in the document itself seems like the easiest solution - possibly combined with an editor that supports code folding if you think that all the code is cluttering the view. – Marijn May 3 at 19:04
• @Marijn I would give it a try, as I am working with Vim and folding is possible there. Can you show explicitly how? – tush May 3 at 19:05
• Most commands like \tikzset or \pgfmath can be used both inside or outside a tikzpicture environment. The only exception I know of is \tikzfadings (see tex.stackexchange.com/questions/277784/…). – John Kormylo May 3 at 19:14
• @John Kormylo I didn't know that! That works, and it sounds like a good answer to my question. – tush May 3 at 20:42