31

I would like to insert figures which were created with TikZ to my LaTeX document.

I suppose that if I create the TikZ figure inside the document, that wouldn't be a problem - I'd just play with the scale value which comes right after \begin{tikzpicture}. However, sometimes the figure is too complicated for it to be written explicitly in the LaTeX document, so I create the figure in a different file and use \input{foo.tikz}. The problem with that is that I don't see how to scale it to fit the rest of the document.

6 Answers 6

38

You can set the every picture locally in a group around the input file to set any option which should be used for the tikzpicture in that file.

\begingroup
\tikzset{every picture/.style={scale=0.3}}%
\input{sometikzpic}%
\endgroup

However, if you have other tikzpictures inside nodes of the main picture they will also be affected (twice I mean). In this case, \tikzset{every picture/.style={scale=0.3,every picture/.style={}}} might work better.


Note that scale scales only coordinates. Text is not affected. You might want to scale the whole picture all together. For this use \scalebox{<factor>}{\input{<file>}} or
\resizebox{<width>}{!}{\input{<file>}}. This both macros come from graphics which is loaded already by tikz.

Also have a look at the standalone class, especially the new \includestandalone[<options>]{<file>} from v1.0beta which will include subfiles and scales them if requested, like \includegraphics does for images.

6
  • I actually much prefer this solution to mine. It can be used without modifying the tikz file. Will this override an explicitly set scale in the included file? Presumably not…
    – Seamus
    Dec 17, 2011 at 14:23
  • @Seamus: AFAIK, the every picture style is executed before the optional settings. So you will end up with two scale settings. I think both are then applied, i.e. multiplied which each other. Dec 17, 2011 at 14:28
  • That did the trick. Thanks! Perhaps it's possible to scale it such as it fits the page's width?
    – Shmuel
    Dec 17, 2011 at 16:27
  • 4
    @Shmuel: \resizebox{\textwidth}{!}{\input{<file>}} or \includestandalone[width=\textwidth]{<file>} with standalone v1.0beta1 Dec 17, 2011 at 17:31
  • absolutely no effect
    – OD IUM
    Mar 16, 2020 at 23:11
9

If inside foo.tex you set the scale to \tkzscl or some other user macro, you can then set it like so:

\def\tkzscl{0.3}
\input{foo}

This way, you can set the scale of the picture in the main file.

foo.tex should then look like this:

\begin{tikzpicture}[scale=\tkzscl]
\draw (0,0) -- (1,0);
\end{tikzpicture}

Or whatever…

0
9

I know this has been answered, so here's a little trick to use these answers.

-1- Put this after your \usepackages and before your \begin{document}:

\newcommand{\inputTikZ}[2]{%  
     \scalebox{#1}{\input{#2}}  
}

-2- Use it like this to create a figure:

\begin{figure}  
\begin{centering}  
    \inputTikZ{0.5}{mytikzfile.tkz}  
\end{centering}  
\label{fig:myFig}  
\caption{some caption for the figure}  
\end{figure}  

In this way, the syntax is similar to \includegraphics[scale=...]{filename}

3
  • 3
    instead of using the environment centering (which I do not know existed), use the command \centering and remove the enviroment. See center vs. centering.
    – nickpapior
    Sep 22, 2012 at 7:14
  • upvoted for usage of scalebox! Oct 19, 2014 at 18:28
  • @zeroth agreed ! Using \begin{centering} \end{centering} doesn't center properly, whereas \centering does.
    – matthieu
    Sep 20, 2018 at 9:11
3

It seems to me that you should use the tikzscale package. Supposing your complicated tikz picture is in the file drawing.tikz, you could do something like:

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents*}{drawing.tikz}
\begin{tikzpicture}

\draw[red,thick,dashed] (0,0) -- (2,1);
\draw (1,1) node[below] {$a+b^2$};

\end{tikzpicture}
\end{filecontents*}


\usepackage{tikz}
\usepackage{tikzscale}

\begin{document}
    \begin{figure}
        \includegraphics[width=\linewidth]{drawing.tikz}
        \caption{A nice drawing.}
    \end{figure}
\end{document}

The same approach also works for related packages, for instance tikz-3dplot, or plots drawn with pgfplots:

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents*}{plot.tikz}
\begin{tikzpicture}
\begin{axis}[
    ylabel={Something },
    xlabel={Something else},
]

\addplot[red,thick,domain=-2:2,samples=100] {exp(-x)+4*sin(deg(x))-x^2};
\addplot[blue,ultra thick,domain=-2:2,samples=100] {2*cos(deg(x))+x};

\end{axis}
\end{tikzpicture}
\end{filecontents*}

\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepackage{tikzscale}

\begin{document}
    \begin{figure}
        \includegraphics[width=\linewidth,height=60mm]{plot.tikz}
        \caption{A nice plot.}
    \end{figure}
\end{document}
1

Following the style presented by fiacobelli's answer.

On Preamble insert the packages

\usepackage{tikz}
\usepackage{adjustbox}

My advice is to create a new command

\newcommand{\figTikzAdjustShortCap}[4]
{   
    \centering
    \adjustbox{#1}{\input{#2}}
    \caption[#4]{#3}
}

You may create variations changing the "adjustbox" line to:

\resizebox{#1\linewidth}{#2\linewidth}{\input{#3}}

or

\scalebox{#1}{\input{#2}}

remembering to correct the number of inputs of the \newcommand.

This follow the same standard I use for includegraphics:

\newcommand{\figIncLongCap}[3]
{   
    \centering
    \includegraphics[#1]{#2}
    \caption{#3}
}

I like it because let me change the caption position of all figure in a single move.

And you may use it inside Figure environment, where "Fig_Tikz" is a .tex file.

\begin{figure}[h]
    \figTikzAdjustLongCap
    {width = 0.8\linewidth, keepaspectratio, trim = 1.5cm 2cm 3cm 0cm, clip = true}
    {Fig_Tikz}
    {Tikz with \textit{AdjustBox} and \textit{Trim} Caption}
    \label{Fig_Tikz}
\end{figure}

This results gives you the same structure of a includegraphic command:

\begin{figure}[h]
    \figIncLongCap
    {width = 0.5\linewidth, trim = 0cm 1cm 0cm 0cm, clip = true}
    {Fig_not_tikz}
    {Fig not Tikz Caption.}
    \label{Fig_not_tikz}
\end{figure}

This option let us work and format a \input exactly like a \includegraphic.

I prefer to use the figure environment because of the label highlight and "click and found" features of TexStudio and others LaTeX editors.

0
1

When using \input to import a figure consisting of a PGFPlots axis, I found that

\begingroup
\pgfplotsset{every axis/.style={scale=2}}
\input{tikzfig}
\endgroup

was the best solution to scale only the axes without changing nor the ticks nor the axes labels font.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .