How to display a percent sign (%) using latex and sprintf

조회 수: 334 (최근 30일)
Ryan McLaughlin
Ryan McLaughlin 2021년 4월 1일
댓글: Ryan McLaughlin 2021년 4월 1일
I am somewhat new to using latex for plot labels in MATLAB, and am having difficulty getting a percent sign to display using the combination of sprintf and latex. According to documentation for sprintf, the command for a percent sign would be this:
sprintf('New percent OS = $%.2f$ %%',percentOS)
However, this gives an error for the variable insertion portion of text,
I have also tried using the latex syntax for a percent sign, which would be:
sprintf('New percent OS = $%.2f$ $\%$',percentOS)
This gave a different error, but nonetheless did not work.
It seems that latex and sprintf having their own unique syntax is causing problems, but I am unsure what a solution would be.

채택된 답변

Steven Lord
Steven Lord 2021년 4월 1일
percentOS = 23.45;
S = sprintf('New percent OS = $%.2f$ %%',percentOS)
S = 'New percent OS = $23.45$ %'
However, this gives an error for the variable insertion portion of text,
What is the full and exact text of the error message you received (all the text displayed in red)? As you can see from the code segment above, it does work at least in this simple case. If you have a more complicated example that throws the error please show it.
I have also tried using the latex syntax for a percent sign, which would be:
S2 = sprintf('New percent OS = $%.2f$ $\%$',percentOS)
Warning: Escaped character '\%' is not valid. See 'doc sprintf' for supported special characters.
S2 = 'New percent OS = $23.45$ $'
This gave a different error, but nonetheless did not work.
So as you can see this issues a warning but does not throw an error. Ah, I think I might know what's going on. Is the following the warning/error that you are asking about?
text(0.5, 0.5, S, 'Interpreter', 'LaTeX')
Warning: Error updating Text.

String scalar or character vector must have valid interpreter syntax:
New percent OS = $23.45$
If so:
figure
percentOS = 23.45;
S3 = sprintf('New percent OS = $%.2f$ \\%%',percentOS)
S3 = 'New percent OS = $23.45$ \%'
text(0.5, 0.5, S3, 'Interpreter', 'LaTeX')
The \\ in the format used to create S3 transforms into a \ in S3 itself just like the %% puts a % in the char array. The LaTeX interpreter interprets the \% as a percent sign. You could achieve the same result with string operations on S if you can't change the format.
S4 = replace(S, '%', '\%')
S4 = 'New percent OS = $23.45$ \%'
isequal(S3, S4)
ans = logical
1
  댓글 수: 1
Ryan McLaughlin
Ryan McLaughlin 2021년 4월 1일
Yes, I should have originally included the text command as a part of my issue, but I was not aware this could be a part of the problem. Thanks for the help!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

David Hill
David Hill 2021년 4월 1일
sprintf('New percent OS = %s%.2f','%',percentOS);

Meg Noah
Meg Noah 2021년 4월 1일
% not using latex
loveMetric = 99.99;
fprintf(2,'I love matlab: %0.8f %%\n', loveMetric);

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by