10

When kniting a Rmarkdown file to MS Word, is there a way to have equations labled with a number that is flushed to the right?

For example:

When typing this in Rmarkdown:

$$a + b = c$$

I need it to end up in Word as:

a + b = c (1)

I saw the code below somewhere else, but it does not seem to work when kniting to Word...

\begin{equation} \label{eq-abc} a + b = c \end{equation}

Thanks!

Sholom
  • 141
  • 1
  • 7
  • For `html` and `pdf` output formats, the [`bookdown`](https://bookdown.org/yihui/bookdown/) package does what you want. MS Word however, is a pain, you may be able to accomplish your aims with [`officer`](https://davidgohel.github.io/officer/) that includes captions... or develop a `pandoc` template of your own to suit the conversion. The code included in your question is `TeX` and is recognised in `pandoc` markdown for `pdf` output, but the equation numbering is not in the standard template to my knowledge. – Kevin Arseneau Dec 27 '17 at 05:25
  • Certain academic journals (such as The Accounting Review) require submissions specifically in Word format. They also require equation numbering. I haven't been able to work this out using `officer` and am not familiar with how to manipulate `pandoc`. I did just come across the following but do not know how to use it... [pandoc-crossref package](https://hackage.haskell.org/package/pandoc-crossref) – Sholom Dec 27 '17 at 13:49

1 Answers1

7

@Sholom's comment regarding pandoc-crossref inspired me to have a read, I was not previously aware of it and it is not available as an extension to rmarkdown currently.

It is entirely possible to achieve basic equation numbering with this "filter".

Markdown

---
output:
  word_document:
    pandoc_args: ["-Fpandoc-crossref"]
---


$$a^2 + b^2 = c^2$$ {#eq:eqn1}

$$\log xy = \log x + \log y$$ {#eq:eqn2}

$$\frac{df}{dt} = \lim_{h\to0}\frac{f(t+h)-f(t)}{h}$$ {#eq:eqn3}

Output

enter image description here

pandoc-crossref

The binary releases of the code are located here. From a windows machine, I dropped the pandoc-crossref.exe into my Pandoc install folder alongside pandoc.exe and that was sufficient to allow my markdown to render.

N.B. I did this with a standalone install of Pandoc, if you are using the version bundled with RStudio you may need to install differently.

Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40
  • This worked for me. As a side note about installation, I downloaded the `.zip` file containing the `.exe` and dropped that into the `bin` folder within my `RStudio` installation alongside the `pandoc.exe` – tbradley Jan 30 '18 at 17:24
  • 1
    @tbradley, yes, that may work for you but I would suggest the solution is *fragile*. I say this for two main reasons: (1) if you change your RStudio installation you may lose `pandoc-crossref`, a fairly minor annoyance; (2) this may not necessarily work indefinitely into the future as the packaged version of `pandoc` may be incompatible with the `pandoc-crossref` you drop into the RStudio location. – Kevin Arseneau Jan 30 '18 at 22:11
  • Any suggestions on how to install pandoc-crossref on a Mac for R Studio? – Jeremy K. Oct 11 '19 at 04:17
  • 2
    For any future Mac users reading this, `brew install pandoc-crossref` from terminal installed it and got it working for me. – Jeremy K. Oct 11 '19 at 04:22