MatLab latex title doesn't work for powers (^) -


in matlab (r2015a), want style title of plots using latex.

this working fine functions, not if there's power in equation.

the below code works, , shows formatted title right, , unformatted title left.

it shows 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 found out can work appending $ on both sides. seems weird have this.

so works:

title(strcat('$',latex(eq),'$'),'interpreter','latex') 

solution

the problem can solved adding $-signs before , after generated latex-expression. can change «title-lines» to:

title(['$',latex(eq),'$'],'interpreter','latex') 

an alternative use strcat proposed in question.


explanation

since answered question already, i'm going explain why happened. after reading this, it's no longer 'weird' behaviour. if choose use latex-interpreter in matlab, latex-interpreter. means provided string must valid latex-syntax.

using ^ outside math-environment considered invalid syntax because reserved character in latex. interpreters automatically add $ before , after in case, throw warning @ same time.

the output of latex-function in matlab provided without $-signs. way can combine outputs , concatenate if needed without creating mess $-signs.

to change math-environment in latex can use mentioned shortcut $...$. alternative way use \begin{math} your_equation \end{math}. produces same result equations , can used here demonstration purposes. following line same job, bit longer write:

title(['\begin{math}',latex(eq),'\end{math}'],'interpreter','latex') 

now, reason why 1 of equations displayed correctly, lies in invalid character ^ in y = x^2. matlab chooses interpreter none , therefore displays string unformatted. +-sign in y = x + 2 valid outside math-environment, gets displayed correctly (but not interpreted in math-environment).


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -