Preprocess TeX to replace '–' with '-'?

80 views
Skip to first unread message

Christian Perfect

unread,
Nov 20, 2014, 6:24:07 AM11/20/14
to mathja...@googlegroups.com
This is a hack solution to a problem I'm having: wordpress replaces instances of - (the minus sign) in my text with the character –, or unicode character #8211, the en-dash. At the moment, wordpress doesn't let you customise which characters its wptexturize function replaces - you either get all of its "fixes" or none.

I want to keep making my quotation marks pretty and so on, and persuading WordPress to pay attention to TeX delimiters isn't an option, and the en-dash doesn't have a meaning in TeX at the moment, I'd like to get MathJax to preprocess TeX to replace – with -.

I'm sure I already know the answer to do this, but my brain isn't running quickly today: how do I do it?

Peter Krautzberger

unread,
Nov 20, 2014, 6:28:27 AM11/20/14
to mathja...@googlegroups.com
Here you go http://jsbin.com/veliwecimi/1/edit


  <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(/–/g, '-'); 
          }); 
        }); 
  </script> 

Peter.

--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Perfect

unread,
Nov 20, 2014, 6:39:00 AM11/20/14
to mathja...@googlegroups.com
Thanks! I knew there was a mechanism for prefilters but I couldn't find it in the docs.

Peter Krautzberger

unread,
Nov 20, 2014, 6:46:01 AM11/20/14
to mathja...@googlegroups.com
> but I couldn't find it in the docs.

yeah, we need to figure out how to organize these kinds of things. "When we have time"(tm)...

Murray

unread,
Nov 21, 2014, 3:27:20 AM11/21/14
to mathja...@googlegroups.com
Christian

The MathJax prefilter is one way, but FYI, it is possible to customize the wptexturize process. If you get stuck on other Wordpress gremlins, it's easy to create a Wordpress plugin:

This works (it comes from here, corrected):

<?php
/*
Plugin Name: Un-en-dash the Double Dash
*/
add_filter( 'the_content' , 'mh_un_en_dash' , 50 );
add_filter( 'the_title', 'mh_un_en_dash' , 50 );
add_filter( 'the_excerpt', 'mh_un_en_dash' , 50 );
add_filter( 'comment_text', 'mh_un_en_dash' , 50 );
add_filter( 'list_cats', 'mh_un_en_dash' , 50 );

function mh_un_en_dash( $text ) {
        $content = str_replace( '&#8211;' , '-' , $text );
        return $content;
}
?>

Save it as a PHP file and upload to the "plugins" folder. Then enable it.

The other way around the problem is to try putting your LaTeX in a "pre" tag (Wordpress doesn't chew anything in the following tags):

<pre><code><kbd><style><script>and <tt>

This is what I had to do on this page, otherwise Wordpress mangled the JSXGraph code:


Regards
Murray 

Christian Perfect

unread,
Nov 21, 2014, 8:00:52 AM11/21/14
to mathja...@googlegroups.com
I considered making a plugin to do what you want, but I'd prefer to keep the en-dash in normal text. Using a MathJax prefilter to only do the replacement on TeX feels like the best way to me.
Reply all
Reply to author
Forward
0 new messages