operatorname needs braces as subscript?

313 views
Skip to first unread message

David Farmer

unread,
Oct 18, 2014, 8:49:49 PM10/18/14
to mathja...@googlegroups.com
Using the AMSmath extension, the following gives a "missing opening brace for subscript" error:

\(
\newcommand{\df}{\operatorname{df}}

P_\df
\)

I wouldn't have written in that way, but I am trying to make html versions of the LaTeX
written by someone else, and they have many papers with a similar construction.

I experimented a bit and found the same problem when using DeclareMathOperator.

If I wrap "\operatorname{df}" in a extra set of curly brackets, then it works.
But I couldn't get anything similar to work when using DeclareMathOperator.

Also, \(P_\sin\) seems to give the same error.

Here is my whole file:

<!DOCTYPE html>
<html>
<head>
<script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        extensions: ["tex2jax.js","TeX/AMSmath.js"],
        jax: ["input/TeX", "output/HTML-CSS"],
      });
</script>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js">
</script>
</head>
<body>

\(
\newcommand{\df}{\operatorname{df}}
P_\df
\)

\(
P_\sin
\)

\(
P_xyz
\)
</body>


Davide P. Cervone

unread,
Oct 19, 2014, 7:05:42 AM10/19/14
to mathja...@googlegroups.com
I'm afraid that this is how TeX works.  The code that you give below doesn't run in LaTeX either, and produces a similar error (it says that a missing brace has been inserted, but that is because it is trying to recover from the error, though in this case it will just cause a missing code brace later on).

What's happening is that the underscore takes the first token that follows it as the subscript (unless it is a brace, in which case it is the complete braced group).  The caveat is that this is AFTER macro expansion.  So 

P_\df

becomes

P_\operatorname{df}

which is treated like

{P_\operatorname}{df}

When the \operatorname is processed, it appears to not have its parameter.  LaTeX works in the same way.

Similarly for \sin, which (in TeX) is a macro that produces \mathop{\rm sin}\nolimits (it is a little more complicated in LaTeX, but the point is, it is a multi-token result, and only the first token gets applied to the subscript).

As for \DeclareMathOperator, that produces a macro that contains essentially \operatorname{df}, and so it works the same way as above.  I do not know of a way to cause that to add braces to the definition.

Your final example of 

P_xyz

is interpreted as

P_{x}yz

by both LaTeX and MathJax.  So all of this is by design, and consistent with LaTeX.

The correct form for each of these, in both MathJax and LaTeX is

P_{\df}
P_{\sin}
P_{xyz}

It would be possible to configure MathJax to add a TeX input preprocessor that replaces P_\df with P_{\df} if you didn't want to produce correct TeX initially, but that seems like a hack.

Davide




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

David Farmer

unread,
Oct 19, 2014, 7:42:29 AM10/19/14
to mathja...@googlegroups.com, dp...@union.edu

Now I am more confused, because if I go to

http://mathjax.org/mathjax/test/sample-dynamic.html 

and enter P_\sin, it puts "sin" in the subscript without complaining.
That is why I included that example.  I put the P_xyz just to see that simple subscripts were working normally,
which they were.

Since other people are writing the LaTeX and I have to process it with a script, maybe the script should look if a backslash
immediately follows the underscore, and then wrap it in curly brackets before handing it to MathJax?
Maybe as hacks go, that isn't too bad?

jjto...@gmail.com

unread,
Oct 20, 2014, 4:38:49 AM10/20/14
to mathja...@googlegroups.com, dp...@union.edu
As a rule of thumb, unless you know what you are doing, super- and subscripts should be surrounded by curly braces. 

In your case, a quick fix is to define \df as

\newcommand{\df}{{\operatorname{df}}}

In this way P_\df expands to

P_{\operatorname{df}}

as required. Anyway, since \df is not properly used here as an operator, I would define it by 

\newcommand{\df}{{\text{df}}}

Concerning your test in http://mathjax.org/mathjax/test/sample-dynamic.html , I do not understand why P_\sin works there. It is not valid LaTeX and yields and error when typeset as usual.

Regards 

Juanjo

Peter Krautzberger

unread,
Oct 20, 2014, 4:43:28 AM10/20/14
to mathja...@googlegroups.com, Davide P. Cervone
David Farmer wrote

> Now I am more confused, because if I go to 
> and enter P_\sin, it puts "sin" in the subscript without complaining.

Wow, how did you find that link? I didn't know this still existed and will remove it. It's very very outdated (v1.0.5) 

Please use http://cdn.mathjax.org/mathjax/latest/test/sample-dynamic.html where P_\sin will, in fact, give an error.

Peter.

Davide P. Cervone

