# Change font size in matlab2tikz

I am using `matlab2tikz` to export my Matlab figures in order to be able include them in my LaTeX document. I think the fonts of the tick labels, axis labels, titles and legends are too big. How can I define/change the font size into `\small` for an export via `matlab2tikz`. I'd rather not change it manually in for each exportfile in LaTeX.

`matlab2tikz` doesn't seem to have a direct axis font setting, for some reason...

I found two ways of doing this (and a bug in `matlab2tikz` meanwhile):

## The Good

Automatic way

This one seems to give a start to font support in `matlab2tikz`, but I'm probably missing a lot of edge-cases, so it's far from stable, but it does work :)

I'll use this code for testing:

``````x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);

figure
ax = axes;
plot(x,y1,x,y2,'--',x,y3,':')
ax.FontSize = 6;
ax.FontWeight = 'b';
xlabel('x')
ylabel('y')

matlab2tikz('test.tex','standalone',true)
``````

The picture on the left is with the version of `matlab2tikz` that can be downloaded from the FileExchange (with the fix at the bottom of this answer). The picture on the right is the same code, after the patch I propose here:

Obviously the font is far too small, but it's for the sake of the demonstration.

The original code applies font formatting for the axis labels (see that the `x` and the `y` are the same on both versions), but it was omitting the axis tick labels.

To use this patch:

Put the following function either at the end of `matlab2Tikz` or in a separate file called `getAxisFontProps.m` in a folder that Matlab can find:

``````function [m2t, fopts] = getAxisFontProps(m2t, handle)
fopts = {'font' ''};
if strcmp(handle.FontWeight,'bold')
fopts{2} = [fopts{2} '\bfseries'];
end
if strcmp(handle.FontAngle,'italic')
fopts{2} = [fopts{2} '\itshape'];
end
switch handle.FontUnits
case {'points'}
mul = 1;
case {'inches'}
mul = 72.27;
case {'centimeters'}
mul = 28.45275590551181;
otherwise
error('matlab2tikz:getAxisFontProps','Unsupported axis units.')
end
fsize_str = sprintf('\\fontsize{%f}{%f}\\selectfont',...
get(gca,'FontSize')*[1 1.2]*mul);
fopts{2} = [fopts{2} fsize_str];
end
``````

Now, go to line ~`890` of `matlab2tikz.m`, where you'll find these lines:

``````[m2t, xopts] = getAxisOptions(m2t, handle, 'x');
[m2t, yopts] = getAxisOptions(m2t, handle, 'y');

m2t.axes{end}.options = opts_merge(m2t.axes{end}.options, xopts, yopts);
``````

change them to

``````[m2t, xopts] = getAxisOptions(m2t, handle, 'x');
[m2t, yopts] = getAxisOptions(m2t, handle, 'y');
[m2t, fopts] = getAxisFontProps(m2t, handle); % Call the getAxisFontProps to parse the properties

m2t.axes{end}.options = opts_merge(m2t.axes{end}.options, xopts, yopts, fopts);% Add these properties to the main option list
``````

Now the code of the example should produce the picture on the right (don't forget to apply the fix proposed at the bottom of this answer).

Manual way

A simpler version, that doesn't require messing with the code of `matlab2tikz` is to use the `extraAxisOptions` key.

You'll be doing basically the same as in the patch above, but manually. You'll pass the key-value pair to `matlab2tikz`: `'extraAxisOptions','font=\\fontsize{%f}{%f}\\selectfont'`. You can replace the values of `%f` manually bu the font size and the baseline skip. A code to do that without having to change the font size by hand would get the font from the axes handle:

``````x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);

figure
ax = axes;
plot(x,y1,x,y2,'--',x,y3,':')
ax.FontSize = 6;
ax.FontWeight = 'b';
xlabel('x')
ylabel('y')

fsize_str = sprintf('font=\\fontsize{%f}{%f}\\selectfont',...
get(gca,'FontSize')*[1 1.2]);

matlab2tikz('test.tex','standalone',true, ...
'extraAxisOptions',fsize_str)
``````

Both these approaches will pass `font = \fontsize{<some amount>}{<another amount>}` to the `axis` environment. Additionally you can pass any other option to `axis` using the `'extraAxisOptions'` key.

## The Ugly

Bug, apparently

It seems that a quite unfortunate combination of factors break `matlab2tikz`. The code:

``````x = 0:pi/100:2*pi;
y = sin(x);

figure
plot(x,y)
set(gca,'FontSize',12)
xlabel('x')
matlab2tikz('test.tex','standalone',true,'strictFontSize',true)
``````

will produce a `.tex` file with this line:

``````xlabel style={font=\fontsize{1.320000e+01pt}{1em}\bfseries\color{white!15!black}\selectfont},
``````

which contains an invalid `\fontsize{1.320000e+01pt}{1em}` instruction which will break when running the code in the TeX engine.

One way to fix this would be to change line `4653` of `matlab2tikz` (16th line of the `getFontStyle` function) from

``````fontStyle = sprintf('\\fontsize{%d%s}{1em}%s\\selectfont',fontSize,fontUnits,fontStyle);
``````

to

``````fontStyle = sprintf('\\fontsize{%f%s}{1em}%s\\selectfont',fontSize,fontUnits,fontStyle);
``````

(changed the format descriptor from `%d` to `%f`). Matlab, for some obscure reason, turns the `%d` descriptor (for integers) into `%e` (exponential notation) if the number given is not an integer.

Another option would be to botch-in a LaTeX FP parser around the `%d`, but that's probably overkill and much more likely to break than `%f`.