Angstrom symbol in Matlab - matlab

I would like to write q=0.1 Å inside a textbox in a plot that I have made using code. I am using Matlab. I have written the following:
str={'q=0.1$\AA$'};
annotation('textbox',...
[0.45 0.8 0.2 0.1],...
'interpreter','latex','string',str,...
'fontsize',20,...
'fontname','times new roman',...
'edgecolor','none',...
'fitboxtotext','on');
which yields:
The problem is that the angstrom symbol it is producing is a bit weird. The circle at the top of the A is very off. I have tried other options for 'fontname', but the result is the same. Is there a way to get a proper angstrom symbol in Matlab?

This is more a LaTeX problem than a MATLAB problem. If you print $\AA$ in a LaTeX document, it will look the same.
A workaround would be to remove the $...$, as you don't need a math environment for \AA:
str={'q=0.1\AA'};
annotation('textbox',...
[0.45 0.8 0.2 0.1],...
'interpreter','latex','string',str,...
'fontsize',20,...
'fontname','times new roman',...
'edgecolor','none',...
'fitboxtotext','on');
In my opinion, the result looks much better:

You should remove the $ around the \AA:
str = 'q=0.1\AA';
annotation('textbox', [0.45 0.8 0.2 0.1], 'interpreter','latex','string',str);
produce a straight angstrom sign, while '$\AA$' produce what you have.

I think the main problem is that in LaTeX math mode (between the '$' characters), most text defaults to italics. You can remedy this by changing your first line to use \textrm:
str={'q=0.1$\textrm{\AA}$'};
The output annotation string now looks like:
\rm will also work, but may be deprecated. \mathrm also appears to work in this case, but apparently can do more complicated font sunbstitutions. I would actually make the full string math in order to italicize 'q' and get proper spacing around the '=' sign:
str={'$q=0.1\textrm{\AA}$'};
See the difference:
Though, as #hbaderts points out, str={'$q=0.1$\AA'}; would work too, but may yield slightly different kerning.

Related

How can I write with latex character in xlabel in matlab

I would write my xlabel with Latec character so I used this code
x = -10:0.1:10;
y = [sin(x); cos(x)];
plot(x,y)
xlabel('$\mathbb{x}$','Interpreter','latex')
but I have this warning message
Warning: Error updating Text.
String scalar or character vector must have valid interpreter syntax:
$\mathbb{x}$
and the xlabel appear like this
https://i.stack.imgur.com/NCI4n.png
please how I can fix this problem.
The problem with your example is the mathbb command, which is not in base Latex. To show this, replace the \mathbb with, e.g., \mathrm. This will work.
I've never seen an example of adding Latex packages into the Matlab environment. (Update below)
You can have a look at this question, which includes a very aggressive potential way to add the package you need. But it's not clear to me if the solution proposed is actually functional.
How do you use the LaTeX blackboard font in MATLAB?

Endash for minus sign instead of hyphen for matlab

So here's my code:
set(groot, 'defaultAxesTickLabelInterpreter', 'latex') %For axes;
ax = gca;
yticklabels(ax, strrep(yticklabels(ax),'--','–'));
set(ax,'ticklabelinterpreter','tex') %or 'tex' but not 'latex'
figure(1)
t= [0:0.01:2*pi];
x = sin(t);
y = cos(t)
plot(t, x, t, y)
Output:
I tried the solution here, but the hyphens still remain there. I want the en-dash to appear because it's the standard sign for the negative sign. What is the correct way of getting an en-dash to appear instead of the hyphen?
This post at MATLAB Answers explains how to set the (default) interpreter for the axes' labels.
set(groot,'defaultAxesTickLabelInterpreter','latex');
You need to call this before plotting.
Having this set, the tick-labels will be interpreted as LaTeX code. Here is a comparison. The last two examples includes #XiangruiLi's answer (the next code snippets must be called after the plot was created):
yticklabels(gca, strrep(yticklabels(gca),'-','--'));
yticklabels(gca, strrep(yticklabels(gca),'-','$-$'));
none:
latex:
latex + strrep(...,'-','--')):
latex + strrep(...,'-','$-$')):
While the last is probably what you wanted, note that this is certainly not the representation MATLAB intended. It is therefore the question if you really need/want to go through this fuzz.
It seems to me you mis-used strrep. This worked for me:
yticklabels(ax, strrep(yticklabels(ax),'-','--'));
Using the actual Unicode minus character should also work (also in Octave):
yticklabels(gca, strrep(yticklabels(gca),'-','−'));
In this case, there is no need to set the interpreter to LaTeX.

