equation numbering in markdown/knitr

10,816 views
Skip to first unread message

Guy Lebanon

unread,
May 22, 2012, 6:11:19 AM5/22/12
to kn...@googlegroups.com
I can't get equation numbering to work with R markdown. The \label and \ref from latex mechanism doesn't work. I've seen somewhere it's possible to use @ notation for a reference, but couldn't get it to work with R+markdown. Do you know if this works, or have any other idea?


Guy Lebanon

unread,
May 22, 2012, 9:04:35 AM5/22/12
to kn...@googlegroups.com
just to clarify: I am looking for a solution to have equations numbered consecutively, and by able to refer to each of them by name (similar to \label and \eqref or \ref in latex).

Reading on pandoc, I saw that the following should work, but it does not seem to work in R Markdown.

Any ideas on how to accomplish such a thing?

(@myeqn1) 
$$ 
\begin{align}
f(x)&=\int_{-\infty}^x e^{-x^2/2}\,dx\\
&=A
\end{align}
$$

Equation (@myeqn1) is very important

Yihui Xie

unread,
May 22, 2012, 11:48:56 AM5/22/12
to Guy Lebanon, kn...@googlegroups.com
Because this functionality is not in markdown. Markdown is a really
simple language; that is why other people keep on inventing new
variants. Pandoc has its own invention, which supports equation
numbers and labels, so if you need them, you'll have to use pandoc to
convert your markdown document instead of RStudio (which uses the
sundown library). However, you may send a feature request to RStudio
developers to support this with MathJax, since MathJax supports
numbering: http://www.mathjax.org/docs/2.0/tex.html#autmatic-equation-numering

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Carl Boettiger

unread,
May 22, 2012, 12:21:46 PM5/22/12
to Yihui Xie, Guy Lebanon, kn...@googlegroups.com
Hi Guy,

