Officedown: equation numbering and cross-referencing not working - latex

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.

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:

Related

Latex - Uncomplete vertical line on tabular

I'm trying to make a bit tricky table in Latex using tabular, I wrote the following code :
\begin{tabular}{|c|c|c|c|c|}
\hline
n° structure & n° hexagone & valeur circuit & $R(K_i)$ & $E(B)$\\
\hline
\multirow{3}{*}{$K_1$} & $h_1$ & $R_1$ & \multirow{3}{*}{$2R_1 + R_2 = 1.984$} & \multirow{12}{*}{$\frac{6R_1 + 4R_2 + 2R_3}{4} = 1.5995$}\\
& $h_2$ & $R_1$ & \\
& $h_3$ & $R_2$ & \\
\cline{1-4}
\multirow{3}{*}{$K_2$} & $h_1$ & $R_1$ & \multirow{3}{*}{$R_1 + R_2 + R_3 = 1.215$} \\
& $h_2$ & $R_2$ & \\
& $h_3$ & $R_3$ & \\
\cline{1-4}
\multirow{3}{*}{$K_3$} & $h_1$ & $R_2$ & \multirow{3}{*}{$2R_1 + R_2 = 1.984$} \\
& $h_2$ & $R_1$ & \\
& $h_3$ & $R_1$ & \\
\cline{1-4}
\multirow{3}{*}{$K_4$} & $h_1$ & $R_3$ & \multirow{3}{*}{$R_1 + R_2 + R_3 = 1.215$} \\
& $h_2$ & $R_2$ & \\
& $h_3$ & $R_1$ & \\
\hline
\end{tabular}
And I got the following output, I really don't understand why the last vertical line isn't complete :
Does someone known the origin of the problem ?
Cordialy.
Your table would look much more professional if you would not use vertical lines. See e.g. the documentation of the booktabs package for more information about creating good looking tables.
If you insist on vertical lines, you must make sure that each row has the same number of cells:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|c|c|}
\hline
n° structure & n° hexagone & valeur circuit & $R(K_i)$ & $E(B)$\\
\hline
\multirow{3}{*}{$K_1$} & $h_1$ & $R_1$ & \multirow{3}{*}{$2R_1 + R_2 = 1.984$} & \multirow{12}{*}{$\frac{6R_1 + 4R_2 + 2R_3}{4} = 1.5995$}\\
& $h_2$ & $R_1$ & & \\
& $h_3$ & $R_2$ & & \\
\cline{1-4}
\multirow{3}{*}{$K_2$} & $h_1$ & $R_1$ & \multirow{3}{*}{$R_1 + R_2 + R_3 = 1.215$} &\\
& $h_2$ & $R_2$ & & \\
& $h_3$ & $R_3$ & & \\
\cline{1-4}
\multirow{3}{*}{$K_3$} & $h_1$ & $R_2$ & \multirow{3}{*}{$2R_1 + R_2 = 1.984$} &\\
& $h_2$ & $R_1$ & & \\
& $h_3$ & $R_1$ & & \\
\cline{1-4}
\multirow{3}{*}{$K_4$} & $h_1$ & $R_3$ & \multirow{3}{*}{$R_1 + R_2 + R_3 = 1.215$} &\\
& $h_2$ & $R_2$ & & \\
& $h_3$ & $R_1$ & & \\
\hline
\end{tabular}
\end{document}

How to improve latex table

In this table, how can I have the upper line that matches between the two tables? I would like to have two tables with the same height. Here is the code that I used to create the table
\documentclass{article}
\usepackage{geometry}
\usepackage{textcomp}
\usepackage{adjustbox}
\usepackage{mathtools}
\usepackage{booktabs} %
\usepackage[group-separator={,}]{siunitx}
\usepackage{changepage}
\newcommand{\undepth}[1]{%
\smash[b]{%
\begin{varwidth}[t]{\linewidth}#1\end{varwidth}
}%
}
\usepackage{makecell}%To keep spacing of text in tables
\begin{document}
\begin{table}[htbp!]
\centering
\footnotesize
\caption{caption}
\begin{tabular}{lSSSS}
\toprule
\makecell[cc]{column1 \\ second line} & \multicolumn{1}{l}{column2} & \multicolumn{1}{l}{column3} & \multicolumn{1}{l}{column4} & \multicolumn{1}{l}{column5} \\
\midrule
A & 4 & 0 & 0.00 & 4 \\
B & 30 & 0 & 0.00 & 30 \\
\bottomrule
\end{tabular}
\quad
\footnotesize
\begin{tabular}{lSS}
\toprule
& {Column1.1 } & {Column2.1}\\
\midrule
A & 0.02 & 0.00 \\
B & 0.04 & 0.00 \\
\bottomrule
\end{tabular}%
\end{table}%
\end{document}
Quick hack:
Put some invisible dummy header of the same height as in the other table
\documentclass{article}
\usepackage{geometry}
\usepackage{textcomp}
\usepackage{adjustbox}
\usepackage{mathtools}
\usepackage{booktabs} %
\usepackage[group-separator={,}]{siunitx}
\usepackage{changepage}
\newcommand{\undepth}[1]{%
\smash[b]{%
\begin{varwidth}[t]{\linewidth}#1\end{varwidth}
}%
}
\usepackage{makecell}%To keep spacing of text in tables
\begin{document}
\begin{table}[htbp!]
\centering
\footnotesize
\caption{caption}
\begin{tabular}{lSSSS}
\toprule
\makecell[cc]{column1 \\ second line} & \multicolumn{1}{l}{column2} & \multicolumn{1}{l}{column3} & \multicolumn{1}{l}{column4} & \multicolumn{1}{l}{column5} \\
\midrule
A & 4 & 0 & 0.00 & 4 \\
B & 30 & 0 & 0.00 & 30 \\
\bottomrule
\end{tabular}
\quad
\footnotesize
\begin{tabular}{lSS}
\toprule
\makecell[cc]{\mbox{} \\ \mbox{}} & {Column1.1 } & {Column2.1}\\
\midrule
A & 0.02 & 0.00 \\
B & 0.04 & 0.00 \\
\bottomrule
\end{tabular}%
\end{table}%
\end{document}
Cleaner solution:
Use only one tabular
\documentclass{article}
\usepackage{geometry}
\usepackage{textcomp}
\usepackage{adjustbox}
\usepackage{mathtools}
\usepackage{booktabs} %
\usepackage[group-separator={,}]{siunitx}
\usepackage{changepage}
\newcommand{\undepth}[1]{%
\smash[b]{%
\begin{varwidth}[t]{\linewidth}#1\end{varwidth}
}%
}
\usepackage{makecell}%To keep spacing of text in tables
\begin{document}
\begin{table}[htbp!]
\centering
\footnotesize
\caption{caption}
\begin{tabular}{lSSSSSS}
\cmidrule[\heavyrulewidth](r){1-5}\cmidrule[\heavyrulewidth](l){6-7}
\makecell[cc]{column1 \\ second line} & \multicolumn{1}{l}{column2} & \multicolumn{1}{l}{column3} & \multicolumn{1}{l}{column4} & \multicolumn{1}{l}{column5} & {Column1.1 } & {Column2.1} \\
\cmidrule(r){1-5}\cmidrule(l){6-7}
A & 4 & 0 & 0.00 & 4 & 0.02 & 0.00 \\
B & 30 & 0 & 0.00 & 30 & 0.04 & 0.00 \\
\cmidrule[\heavyrulewidth](r){1-5}\cmidrule[\heavyrulewidth](l){6-7}
\end{tabular}%
\end{table}%
\end{document}

How do you align a system of equations when one line is longer than the others?

I'm trying to align this system of equations nicely but it doesn't work out. I think I could hack it but I keep running into this problem and would like to do it right. fiddle
\begin{align}
a_{11}x_1 + a_{12}x_2 + a_{13}x_3 + \cdots + a_{1n}x_n &= 0 \\
a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + \cdots + a_{2n}x_n &= 0 \\
a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + \cdots + a_{3n}x_n &= 0 \\
&\vdots \\
a_{m1}x_1 + a_{m2}x_2 + a_{m3}x_3 + \cdots + a_{mn}x_n &= 0
\end{align}
You can set this in a single align and adjust for the spacing using some \phantoms and overlapping:
\begin{align}
\phantom{a_{m1}x_1}\llap{a_{11}x_1\,} +
\phantom{a_{m2}x_2}\llap{a_{12}x_2\,} +
\phantom{a_{m3}x_3}\llap{a_{13}x_3\,} + \cdots +
\phantom{a_{mn}x_n}\llap{a_{1n}x_n\,} &= 0 \\
\phantom{a_{m1}x_1}\llap{a_{21}x_1\,} +
\phantom{a_{m2}x_2}\llap{a_{22}x_2\,} +
\phantom{a_{m3}x_3}\llap{a_{23}x_3\,} + \cdots +
\phantom{a_{mn}x_n}\llap{a_{2n}x_n\,} &= 0 \\
\phantom{a_{m1}x_1}\llap{a_{31}x_1\,} +
\phantom{a_{m2}x_2}\llap{a_{32}x_2\,} +
\phantom{a_{m3}x_3}\llap{a_{33}x_3\,} + \cdots +
\phantom{a_{mn}x_n}\llap{a_{3n}x_n\,} &= 0 \\
& \phantom{{}={}}\llap{\vdots~~} \\
a_{m1}x_1 + a_{m2}x_2 + a_{m3}x_3 + \cdots + a_{mn}x_n &= 0
\end{align}
Fiddle
\phantom{<stuff>} sets <stuff> in an invisible box thereby taking up the space of <stuff> (horizontally and vertically) without setting anything visually. \llap{<stuff>} puts <stuff> in a zero-width box that is right-aligned. This effectively lets <stuff> hang or overlap to the left.
Additional alignment tweaks are made possible by \, - a small horizontal space.
In align and similar environments, vertical alignment is performed along the & characters in each line. So, if you want to align along each + sign, equip them with & each:
EDIT: Since align centers the rows, you need to surround the + with & on both sides:
\begin{align}
a_{11}x_1 &+& a_{12}x_2 &+& a_{13}x_3 &+& \cdots &+& a_{1n}x_n &=& 0 \\
a_{21}x_1 &+& a_{22}x_2 &+& a_{23}x_3 &+& \cdots &+& a_{2n}x_n &=& 0 \\
a_{31}x_1 &+& a_{32}x_2 &+& a_{33}x_3 &+& \cdots &+& a_{3n}x_n &=& 0 \\
&&&&&&&&&\vdots \\
a_{m1}x_1 &+& a_{m2}x_2 &+& a_{m3}x_3 &+& \cdots &+& a_{mn}x_n &=& 0
\end{align}
Alternatively, you could use \[\begin{array}{lllll}...\end{array}\] as an environment, left-aligning the individual columns.

