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
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