Officedown: equation numbering and cross-referencing not working

508 Views Asked by At

I am trying to generate equation numbers and cross-referencing in docx using Rmarkdown, but it is not working.

Here is my YAML

output:
  officedown::rdocx_document:
    reference_docx: ref_transfer.docx
header-includes:
- \usepackage{amsmath}

I have tried using (\#eq:test) in the equation chunk enclosed with $$ (code below)

\begin{equation} 
\begin{gather}
\text{Y}_{ij} = \alpha_{0} + \beta_{0}\text{X}_{1ij} + \alpha_{1}\text{Z}_{1j} + 
\alpha_{2}\text{Z}_{2j} + \beta_{1}\text{Z}_{1j}\text{X}_{1ij} +
\beta_{2}\text{Z}_{2j}\text{X}_{1ij} + v_j\text{X}_{1ij}  + u_j + \epsilon_{ij} \\
\text{where} \\
\epsilon_{} \sim N(0, \sigma^2) \\
u_j \sim N(0, \sigma^2_u) \\
v_j \sim N(0, \sigma^2_v) 
\end{gather}
(\#eq:test)
\end{equation}

and \@ref(eq:test) to cross-reference in line. That produced @ref(eq:test) in line and (#eq:test) as the equation number, see RenderOutput below.

I have also tried using \label{eq:test} (code below)

\begin{equation} 
\begin{gather}
\text{Y}_{ij} = \alpha_{0} + \beta_{0}\text{X}_{1ij} + \alpha_{1}\text{Z}_{1j} + 
\alpha_{2}\text{Z}_{2j} + \beta_{1}\text{Z}_{1j}\text{X}_{1ij} +
\beta_{2}\text{Z}_{2j}\text{X}_{1ij} + v_j\text{X}_{1ij}  + u_j + \epsilon_{ij} \\
\text{where} \\
\epsilon_{} \sim N(0, \sigma^2) \\
u_j \sim N(0, \sigma^2_u) \\
v_j \sim N(0, \sigma^2_v) 
\end{gather}
\label{eq:test}
\end{equation}

in conjunction with \eqref{eq:test} in line.

And that produces blanks where you'd expect to see the equation number and citation, see RenderOutput2.

Does anyone have a work around for this type of problem?? Please help.

1

There are 1 best solutions below

0
On

To get this the expected output in a word document you will need to use bookdown for the proper numbering and cross-referencing. See where the base_format argument is specified in the yaml section below.

---
output: 
  officedown::rdocx_document:
    base_format: "bookdown::word_document2"
---


\begin{equation}
  \begin{gather}
    \text{Y}_{ij} = \alpha_{0} + \beta_{0}\text{X}_{1ij} + \alpha_{1}\text{Z}_{1j} + 
    \alpha_{2}\text{Z}_{2j} + \beta_{1}\text{Z}_{1j}\text{X}_{1ij} +
    \beta_{2}\text{Z}_{2j}\text{X}_{1ij} + v_j\text{X}_{1ij}  + u_j + \epsilon_{ij} \\
    \text{where} \\
    \epsilon_{} \sim N(0, \sigma^2) \\
    u_j \sim N(0, \sigma^2_u) \\
    v_j \sim N(0, \sigma^2_v) 
  \end{gather}
(\#eq:test)  
\end{equation}

This is a cross-reference \@ref(eq:test)

And the results I got:

word output