Why using _LatexSmallFractionConstant:=1 causes bad latex to be generated?

The option _LatexSmallFractionConstant:=1 is very useful in Maple, as it makes the fraction generated in latex looks natural. Here is an example
restart;
latex(1/2);
1/2
Now using _LatexSmallFractionConstant:=1, the output becomes
restart;
_LatexSmallFractionConstant:=1:
latex(1/2);
{\frac{1}{2}}
Which is the correct Latex.
But _LatexSmallFractionConstant:=1 causes big problem in other places, like here
mu:=1/((4*t+1)^(8/5)*(t-1)^(7/5));
Now see what happens when latex is generated with _LatexSmallFractionConstant:=1 set
_LatexSmallFractionConstant:=1:
latex(simplify(mu))
{1 \left( 4\,t+1 \right) ^{-{\frac{8}{5}}} \left( t-1 \right) ^{-{
\frac{7}{5}}}}
Which is completely wrong. It renders as
Removing _LatexSmallFractionConstant:=1: gives
restart;
mu:=1/((4*t+1)^(8/5)*(t-1)^(7/5));
latex(simplify(mu))
{\frac {1}{ \left( 4\,t+1 \right) ^{8/5} \left( t-1 \right) ^{7/5}}}
Which renders correctly.
Is there a way to correct the above, so I can still use _LatexSmallFractionConstant:=1: ? I need to use this option to make fractions appear in correct Latex, but I also do not want to obtain bad Latex in other places like the above.
Is there a solution to this problem?
Maple 2018.1 on windows.
It's a bug. You should report it. I'd link to where, but at the moment, it looks like both www.maplesoft.com and www.mapleprimes.com are down.

How to write an overbar and subscript infinity in the same xlabel on a Matlab figure

I want the xlabel of my Matlab figure to read v / Uinf, where the v has an overbar, and the inf is a symbol in subscript.
The line:
xlabel('$\bar{v}$','interpreter','latex')
creates the v overbar, and:
xlabel('U_\infty')
creates the U subscript infinity, but when I try to put them together, Matlab says 'String must have valid interpreter syntax'. It seems setting the interpreter to latex means the U_\infty command doesn't work any more.
Is there a way of writing U_infty that is compatible with latex or another way of writing the two together?
Thanks in advance,
Holly
In LaTeX, U_\infty works only in math mode, so you have to write $U_\infty$ instead.

matlab's phi symbol

Not that significant, but annoying to no end. Why does matlab has no small phi (\varphi) symbol ? It has pretty much all other symbols LaTeX offers, but not this one. Why ?
I may be wrong of course, in which case would be delighted if someone could prove me wrong...
The default interpreter is TeX actually, not LaTeX, which is why you're having this problem. You can use LaTeX as the interpreter for a given part doing something like this:
plot(1);
hl = legend('$$\varphi$$');
set(hl,'Interpreter','latex')
or you can set LaTeX as your default interpretor using
set(0,'DefaultTextInterpreter', 'latex');
which can be put in your startup.m file if you like.
Matlab uses TeX as default. Often, it is possible to switch to LaTeX, but in some cases (dialog boxes), this is impossible.
%# here's an example with all three phis
plot(rand(3))
yh = get(gca,'YLabel');
set(yh,'Interpreter','latex','string','$\varphi$ $\phi$ $\Phi$')