Changing single backslashes to double ones

2,333 views
Skip to first unread message

Bruce Sherwood

unread,
Jan 4, 2012, 3:41:17 PM1/4/12
to mathja...@googlegroups.com
I'm new to MathJax, and I'm very impressed. In my own uses I will nearly always be creating LaTeX character strings in JavaScript, to be rendered by MathJax after adding them to the Document. I realize that in this situation all backslashes need to be doubled (escaped). Is there a way in JavaScript to convert a string containing single backslashes into a string with double ones? I couldn't think of a way to do this other than manual editing of the string. Thanks for any suggestions.

Davide P. Cervone

unread,
Jan 6, 2012, 8:46:46 AM1/6/12
to mathja...@googlegroups.com
Double backslashes are needed in a string literal (i.e., a string
contained in quotation marks) because the backslash is the JavaScript
escape character that is used to obtain character that are not
otherwise available, like a newline (\n), a tab (\t), or a quotation
mark (\"). Because of this, you need to "quote" the backslash if you
want a literal backslash (\\), since otherwise it would combine with
the following character to determine one of these special characters.

But that is only needed in a string literal. If you already HAVE a
string that contains backslashes (stored in a javascript variable),
there is no need to double them. The doubling is only to get the
backslashes into the string in the first place. So I'm confused by
the request.

For example, if you uses

var cs = "\newcommand{\t}{{\bf t}}";

then cs would become a string starting with a newline, then the
characters "ewcommand{" then a tab, then "}{{" then a backspace, then
"f t}}". Note that there are no slashes in the string. Javascript
used those backslashes to represent special characters within the
string. The doubling of backslashes isn't something that you will be
able to do from within the javascript itself; they have to be there
BEFORE javascript parses the string literal.

If you really want to double the backslashes that are in a string
stored in a variable, then you can use

cs = cs.replace(/\\/g, "\\\\");

which does a search and replace on the contents of cs, searching of a
single slash (which we had to double in order to get an actual slash
in the pattern) and replaces it by a double slash (which we had to
represent as two pairs of slashes, each becoming a single slash in the
replacement string). But I don't think this is what you really want.

Davide

Bruce Sherwood

unread,
Jan 6, 2012, 11:52:53 AM1/6/12
to mathja...@googlegroups.com
Sorry, I guess I wasn't clear. The situation is this:

1) I have some LaTeX expression(s) existing in a LaTeX file. They of course contain things like "\dfrac", with single backslashes.

2) I would like to copy and paste such an expression into a JavaScript program, just by putting quote marks around the existing expression, but that won't work: I need to go through the string by hand and put an additional backslash in front of each existing backslash. 

3) I was hoping I could write a JavaScript function "fixup" that would accept a standard LaTeX expression and return a string containing doubled backslashes. However, I haven't found any way to do this, because there seems to be no way in JavaScript to distinguish between \d and d. In Python there is a way to deal with "raw" strings where "\d" can be read as "\" followed by "d". But there seems to be nothing like this in JavaScript.

I hope I've stated the problem sufficiently clearly this time that someone can tell me some secret for writing a fixup routine. Thanks.

Bruce Sherwood

Davide P. Cervone

unread,
Jan 6, 2012, 3:00:17 PM1/6/12
to mathja...@googlegroups.com
I do not think you will be able to do what you request. Javascript
doesn't have a string literal form where the backslash is not an
escape character (at least not that I know of). So if you put a LaTeX
expression into quotes in a Javascript program, you will run into the
problem you are having. The backslashes will be treated as escape
characters, and the conversion is not reversible. As you point out,
"\d" and "d" both produce the same one-character string, and there is
no way for the javascript program to know which it came from.

The only thing I can think of is not to store the LaTeX expressions as
javascript strings. For example, you could put the expressions in
<script type="text/x-data" id="math1">...</script> tags, and give each
script an ID so you could look it up, and then use
document.getElementById("math1").innerHTML to obtain the string that
is contained in the script. This is very ugly and cumbersome, but it
is one way to get a LaTeX string into a javascript variable without
doubling all the backslashes.

Davide

Comer Duncan

unread,
Jan 6, 2012, 3:09:33 PM1/6/12
to mathja...@googlegroups.com
Maybe I am not understanding what is needed, but from what I can glean from the description, I wonder whether it might be a reasonable idea to write a python script which takes the latex document and produces a new document with the substitutions done. Then use the reformatted document in MathJax.

Comer

Davide P. Cervone

unread,
Jan 6, 2012, 3:13:05 PM1/6/12
to mathja...@googlegroups.com
I almost suggested that as well, but since he said he wanted to cut-and-paste the expressions into the javascript, I assumed this was not the path he wanted to take.  That seemed the obvious approach to me as well, however.

Davide

Bruce Sherwood

unread,
Jan 6, 2012, 4:04:03 PM1/6/12
to mathja...@googlegroups.com
Indeed, as I mentioned, Python does have the concept of a raw string, so I could use Python. But I'm developing a JavasScript+WebGL programming environment at glowscript.org which makes it easy for novice programmers to create real-time navigable 3D animations, and these users may not be Python users; see


The role of MathJax is the wonderful ability to add real math displays to web pages that show 3D animations.

A colleague has pointed out that the easiest scheme may simply be search-and-replace in the JavaScript program editor.

Thanks for the help, even though it only confirmed my impression that maybe there wasn't any way to do this. Seems like there's a missing feature in JavaScript: raw strings.

Bruce Sherwood

mhorn...@gmail.com

unread,
Apr 15, 2019, 5:00:41 PM4/15/19
to MathJax Users
try String.raw`\dtest`
Reply all
Reply to author
Forward
0 new messages