unread,
Oct 20, 2014, 6:11:40 AM10/20/14
to mathja...@googlegroups.com
It looks like my response went to David directly, and not to the list. Here is what I said. Note that Peter also points out the outdated version.

>> http://mathjax.org/mathjax/test/sample-dynamic.html
>
> That page is running a really really old version of MathJax (1.0.5). You probably want to use
>
> http://cdn.mathjax.org/mathjax/latest/test/sample-dynamic.html
>
> instead. That older version is left over from before we had the CDN, and probably should be removed.
>
>> Since other people are writing the LaTeX and I have to process it with a script, maybe the script should look if a backslash
>> immediately follows the underscore, and then wrap it in curly brackets before handing it to MathJax?
>> Maybe as hacks go, that isn't too bad?
>
> The problem is that you don't know what else should be included within the braces. For example,
>
> P_\operatorname{df}
>
> should include the argument as well. So it is harder to do what you want in a way that works generically. But if you know the macros you want to be able to use without braces in the subscript, you might be able to work out the regular expression for that. Otherwise, you'd need to know the argument counts for all the macros that could be used in the subscript, and that is more than what is reasonable for your script, I would suspect.
>
> Davide


David Farmer

unread,
Oct 21, 2014, 3:34:26 PM10/21/14
to mathja...@googlegroups.com

Thank you for the useful responses.

I have a pretty good idea of some heuristics for automatically inserting
the curly brackets around the subscript. LaTeX seems to assume everything
following the underscore should be subscript, so
$P_\sqrt{x} + 1234$
puts the + 1234 in the subscript. It seems unlikely that is what the
author intended. I'll check a lot of examples once I have something
working.
> You received this message because you are subscribed to a topic in the Google Groups "MathJax Users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/mathjax-users/FL86cGKluVI/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to mathjax-user...@googlegroups.com.

Davide P. Cervone

unread,
Oct 21, 2014, 4:03:54 PM10/21/14
to mathja...@googlegroups.com
> LaTeX seems to assume everything following the underscore should be subscript,

This is not the actually true. LaTeX takes the first non-space token after the underscore (after expanding macros) as the subscript, unless it is a brace, in which case the subscript is what follows up until the next matching close brace.

If there is an error processing the subscript, as in the examples you have given, TeX may alter the input in order to attempt to recover. In the case of your

$P_\sqrt{x} + 1234$

TeX takes "\sqrt" as the subscript, but when trying to process that, it is missing its argument, and so this produces an error. In particular, the error is

! Missing { inserted.

and it inserts a brace before the \sqrt in an attempt to fix the most probable cause of the error (missing braces around the subscript). So the input is now effectively

$P_{\sqrt{x} + 1234$

This now causes the remainder of the expression to be considered part of the subscript, since the new open brace has no matching close brace. When TeX reaches the end of the expression, it will throw a second error,

! Missing } inserted.

and add a brace at the end, making the input effectively equivalent to

$P_{\sqrt{x} + 1234}$

where the rest of the expression is the subscript. On the other hand, properly formed expressions like

$P_x + 1234$

will make the subscript only the "x", not including the "+ 1234".

So your claim is only true in the case where the subscript produces an error, and only if you ignore the two error messages that TeX generates concerning the incorrect LaTeX that you gave it. If you are running TeX in batch or quiet mode, you will not see the errors, so perhaps that is what is going on in your author's case, but the TeX is malformed, and should never have been accepted in the first place. This does mean you have to deal with how to make it into valid TeX at this point, which is unfortunate. Making the subscript be the rest of the expression is at least consistent with TeX's attempt to recover from the error.

Davide


David Farmer

unread,
Oct 21, 2014, 5:08:15 PM10/21/14
to mathja...@googlegroups.com

Yes, I meant to be describing what happens when there is an error.

My source files are from the ArXiv, which I assumed, apparently
mistakely, rejected uploads that have errors. Is there some package
that suppresses those type of errors? No need to answer that question!

I'll look more closely at the source and I'm sure I have enough
information to proceed now.

William F Hammond

unread,
Oct 21, 2014, 7:19:20 PM10/21/14
to mathja...@googlegroups.com
On Tue, Oct 21, 2014 at 12:34 PM, David Farmer <far...@aimath.org> wrote:
I have a pretty good idea of some heuristics for automatically inserting
the curly brackets around the subscript.  LaTeX seems to assume everything
following the underscore should be subscript, so
$P_\sqrt{x} + 1234$
puts the + 1234 in the subscript.

Well, that's what I see in *repaired* output.  (Check your log file.)

Go for $P_{\sqrt{x}} + 1234$ or, if you so intend,
$P_{\sqrt{x} + 1234}$.

     -- Bill
Reply all
Reply to author
Forward
0 new messages