Question

I've got LaTex code from server and have no control over it. I would like to print the LaTex code as it is using JavaScript, but it interprets as control characters on assigning to variable.

var str = '\[\frac{{{d^2}y}}{{d{x^2}}} - \,3\,\left( {\frac{{dy}}{{dx}}} \right)\, + \,2y\, = \,0\]';

This interprets as follows

[rac{{{d^2}y}}{{d{x^2}}} - ,3,left( {rac{{dy}}{{dx}}} ight), + ,2y, = ,0]

And I want as follows:

\[\frac{{{d^2}y}}{{d{x^2}}} - \,3\,\left( {\frac{{dy}}{{dx}}} \right)\, + \,2y\, = \,0\]

I tried varies RegExps, and result of

var index = str.indexOf("\\")

is -1

JavaScript removes control characters like \f, \r and so on and there are thousands of backslash notations in TeX.

Was it helpful?

Solution

How is loaded in str var?

Anyway, there is a work around (working here):

var c=',[]flr';
for(var i=0;i<c.length;i++)
 eval("str=str.replace(/\\"+c.charAt(i)+"/g,'\\\\"+c.charAt(i)+"');");

What we do is for any char in c evaluate the following code (replacing X with the char):

str=str.replace(/\X/g,'\\X');

Anyway, there is a work around (working here):

var c=',[]flr';
for(var i=0;i<c.length;i++)
 eval("str=str.replace(/\\"+c.charAt(i)+"/g,'\\\\"+c.charAt(i)+"');");

What we do is for any char in c evaluate the following code (replacing X with the char):

str=str.replace(/\X/g,'\\X');

Ok, that's an odd (and maybe dangerous) bug at the site which delivers the JSON. If it does not turns the \' to \', maybe it does not turns the ' to \'. So, anything after a ' or a " (depending on how the string is quoted) will be executed on any site that includes that (i.e. your site). Maybe as JSON is a little more secure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top