Aligning equations within a box in rmarkdown

I am trying to get aligned equations within a box in rmarkdown. I am knitting to pdf.
The reason the box is necessary is because the YAML metadata I use automatically left-aligns all the text, but I want the equations centered.
Here is my rmarkdown code. As you can see when you run it, the equations are centered but the second set of = signs are not aligned across the two lines.
Any help much appreciated.
---
title: "Aligning equations in a box"
output:
pdf_document: default
html_document: null
word_document: null
toc: yes
linestretch: 1.3
classoption: fleqn
header-includes:
- \setlength{\mathindent}{0pt}
- \setlength\parindent{0pt}
---
\setlength{\abovedisplayskip}{-15pt}
\setlength{\belowdisplayskip}{1pt}
\setlength{\abovedisplayshortskip}{1pt}
\setlength{\belowdisplayshortskip}{1pt}
```{r global_options, include=FALSE, echo = FALSE}
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
echo=FALSE, warning=FALSE, message=FALSE, dev = 'pdf')
```
These words are automatically left-aligned by the YAML meta-data above so a box is necessary to centre equations.
### Equation 11.6
\[\large
\makebox[\linewidth]{$\displaystyle
\begin{aligned}
\text{when}\ PT &= 0:logit\ h(t_{j})\ &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
\text{when}\ PT &= 1:logit\ h(t_{j})\ &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
\end{aligned}
$}
\]
Not sure I understand, but is it necessary to have a big space between? If not, you can just remove the &,
\[\large
\makebox[\linewidth]{$\displaystyle
\begin{aligned}
\text{when}\ PT =& 0:logit\ h(t_{j})\ = [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
\text{when}\ PT =& 1:logit\ h(t_{j})\ = [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
\end{aligned}
$}
\]
this gives
If you want a space you could use something like \qquad or \quad
\[\large
\makebox[\linewidth]{$\displaystyle
\begin{aligned}
\text{when}\ PT =& 0:logit\ h(t_{j})\ \qquad= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
\text{when}\ PT =& 1:logit\ h(t_{j})\ \qquad= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
\end{aligned}
$}
\]
which gives me
EDIT:
Answering the question in your comment. Wrap the blocks in between two &.
\[\large
\makebox[\linewidth]{$\displaystyle
\begin{aligned}
\text{whenmoretext}\ PT &= 0:logit\ h(t_{j})\ & &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
\text{when}\ PT &= 1:logit\ h(t_{j})\ & &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
\end{aligned}
$}
\]

how to center matrix in latex presentation

\centering
\[
\begin{matrix}
\hline P& \beta^{1p} &\beta^{2p} & \beta^{3p} & \beta^{4p} &E(c^{p}) &\sigma(\delta c^{p}) &E(r^{p}-r^{f}) & \sigma( r^{p}-r^{f}) \\
\hline 1& 1.4360 & 0.0018& -0.0123 &-0.0195 &0.0292 & 0.0048 &0.0013 &0.0809 \\
5& 1.1428 & 0.0007 &-0.0098 &-0.0091 & 0.0113 &0.0018 &0.0041 &0.0618 \\
10& 1.0108 & 0.0005 & -0.0083 & -0.0073 & 0.0086 &0.0013 &0.0042 &0.0544 \\
15& 0.9065 &0.0004 &-0.0080 &-0.0061 & 0.0068 & 0.0010 &0.0050 &0.0493 \\
20&0.7602 &0.0003 &-0.0065 & -0.0045 & 0.0050 &0.0008 & 0.0060 & 0.0417\\
25& 0.7192 & 0.0000 &-0.0061 & -0.0010 & 0.0020 & 0.0003 & 0.0068 &0.0417
\end{matrix}
\]
This is my code and I want this matrix to be more in center.

Resources