How to put latex in Xtick label? (Matlab 2015b)

How can I have latex \mu and \mu_2 on the dotted lines Xtick? No other Xtick is necessary.
clc;
clear all;
mu=0;
mu2=4;
sigma=1;
x=linspace(mu-4*sigma, mu+4*sigma, 10000);
x2=linspace(mu2-4*sigma, mu2+4*sigma, 10000);
m_x = [mu mu2];
m_y = [0 0];
figure;
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 3.5 2]);plot(x, normpdf (x,mu,sigma),'blue');
hold all;
plot(x2, normpdf (x2,mu2,sigma),'green');
plot(m_x, m_y,'red','LineWidth',3);
yL=get(gca,'YLim');
SP=0;
SP2=4;
line([SP SP],yL,'color','k','LineStyle',':');
line([SP2 SP2],yL,'color','k','LineStyle',':');
hold off

 Accepted Answer

I don’t see where you’re defining them, but to invoke the LaTeX interpreter, use the name-value pair 'Interpreter','latex' in whatever call you want to use it in.

6 Comments

I edited the code above. Now it compiles.
I've been playing around with variants of:
a = axes;
set(a,'TickLabelInterpreter', 'tex');
set(a,'XTickLabel', {'\mu','\mu_2'});
But I had no success at all.
For future reference, this solves:
clc;
clear all;
mu=0;
mu2=4;
sigma=1;
x=linspace(mu-4*sigma, mu+4*sigma, 10000);
x2=linspace(mu2-4*sigma, mu2+4*sigma, 10000);
m_x = [mu mu2];
m_y = [0 0];
figure;
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 3.5 2]);
plot(x, normpdf (x,mu,sigma),'blue');
hold all;
set(gca,'TickLabelInterpreter', 'tex');
set(gca,'XTickLabel', {'\mu','\mu_2'});
set(gca,...
'Units','normalized',...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times',...
'XTick',[0 4],...
'Box','off')
plot(x2, normpdf (x2,mu2,sigma),'green');
plot(m_x, m_y,'red','LineWidth',3);
yL=get(gca,'YLim');
SP=0;
SP2=4;
line([SP SP],yL,'color','k','LineStyle',':');
line([SP2 SP2],yL,'color','k','LineStyle',':');
hold off
The 'TickLabelInterpreter' snuk up on me. (I haven’t used it in R2015b.) It used to be just 'Interpreter' for all text objects, including titles, tick labels, legends, and the rest.
With respect to making your labels compatible with LaTeX, they have to have '$' around them:
set(gca,'XTickLabel', {'$\mu$','$\mu_2$'});
Those should work with the 'latex' option. (This time I tested it to be sure — as I usually do — and didn’t rely only on my previous experience.) LaTeX has capabilities TeX lacks. See The (Not So) Short Introduction to LaTeX2e, particularly section 3.3, page 56.
The problem is when you attempt to put a two-line Xtick. I could do it with TeX interpreter, but not with LaTeX:
clc;
clear all;
mu=0;
mu2=4;
sigma=1;
x=linspace(mu-4*sigma, mu+4*sigma, 10000);
x2=linspace(mu2-4*sigma, mu2+4*sigma, 10000);
y_up=normpdf (0,mu,sigma);
m_x = [mu mu2];
m_y = [0 0];
figure;
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 3.8 2]);
plot(x, normpdf (x,mu,sigma),'blue');
hold all;
set(gca,'TickLabelInterpreter', 'tex');
set(gca,'XTickLabel', {' {first} \newline{mean}','{second} \newline {mean}'});
set(gca,...
'Units','normalized',...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times',...
'XTick',[0 4],...
'YTick',[],...
'YLim',[0 .5],...
'Box','off');
xlabel('outcomes')
ylabel('probability density')
plot(x2, normpdf (x2,mu2,sigma),'green');
yL=[0 y_up];
SP=0;
SP2=4;
line([SP SP],yL,'color','k','LineStyle',':');
line([SP2 SP2],yL,'color','k','LineStyle',':');
hold off
In some contexts, I also wanted to implement a red-colored Xtick. I could not do it with LaTeX or TeX and had to use other means:
clc;
clear all;
mu=0;
mu2=4;
sigma=1;
x=linspace(mu-4*sigma, mu+4*sigma, 10000);
x2=linspace(mu2-4*sigma, mu2+4*sigma, 10000);
y_up=normpdf (0,mu,sigma);
m_x = [mu mu2];
m_y = [0 0];
figure;
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 3.8 2]);
plot(x, normpdf (x,mu,sigma),'blue');
hold all;
set(gca,'TickLabelInterpreter', 'tex');
set(gca,'XTickLabel', {' {first} \newline{mean}','{second} \newline {mean}'});
set(gca,...
'Units','normalized',...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times',...
'XTick',[0 4],...
'YTick',[],...
'YLim',[0 .5],...
'Box','off');
xlabel('outcomes')
ylabel('probability density')
plot(x2, normpdf (x2,mu2,sigma),'green');
plot(m_x, m_y,'red','LineWidth',3);
text(2,-.03,...
'shift',...
'HorizontalAlignment','center','color','red','FontSize',9,...
'FontName','Times');
yL=[0 y_up];
SP=0;
SP2=4;
line([SP SP],yL,'color','k','LineStyle',':');
line([SP2 SP2],yL,'color','k','LineStyle',':');
hold off
It looks ok. I guess an implementation using Xtick would be preferable though if I have to adjust the figure size to incorporate in a paper later on.
I’ve not ever attempted multi-line tick labels, so I would have to experiment as well. Reverting to the text function (the typical approach prior to R2014b) to do what you want seems the best option.
I am somewhat surprised that LaTeX doesn’t offer a way to do multi-line labels, since TeX is a subset of LaTeX, and LaTeX is usually more powerful.
You’ve raised a number of significant issues in your post and explained them thoroughly, so I suggest you bring it to the attention of MathWorks Tech Support. Choose the ‘bug report or enhancement request’ option, and include a link to this thread with an introductory explanation. Have MATLAB open and run ver in the Command Window first.
If you want to have multiple lines in your TickLabels, you must put everything in a \parbox environment:
ax.TickLabelInterpreter='latex';
ax.XTickLabel={'\parbox{4em}{line \#1 \\ line \#2}'};
This produces the following output:
I have not managed to get LaTeX colors working. It only seems to be working in TeX:
ax.TickLabelInterpreter='tex';
ax.XTickLabel={'\color{red}RED','\color{blue}BLUE'};
The output of this is:

Sign in to comment.

More Answers (0)

Categories

Asked:

on 10 Jan 2016

Edited:

on 15 Nov 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!