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

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.

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?

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 latex title doesn't work for powers (^)

In MatLab (R2015a), I want to style the title of my plots using latex.
This is working fine for some functions, but not if there's a power in the equation.
The below code works, and shows a formatted title to the right, and an unformatted title to the left.
It shows the warning:
Warning: Error updating Text.
String must have valid interpreter syntax: y = x^2
syms x y
eq = y == x^2;
subplot(1,2,1)
ezplot(eq)
title(latex(eq),'interpreter','latex')
eq = y == x+2;
subplot(1,2,2)
ezplot(eq)
title(latex(eq),'interpreter','latex')
EDIT:
I just found out I can get it to work by appending $ on both sides. But it seems weird that I would have to do this.
So this works:
title(strcat('$',latex(eq),'$'),'interpreter','latex')
Solution
The problem can be solved easily by adding $-signs before and after the generated LaTeX-expression. So you can change your «title-lines» to:
title(['$',latex(eq),'$'],'interpreter','latex')
An alternative is to use strcat as proposed in your question.
Explanation
Since you basically answered the question yourself already, I'm going to explain why it happened. Hopefully after reading this, it's no longer 'weird' behaviour. If you choose to use the LaTeX-interpreter in Matlab, you really get a LaTeX-interpreter. This means that the provided string must be valid LaTeX-syntax.
Using ^ outside a math-environment is considered invalid syntax because it is a reserved character in LaTeX. Some interpreters automatically add the $ before and after in this case, but throw a warning at the same time.
The output of the latex-function in Matlab is provided without the $-signs. This way you can combine outputs and concatenate if needed without creating a mess with $-signs.
To change to the math-environment in LaTeX you can use the already mentioned shortcut $...$. An alternative way is to use \begin{math} your_equation \end{math}. It produces the same result for your equations and can be used here for demonstration purposes. The following line would do the same job, but is a bit longer to write:
title(['\begin{math}',latex(eq),'\end{math}'],'interpreter','latex')
Now, the reason why only one of your equations is displayed correctly, lies in the invalid character ^ in y = x^2. Matlab then chooses interpreter none and therefore displays the string unformatted. The +-sign in y = x + 2 is valid outside a math-environment, so it gets displayed correctly (but is not interpreted in a math-environment).

Angstrom symbol in 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.

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$')