LaTeX interpreter with multiple strings or character vectors

9 次查看(过去 30 天)
I have a series of values in units of π that I want to label on the x-axis using xticklabels(mylabels). I want to typeset the units using LaTeX command. Thus I specified latex as the interpretor for my tick labels using the commands:
ax=gca;
ax.TickLabelInterpreter='Latex';
The labels are displayed correctly when I set mylabels as cell array of character vectors as follows:
mylabels={'$-2\pi$','$-\frac{3\pi}{2}$','$-\pi$','$-\frac{\pi}{2}$','0~~~~','$\frac{\pi}{2}$','$\pi$','$\frac{3\pi}{2}$','$2\pi$'};
However, when I specify mylabels as a cell array of strings, or a string array, or a character array as follows, the labels are not displayed on the x-axis.
  1. mylabels=['$-2\pi$','$-\frac{3\pi}{2}$','$-\pi$','$-\frac{\pi}{2}$','0~~~~','$\frac{\pi}{2}$','$\pi$',... '$\frac{3\pi}{2}$','$2\pi$'];
  2. mylabels={"-2\pi","-\frac{3\pi}{2}","-\pi","-\frac{\pi}{2}","0~~~~", "\frac{\pi}{2}","\pi","\frac{3\pi}{2}","2\pi"};
  3. mylabels=["-2\pi","-\frac{3\pi}{2}","-\pi","-\frac{\pi}{2}","0~~~~","\frac{\pi}{2}","\pi","\frac{3\pi}{2}","2\pi"];
I would like to understand what's the problem here.
A complete MWE is given as follows:
figure;
xlabel("x")
ylabel("y")
xlim([-2*pi 2*pi])
xticks(-2*pi:pi/2:2*pi)
mylabels={'$-2\pi$','$-\frac{3\pi}{2}$','$-\pi$','$-\frac{\pi}{2}$','0~~~~','$\frac{\pi}{2}$','$\pi$','$\frac{3\pi}{2}$','$2\pi$'};
xticklabels(mylabels)
ax=gca;
ax.TickLabelInterpreter='Latex';
Thank you very much in advance!

采纳的回答

Stephen23
Stephen23 2025-2-16
编辑:Stephen23 2025-2-16
Without even reading the XTICKLABELS documentation lets first do some basic reasoning:
  1. is a single character vector. XTICKLABELS has no way to distinguish between what the user incorrectly thinks are different labels. There is no reason to expect this to work.
  2. is a cell array of string scalars. In other words, you are placing lots of scalar container arrays inside another container array, which as the documentation clearly states has none of the benefits of string arrays and is unlikely to work with most MATLAB functions. There is no reason to expect this to work.
  3. a string array should work exactly as expected.
Now lets read the XTICKLABELS documentation. It clearly states the the labels input must be a "cell array of character vectors | string array | categorical array". So according to the XTICKLABELS documentation 1. and 2. are not supported, which exactly matches our reasoning above.
So lets try a string array (which the XTICKLABELS documentation states is supported):
xlabel("x")
ylabel("y")
xlim([-2*pi,2*pi])
xticks(-2*pi:pi/2:2*pi)
mylabels= ["$-2\pi$","$-\frac{3\pi}{2}$","$-\pi$","$-\frac{\pi}{2}$","0~~~~","$\frac{\pi}{2}$","$\pi$","$\frac{3\pi}{2}$","$2\pi$"];
xticklabels(mylabels)
ax=gca;
ax.TickLabelInterpreter='Latex';
We can see that it works exactly as expected.
"I would like to understand what's the problem here."
The problems are most likely:
  • the user does not read the XTICKLABELS documentation,
  • the user does not understand what square brackets do,
  • the user made some mistakes in their implementation of a string array,
  • the user has unrealisic expectations of container arrays generally.
  2 个评论
Stephen23
Stephen23 2025-2-16
编辑:Stephen23 2025-2-16
Note that writing
mylabels = ['$-2\pi$','$-\frac{3\pi}{2}$','$-\pi$','$-\frac{\pi}{2}$','0~~~~','$\frac{\pi}{2}$','$\pi$','$\frac{3\pi}{2}$','$2\pi$'];
is a very common sign of users who misunderstand what square brackets are. Square brackets are not a "list" operator (which MATLAB does not have), they are are a concatenation operator. So your code simply concatenates lots of character vectors into one character vector, thus is exactly equivalent to writing this:
mylabels = '$-2\pi$$-\frac{3\pi}{2}$$-\pi$$-\frac{\pi}{2}$0~~~~$\frac{\pi}{2}$$\pi$$\frac{3\pi}{2}$$2\pi$';
which is one loooooong character vector. Clearly there is no reason to expect one long character vector to display in the same way as lots of separate character vectors (in a cell array or string array).

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by