emax
emax

Reputation: 7235

How to display LaTeX symbols in the xtick labels in MATLAB?

I am trying to plot the following:

figure
plot([0,10000],[20,20])
set(gca,'XTick',[0 10000])
set(gca,'XTickLabel',({'0','\phi', 'interpreter', 'latex'}))

But I am not able to display \phi as the Greek letter.

Upvotes: 1

Views: 7248

Answers (3)

gnovice
gnovice

Reputation: 125864

In newer versions of MATLAB...

...this can be accomplished by setting the 'TickLabelInterpreter' property to 'latex'. Here's an example in R2016b:

figure();
plot([0 10000], [20 20]);
set(gca, 'XTick', [0 10000]);
set(gca, 'XTickLabel', {'0', '$\phi$'}, 'TickLabelInterpreter', 'latex');

enter image description here

In older versions of MATLAB...

...before the 'TickLabelInterpreter' property was added, the easiest way I know of to add Greek letters to axes tick labels is to change the 'FontName' property to 'symbol'. Here's an example from R2013a:

figure();
plot([0 10000], [20 20]);
set(gca, 'XTick', [0 10000]);
set(gca, 'XTickLabel', {'0', 'f'}, 'FontName', 'symbol');

enter image description here

Upvotes: 4

ViG
ViG

Reputation: 1868

A really dirty way to do it is to add a picture of phi on the tick place. The problem with this is the background of the figure. By default this is grey, but when saving the figure this becomes white, so if your picture has a white background, you let it be so you can save it, or change it to the background of the figure (or change the background of the figure to white so you cover both cases at onces).

The code

figure
plot([0,10000],[20,20])
set(gca,'XTick',[0 10000])
set(gca,'xticklabel',{'0',''})

% get needed stuff
bg = get(gcf, 'color');   % or set(gcf, 'color',[1 1 1]) to change background of figure to white
box = get(gca, 'Position');
ticklength = get(gca, 'TickLength');
tightinset = get(gca, 'TightInset');
xlims = get(gca, 'XLim');
I = imread('phi.png');

% change white background of picture to background of figure
% for ii=1:3
%     tmp = I(:,:,ii);
%     tmp(tmp == 255) = bg(ii)*255;
%     I(:,:,ii) = tmp;
% end

% position of image
xpos = box(1) + (10000 - xlims(1)) * (box(3)) / (xlims(2) - xlims(1)) - ticklength(1);
ypos = box(2) - tightinset(2) + ticklength(2);

axes('position', [xpos ypos ticklength(1) ticklength(2)])  %[0.9400 0.9400 0.9400]

image(I)
set(gca, 'visible','off')

results in

figure with phi

Upvotes: 1

Ander Biguri
Ander Biguri

Reputation: 35525

You are setting the labels to the words 'interpreter' and 'latex'. To see what I mean run:

figure
plot([0,10000],[20,20])
set(gca,'XTick',[0 2000 4000 10000])
set(gca,'xticklabel',({'0','\phi', 'interpreter', 'latex'}))

enter image description here

(note that my default interpreter is latex so \phi shows correctly)

If you want, in general a latex interpreter, you can set the default of MATLAB as:

set(0, 'defaultTextInterpreter', 'latex');

and you dont need to worry about latex anymore.

Upvotes: 2

Related Questions