Just to be clear, knitr is just an R interpreter, it doesn't touch anything that isn't in an R chunk (which, frankly, is fantastic since we don't need one more variation of markdown).  That said, you are free to do whatever you want with the input format.  As Yihui says, if you want an import format based on pandoc, just make sure to use pandoc as the intepreter for the markdown file output by knitr.  

If you're looking to ultimately generate html output, I'd agree that mathjax is the way to go.  Remember that markdown supports html -- for instance, I can get nice mathjax vector-graphics equations from an R+markdown file using Rstudio just by putting adding the html script to the file, as I do in this example. (html display)

Note that I need to put the displayed math in <div>'s so that it is not clobbered by the markdown->html converter (that trick works with most markdown converters, Rstudio isn't required).  While it would be very nice if RStudio let you customize its html template so that the script files could sit in there (any ideas on how to hack that?  I think it's already a feature request), it's not necessary if you just need numbered equations. 

Carl

--
Carl Boettiger
UC Davis
http://www.carlboettiger.info/

Noam Ross

unread,
May 22, 2012, 12:35:20 PM5/22/12
to kn...@googlegroups.com
If you use carl's example, see these MathJax pages for documentation and examples of how to add labels to your equations in HTML:


Vincent Nijs

unread,
Jul 9, 2015, 3:25:45 AM7/9/15
to kn...@googlegroups.com
Curious if there has been any updates on this issue/feature since 2012. Knitr and rmarkdown support mathJax and mathJax supports equation numbering. Is there a way to specify equation numbers in an Rmd documents?

Yihui Xie

unread,
Jul 9, 2015, 1:37:55 PM7/9/15
to Vincent Nijs, knitr
rmarkdown uses Pandoc to convert Markdown to HTML, so it is up to
Pandoc whether numbering equations is possible (which I don't know).

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name
> --
> You received this message because you are subscribed to the Google Groups
> "knitr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to knitr+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Vincent Nijs

unread,
Jul 9, 2015, 1:41:32 PM7/9/15
to kn...@googlegroups.com, vincen...@gmail.com
At the moment I'm using Knitr rather than rmarkdown. Are equation numbers possible in Knitr with mathJax? The link that Naom provided suggests that mathJax does support equation numbering.

Jeff Newmiller

unread,
Jul 9, 2015, 1:58:28 PM7/9/15
to Vincent Nijs, knitr

Knitr and rmarkdown are not mutually exclusive, so your response makes no sense. To clarify, knitr has nothing to do with mathjax... whatever back end you are sending the output of knitr to handles that, so go look at the documentation for that tool (pandoc).

Noam Ross

unread,
Jul 9, 2015, 2:01:55 PM7/9/15
to Vincent Nijs, kn...@googlegroups.com
This is something that isn't addressed directly within knitr or pandoc, but in your MathJax syntax and options.

For general equation numbering, you need to set MathJax options, which you can do by having something like this in your file or the HTML template you use:

```
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: { equationNumbers: { autoNumber: "AMS" } }
});
</script> 
```

For manual labels and numbering for specific equations, `\label{something}`should go inside the `$$...$$` equation fences, and `\ref{something}` can go in text to refer to equations..  See the MathJax documentation (<http://docs.mathjax.org/en/latest/tex.html#automatic-equation-numbering>) and the source code of the example page (<https://cdn.mathjax.org/mathjax/latest/test/sample-eqrefs.html>) for more on this.
Message has been deleted

Vincent Nijs

unread,
Jul 9, 2015, 4:47:51 PM7/9/15
to kn...@googlegroups.com, vincen...@gmail.com
Thanks for the reply Noam. It took me a while to figure how to apply your suggestion in a ship-app but I got it to work eventually. Example here https://github.com/vnijs/quizr. Thanks again!

Clay S

unread,
Jan 25, 2016, 11:47:26 PM1/25/16
to knitr, vincen...@gmail.com
This helped a lot.

However, 
```
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "AMS"} } });
</script>
```
Does not work, but
```
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "all"} } });
</script>
```
does work.

The only difference is the "AMS" vs "all" parameter.  I did add the `\label{ref1}` to each equation, but I did not need to for the mathjax script that works ("all").  I could not get the "AMS" script to work either way. 

I am also trying to figure out a quick option to add the Section or Chapter number to the equation number, i.e. (9.1) for the first equation in Chapter 9, instead of just autoNumbering them as (1) etc.  Would also like to be able to add leading characters like "eq." to the numbering.

Thanks

Clay S

unread,
Jan 26, 2016, 4:19:06 PM1/26/16
to knitr, vincen...@gmail.com
Update:

I found that I can change the label by adding `\tag{mylabel}` inside the $$..$$, but the auto-numbering will then skip that equation.  The `\tag{mylabel}` also does not need either of the mathjax script statements in the previous post.

The `\label{*}` statement inside the $$..$$ does not seem to do anything with either of the mathjax script statements in my previous post.

Clay S

unread,
Jan 26, 2016, 10:21:16 PM1/26/16
to knitr, vincen...@gmail.com
2nd update:

I can auto-number mathjax equations in an R markdown document in RStudio by adding the following statement to the beginning of the Rmd document:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: { 
      equationNumbers: {
 
            autoNumber: "all",
            formatNumber: function (n) {return '9.'+n}
      } 
  }
});
</script>


The following also works in an Rmd doc, but not with the $$..$$ equations, only equations bracketed with \begin{equation}..\end{equation}.


<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: { 
      equationNumbers: {
 
            autoNumber: "AMS",
            formatNumber: function (n) {return '9.'+n}
      } 
  }
});
</script>


I can change the numbering to a static label with \tag{mylabel} inside the $$..$$, and the auto-numbering will skip that equation.

Unfortunately, to skip with no label add \tag{} results in the equation still labeled with ().


How do I get rid of the empty brackets if needed?


Does anybody know a simple statement to add to either the mathjax script statement or the $$..$$ equations within a Rmd doc that would add an auto-numbered Section number to the equation number?

most of the info from here: http://mathjax.readthedocs.org/en/latest/tex.html#automatic-equation-numbering

Reply all
Reply to author
Forward
0 new messages