text with underscores inside math

763 views
Skip to first unread message

Tim Arnold

unread,
Sep 27, 2013, 10:29:06 AM9/27/13
to mathja...@googlegroups.com
hi,
Is there a way to code this expression for MathJax?
\[
\text{variable\_one} = x + y
\]

The \_ shows the backslash since commands inside the text argument in MathJax are not expanded, and for good reason, at least as I understand it.

so is this the only solution? Can I do it any simpler?
\[
\text{variable}\_\text{one} = x + y
\]

thanks,
--Tim

Frédéric WANG

unread,
Sep 27, 2013, 10:35:30 AM9/27/13
to mathja...@googlegroups.com
What about

\[
\text{variable_one} = x + y
\]

?
--
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/groups/opt_out.

Tim Arnold

unread,
Sep 27, 2013, 10:49:34 AM9/27/13
to mathja...@googlegroups.com
that would be great if I only had to please Mathjax, but the same source is processed by LaTeX, which will raise an error on the unescaped underscore.

William F Hammond

unread,
Sep 27, 2013, 8:17:37 PM9/27/13
to mathja...@googlegroups.com
I think I'm finding some inconsistency in how special ascii characters are handled inside \text{}.
What are the rules?  (That aside, I would not use one of the 33 non-alphanumeric but printable ascii characters as part of the name of a math variable.)
          -- Bill

Peter Krautzberger

unread,
Feb 12, 2014, 5:26:40 PM2/12/14
to mathja...@googlegroups.com
What are the rules?

The rule is: only $ is special. 

I guess it is slightly inconsistent as it might make more sense to tie \$ to the configuration processEscapes: true.You can file a feature request for other characters or (even better) write a small extension that would be contributed back.

Best wishes,
Peter.

Davide P. Cervone

unread,
Feb 13, 2014, 1:57:50 PM2/13/14
to mathja...@googlegroups.com
The rule is: only $ is special. 

The reason for this is because $ is used as a math delimiter within the \text, so that you can do things like

\text{for all $x < 0$}

as you can in LaTeX.  In order to make it possible to obtain a dollar sign within \text{}, we have to handle \$ in text mode.  That is the only one.

I guess it is slightly inconsistent as it might make more sense to tie \$ to the configuration processEscapes: true.

Note that processEscapes is a parameter to the tex2jax preprocessor, while the \$ in \text{} are handled by the TeX input jax, so these have no relation to each other.  Indeed, the TeX processor can run even without the tex2jax preprocessor, so it would be a mistake to tie its processing to the tex2jax configuration.

The reason tex2jax needs a parameter to control processing of \$ is because dollar-sign delimiters are off by default, and so there is no need to processes \$ specially.  It is only when dollar-sign delimiters are enabled that \$ becomes important.  And note that this is only there to help identify what constitutes the math expressions within the page (not how to interpret the TeX code within those delimiters).  The HTML page is not a TeX page, so MathJax needs help to determine how you want to handle the text outside of the math; but the contents of the math should be controlled by TeX's rules.

That being said, you could use something like

<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  var PARSE = MathJax.InputJax.TeX.Parse,
      TEXT = PARSE.prototype.InternalText;
  PARSE.Augment({
    InternalText: function (text,def) {
      text = text.replace(/\\/g,"");
      return TEXT.call(this,text,def);
    }
  });
});
</script>

to remove any backslashes from the contents of \text{} that aren't part of math-mode material within the \text{}.  Note that this should appear BEFORE the script that loads MathJax.js.

You could be more selective about the removal by using

text = text.replace(/\\([_%#])/g,"$1");

instead (add whatever characters you want to quote).

Davide

Reply all
Reply to author
Forward
0 new messages