如何在 Matlab 中使用 Latex 创建带有变量的标题?

How can I create title with variable using Latex in Matlab?

我有一个 Matlab 代码可以使用 Latex 生成表示两个分数相乘的标题。

t=title('$\frac{5}{2} \times \frac{3}{4}$');
set(t,'Interpreter','Latex');

我的问题是如何将Latex方程中的数字替换为变量? 例如,如果我定义

A = 5;
B = 2;
C = 3;
D = 4;

如何用A,B,C,D代替latex表格标题上的数字?

您将需要使用 [] 来连接字符串

titlestr = ['$\frac{', num2str(A), '}{', num2str(B), '} \times \frac{', num2str(C), '}{', num2str(D)'}$'];
title(titlestr)

或者您可以使用 sprintf,但您必须确保转义所有 \ 个字符

titlestr = sprintf('$\frac{%d}{%d} \times \frac{%d}{%d}$', A, B, C, D);
title(titlestr)