LaTeX interpreter with multiple strings or character vectors

40 ビュー (過去 30 日間)
Ying Ying
Ying Ying 2025 年 2 月 16 日 5:39
コメント済み: Ying Ying 2025 年 2 月 16 日 9:21
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 日 7:54
編集済み: Stephen23 2025 年 2 月 16 日 8: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 日 8:03
編集済み: Stephen23 2025 年 2 月 16 日 8:09
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).
Ying Ying
Ying Ying 2025 年 2 月 16 日 9:21
My bad. Thanks for the answer.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLaTeX についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by