# 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}
\addplot { .... };
\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. May 3, 2021 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, 2021 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/…). May 3, 2021 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, 2021 at 20:42

I would try to keep it simple and put the full TikZ code in the document. You can use code folding to hide the details while editing your document.

This would normally hide only the tikzpicture itself, and not the preamble settings. However, I would consider that a feature, because the preamble settings influence other tikzpicture environments as well so it is good to know what the settings are by keeping them in view. Alternatively, you can add most of these settings directly above the environment itself and fold them together with the environment.

An example of basic folding in Vim:

1. Enter full code. Set the foldmethod to manual.
2. Select tikzpicture enviroment with visual selection (ctrl-v)
3. Press zf to fold. Continue editing the rest of the document. If you want to go inside the fold, press zd.
4. When you exit Vim enter :mkview to save the folds. When you start Vim the next time the code shows unfolded. Enter :loadview to re-apply the folds from the previous session.

Note that you can of course automate most of the commands in your .vimrc.

One can do

%main.tex
\documentclass{article}
\usepackage{tikz,tikzscale}
\usetikzlibrary{math} %needed tikz library
\begin{document}
\begin{figure}
\centering
\includegraphics[width=50pt]{myDrawing}
\end{figure}
\end{document}


where myDrawing.tikz is

%myDrawing.tikz
\tikzmath{\x1 = 1; \y1 =1; \x2 = \x1 + 1; \y2 =\y1 +3; }
\begin{tikzpicture}
\draw[very thick, -stealth] (\x1, \y1)--(\x2, \y2);
\end{tikzpicture}


In other words, the tikz and pgfplots libraries may be loaded not necessarily inside the tikzpicture environment, but also in the document preamble.