How I get y-axis bold & new times roman font when using latex as 'TickLabelInterpreter'?

Hi,
I am trying to use latex as TickLabelInterprete. I want to bold both the x-axis (where I use the x-ticklabel along with latex syntax) and y-axis (nothing used, just normal text). At the same time, I want to set the font of all the texts on the figure to be Times New Roman.
I am attaching my code as follows. It appears to be strange for me, when I do not set the TickLabelInterprete to be latex, everything is fine, I can bold the y-axis easily (both y-label and the number on the y-axis). But when TickLabelInterprete = 'latex', the number on the y-axis will automatically turn back to normal font. Any suggestions?
I would like to thank you in advance!
close all;
data = [1,1,1,1;2,2,2,2;3,3,3,3;4,4,4,4];
bar(data)
hold on
grid on
xticks([1 2 3 4])
xtickangle(0)
ax = gca;
ax.TickLabelInterpreter = 'latex';
xTicklabels = {'\textbf{label 1} \boldmath$E_1$', ...
'\textbf{label 2} \boldmath$E_2$', ...
'\textbf{label 3} \boldmath$E_3$', ...
'\textbf{label 4} \boldmath$E_4$'};
set(gca,'XTickLabel',xTicklabels);
xlabel("Different algorithms");
ylabel("Result of schemes");
lgd = legend("1",...
"2",...
"3",...
"4",...
'Location',"northwest");
lgd.FontWeight = "bold";
h = gca;
h.XAxis.FontWeight = 'bold';
h.XAxis.LineWidth = 1;
h.YAxis.FontWeight = 'bold';
h.YAxis.LineWidth = 1;
set(gca,'fontname','times');
hold off;

 Risposta accettata

You can use
ax.YTickLabel = strcat('\textbf{',ax.YTickLabel,'}');
to make the YTickLabels bold with the LaTeX interpreter, as shown below.
close all;
data = [1,1,1,1;2,2,2,2;3,3,3,3;4,4,4,4];
bar(data)
hold on
grid on
xticks([1 2 3 4])
xtickangle(0)
ax = gca;
ax.TickLabelInterpreter = 'latex';
xTicklabels = { ...
'\textbf{label 1} \boldmath$E_1$', ...
'\textbf{label 2} \boldmath$E_2$', ...
'\textbf{label 3} \boldmath$E_3$', ...
'\textbf{label 4} \boldmath$E_4$'};
set(gca,'XTickLabel',xTicklabels);
ax.YTickLabel = strcat('\textbf{',ax.YTickLabel,'}'); % include this line
xlabel("Different algorithms");
ylabel("Result of schemes");
lgd = legend( ...
"1",...
"2",...
"3",...
"4",...
'Location',"northwest");
lgd.FontWeight = "bold";
ax.XAxis.FontWeight = 'bold';
ax.XAxis.LineWidth = 1;
ax.YAxis.FontWeight = 'bold';
ax.YAxis.LineWidth = 1;
ax.FontName = 'times';
hold off;

6 Commenti

Thanks Voss! It does work for me :)
Is there a common approach to simply get the x-axis and y-axis values in bold?
I've read quite often that " 'fontweight' , 'bold' " is essential to get the values in bold. However, I've tried several different variations of that command for the following code but none of it worked.
f.title = 12;
f.axis = 4;
f.legend = 4;
f.ticks = 4;
figure(1)
hold on
grid on
plot(t,v,'LineWidth',2);
title('$\textbf{V}$','FontSize',f.title,'interpreter','latex');
hold off
xticks(0:4:(length(v)-1));
yticks([0 0.2 0.4 0.6]);
axis([0 20 0 0.6]);
ylabel('$\textbf{Y}$','FontSize',f.axis,'FontWeight', 'bold','Interpreter','latex');
xlabel('$\textbf{X}$','FontSize',f.axis,'FontWeight', 'bold','interpreter','latex');
set(gca,'ticklabelinterpreter','latex','Fontsize',f.ticks);
set(gca,'ticklabelinterpreter','latex');
Many thanks!
When using LaTeX interpreter, FontName, FontWeight, and FontAngle have no effect; you'd set those in the LaTeX string (like you are already doing to get bold font weight).
For tick labels with interpreter LaTeX, you can set the X/YAxis TickLabelFormat to include the necessary LaTeX syntax. Example:
t = 1:20;
v = 0.6*rand(1,20);
f.title = 22;
f.axis = 10;
figure()
hold on
grid on
plot(t,v,'LineWidth',2);
title('$\textbf{V}$','FontSize',f.title,'interpreter','latex');
hold off
xticks(0:4:(length(v)-1));
yticks([0 0.2 0.4 0.6]);
axis([0 20 0 0.6]);
ylabel('$\textbf{Y}$','FontSize',f.axis,'Interpreter','latex');
xlabel('$\textbf{X}$','FontSize',f.axis,'interpreter','latex');
ax = gca();
set(ax,'ticklabelinterpreter','latex')
set([ax.XAxis ax.YAxis],'TickLabelFormat','\\textbf{%g}','FontSize',f.axis)
Thank you so much! It's great that you don't just provide a solution to the problem but also illustrate it with an example and explain what you are doing and what it means; many thanks !

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2021a

Richiesto:

il 14 Lug 2022

Commentato:

il 27 Mar 2024

Community Treasure Hunt

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

Start Hunting!

Translated by