CDATA in <script> rendering problem

625 views
Skip to first unread message

Xi Wang

unread,
Apr 19, 2012, 9:23:12 PM4/19/12
to MathJax Users
To make its output XHTML compatible, kramdown (a popular markdown
engine) uses CDATA to enclose latex, as follows:

<script type="math/tex; mode=display"><![CDATA[ ... ]]></script>

The change can be found at:

https://github.com/gettalong/kramdown/commit/512b00a6d050f506c43b824861f7bbf459862a19

The problem is that MathJax now displays the enclosing `<![CDATA[' and
']]>'.

Is there any way to make MathJax ignore them? Thanks.

Davide P. Cervone

unread,
Apr 20, 2012, 5:10:58 PM4/20/12
to mathja...@googlegroups.com
The fact that you are seeing the CDATA comment suggests that you are
not viewing XHTML but HTML. Do they insert this comment in HTML
documents as well? That seems ill-advised. But if that is the case,
then you can use

<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
MathJax.InputJax.TeX.prefilterHooks.Add(function (data) {
data.math = data.math.replace(/^\s*<!\[CDATA\[\s*(.*)\s*\]\]>
\s*$/m,"$1");
});
});
</script>

before the script that loads MathJax.js in order to filter out the
comments.

Davide

thomas....@gmail.com

unread,
Apr 21, 2012, 2:25:55 AM4/21/12
to mathja...@googlegroups.com
On Friday, April 20, 2012 11:10:58 PM UTC+2, Davide Cervone wrote:
The fact that you are seeing the CDATA comment suggests that you are  
not viewing XHTML but HTML.  Do they insert this comment in HTML  
documents as well?  That seems ill-advised.  But if that is the case,  
then you can use

     <script type="text/x-mathjax-config">
     MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
       MathJax.InputJax.TeX.prefilterHooks.Add(function (data) {
         data.math = data.math.replace(/^\s*<!\[CDATA\[\s*(.*)\s*\]\]>
\s*$/m,"$1");
       });
     });
     </script>

before the script that loads MathJax.js in order to filter out the  
comments.


If have tried the CDATA sections in an XHTML document, however, they also showed up in the output...

By looking through the source code of MathJax I found that CDATA sections inside \[...\] and \(...\) are automatically removed. Why is this not done for script tags too? There seems to be no reason not to do this...

Is there any way to create an XHTML and HTML compatible MathJax document that uses script tags?

Davide P. Cervone

unread,
Apr 21, 2012, 3:54:31 PM4/21/12
to mathja...@googlegroups.com
> If have tried the CDATA sections in an XHTML document, however, they
> also showed up in the output...

Then I think the file is being handled as HTML rather than XHTML. In
XHTML the DATA delimiters will be removed by the browser (leaving the
data they contain), and will not be available to MathJax, so will not
appear in the output. If you are seeing the CDATA in the output, then
that suggests that it is not being handled as XHTML. You have to make
sure your server is sending it with the proper MIME-type, or if you
are loading from a local file, that it is properly interpreted as XHTML.

Can you point to a URL with the expected behavior? Or send a test
file that you are using?

> By looking through the source code of MathJax I found that CDATA
> sections inside \[...\] and \(...\) are automatically removed.

Note that in HTML documents, this is not a standard CDATA segment, but
an actual comment (note that it is for <!--[CDATA[...]]--> not <!
[CDATA[...]]>). MathJax uses this "fake" CDATA comment as a means of
allowing people to avoid some problems with <, >, and & that would
otherwise be embedded in the body of the document where they would
have to be escaped. Since these are not real CDATA (and since HTML
doesn't handle CDATA comments anyway), MathJax is forced to handle
them itself.

> Why is this not done for script tags too?

Because in HTML, <script> tags are automatically CDATA, so it is not
needed there, and in XHTML, the browser handles CDATA itself, so
MathJax doesn't have to.

> There seems to be no reason not to do this...

The reason is that is is not needed. If you want to include unneeded
CDATA in your HTML script tags, then you can use the code I gave you
in my previous answer to handle that.

> Is there any way to create an XHTML and HTML compatible MathJax
> document that uses script tags?

XHTML and HTML are different formats with different requirements
(though they are very similar). The same file will not work for both,
in general. But if you want to put CDATA comments in your HTML
scripts, use the code from my previous message.

Davide

thomas....@gmail.com

unread,
Apr 22, 2012, 3:01:20 AM4/22/12
to mathja...@googlegroups.com
On Saturday, April 21, 2012 9:54:31 PM UTC+2, Davide Cervone wrote:
<SNIP />

> Is there any way to create an XHTML and HTML compatible MathJax  
> document that uses script tags?

XHTML and HTML are different formats with different requirements  
(though they are very similar).  The same file will not work for both,  
in general.  But if you want to put CDATA comments in your HTML  
scripts, use the code from my previous message.


Many thanks for your detailed explanations! The problem with the XHTML file was really that the wrong mime type was sent, when using the correct mime type, everything worked fine - thanks!

I have thought a bit more about this problem and came up with a solution that works with XHTML and HTML, and uses the script tag. Since using XHTML and a CDATA section works without problems, we have to make sure that the CDATA section is handled in some way by the MathJax parser. One could either use your solution (thanks again!) or one can just make use of the syntax inside a MathJax script tag. Since everything in the script tag is processed as LaTeX code, one can comment out the code concerning CDATA.

For example, the following works fine in HTML and XHTML documents (tested in Firefox):

<script type="math/tex">%<![CDATA[
\begin{align*}
< &=5 \\
&=6 \\
\end{align*} %]]></script>

I will adjust the output of kramdown (I'm the author) so that the above syntax is produced which will resolve the problem without needing additional steps by users.

Thomas

Davide P. Cervone

unread,
Apr 22, 2012, 9:08:11 AM4/22/12
to mathja...@googlegroups.com
Nice solution. I had been thinking about //<![CDATA[...//]]->, but
knew that the javascript comments weren't the thing to use, but I
didn't take the next step of converting to TeX comments. That seems
obvious now that you have pointed it out. I'm glad you shared your
result.

Davide


On Apr 22, 2012, at 3:01 AM, <thomas....@gmail.com> <thomas....@gmail.com

Reply all
Reply to author
Forward
0 new messages