# tikz+matlab2tikz

I'm quite new at latex, but finding it awesome. Yet when it comes to figures things do get a bit tiresome.

I'm currently linking a big amount of matlab plots with latex articles with matlab2tikz. After downsampling the figures quite a lot, it finally got correctly linked.

But the thing is that now I need to control the display properties of the figures in the document WITHOUT changing stuff in the document of the picture itself.

The way I include the plot in latex is:

\input{/path/fig.tex}
\FloatBarrier


But I can't control very well scale, centering and so on.. tried with construction like:

\begin{figure}[scale=0.5]
\center
\input{/path/fig.tex}
\end{figure}


or the same with a {tikzpicture}... Maybe setting this kind of parameters in the matlab2tikz command itself, in the Matlab script. Well, any help is appreciated..

## 3 Answers

With regard to scaling plots, there are a few options. Is there anything else you want to do?

### Option 1 - Setting width and height

When using matlab2tikz you can specify the width and height of figures:

matlab2tikz('nameoffile.tex','width','8cm','height','4cm');


Note that, if I remember correctly, this will set the size of just the axis. Hence, the total width will be 8cm + the width of yticks, ylabel, and other stuff next to the axis itself.

This won't help you with scaling the plots on the LaTeX end of course, but what you can do instead is to set the width to macros that you define in your LaTeX document. For example, you can say

matlab2tikz('nameoffile.tex','width','\figW','height','\figH');


in Matlab, and have a LaTeX file such as

\documentclass{article}
\usepackage{pgfplots,amsmath}

\newlength\figH
\newlength\figW
\setlength{\figH}{4cm}
\setlength{\figW}{8cm}

\begin{document}

Some text, then a centred plot:

\begin{center}
\input{firstplot}
\end{center}

More text, then a wider plot:

\begin{center}
\setlength{\figW}{10cm} % when added inside the  center environment it has no effect outside it
\input{secondplot}
\end{center}

\end{document}


### Option 2 - Adding a scale parameter

You also have the option to specify additional options for the tikzpicture and axis environments generated by matlab2tikz. This way you can set a scale parameter for one of them, and have a macro holding the value. In Matlab, do

matlab2tikz('nameoffile.tex','extraAxisOptions','scale=\figurescale');


and have a LaTeX file as

\documentclass{article}
\usepackage{pgfplots,amsmath}

\newcommand\figurescale{1} % set default scaling to 1

\begin{document}

Some text, then a centred plot:

\begin{center}
\input{firstplot}
\end{center}

More text, then a twice as wide plot:

\begin{center}
\renewcommand{\figurescale}{2} % when added inside the  center environment it has no effect outside it
\input{secondplot}
\end{center}

\end{document}


Adding the scale parameter to the axis will not change the size of tick labels and axis labels. If you use 'extraTikzpictureOptions','scale=\figurescale in the call to matlab2tikz, instead of 'extraAxisOptions', scaling will change the size of axis labels etc.

### Option 3 - the tikzscale package

(I totally forgot to add this option, which is actually quite convenient. SOrry about that.)

The tikzscale package modifies the \includegraphics command so that it can be used instead of \input for .tikz files, and the same scaling options can be used.

Note that you must use .tikz as the file ending, .tex is not recognized.

A quick code example:

\documentclass{article}%

\usepackage{pgfplots,filecontents}
\usepackage{tikzscale}

% the filecontents environment writes its content to the specified file
\begin{filecontents*}{fig.tikz}
\begin{tikzpicture}
\begin{axis}
\addplot{x};
\end{axis}
\end{tikzpicture}
\end{filecontents*}
\begin{document}

\includegraphics{fig.tikz}

\includegraphics[width=.7\linewidth]{fig.tikz}

\includegraphics[height=6cm]{fig.tikz}

\end{document}


Lastly a note about your second code snippet:

\begin{figure}[scale=0.5]
\center
\input{/path/fig.tex}
\end{figure}


There are two things that are wrong here.

1. The figure environment is just a floating container in which you can put stuff, such as images. It has no options for scaling or similar, so [scale=0.5] does nothing here.
2. The correct command for centering is \centering, not \center. There is also a center environment, used as \begin{center} ... \end{center}, but within figures it is recommended to use \centering (see Should I use center or centering for figures and tables?).
• Note: I didn't actually test any of the code above, so if anyone spots silly errors, let me know. – Torbjørn T. Apr 25 '14 at 15:44
• Torb, thank you very much for your extended and comprehensive response. I will try both options and see which fits better. The second one seams to avoid me re-do the plots, but in the long term that's not an issue. Also thanks for the constructive corrections. If I have any further doubts I'll be posting but your answer should be more than enogh. THX – Est Apr 25 '14 at 16:28

Questions:

• What display properties do you want to control ? Width, height ... ?
• Can you provide exemple where the centering is a problem ? Because for what I have tried \center does the job.

Answers:

• One possibility is to change your tikz files to images which you can scale easily like so: \includegraphics[width=0.425\linewidth]
• The other one is to work inside the tikz files but you seem unwilling to do so.

Both options can be achieved using a bash script. Leave a comment if you are interested and I will go further into details.

• \centering or \begin{center}...\end{center}, for figure and table environments the former. – Torbjørn T. Apr 25 '14 at 15:42
• Yeah It would be scaling in principle but general control is the point. Yes, center does the job indeed. I misspoke, the idea was to control things such as centring, or scale, being the latter the one that I actually had trouble with. I want to automatize the process and since I'm not very experienced with building bash scripts, the only option I had in mind was one 100% restrain within Latex. Converting .tex "pictures" to actual ones, in an automatic way would be a solution for other problems such as compiling time and memory available... – Est Apr 25 '14 at 16:39

Another easy way to have well scaled tikz figures in your main latex document is generating the tikz figure with standalone option

matlab2tikz('plot.tex', 'standalone', true)

Later in your main.tex (file which includes your plot.tex file), use in the preamble \usepackage{standalone} and include your tikz figure as:

\includestandalone[width=\textwidth]{plot.tex}

where you can change width and height sizes, all this without modify your generated matlab2tikz file.