3.6 Cross References
3.6.1 Using bookdown
You can number and refer to an equation by adding \begin{equation}
along with a label, provided with (\#eq:label)
.
The position of the label matters.
- For single-lined equations: First write your equation, then append your label
(\#eq:label)
. Otherwise, your equation won’t be rendered. - For multi-lined equations: append
(\#eq:label)
after\end{split}
,\end{aligned}
…
- For single-lined equations: First write your equation, then append your label
Note that
\begin{equation}
must NOT be quoted in$$...$$
for the equation to be rendered.Otherwise, will cause “Bad math delimiter” error at the time of tex compilation for pdf output. Might be alright for html output though.
Unexpected consequence: Without the
$$...$$
, RStudio won’t provide previews for equations.- For temporary preview in RStudio at the composing stage, you can enclose the whole math environment in
$$...$$
. But remember to delete them when you are done editing the equation. - See this post by Kenji Sato for a more efficient workaround.
- For temporary preview in RStudio at the composing stage, you can enclose the whole math environment in
You can then refer to the equation in text using
\@ref(eq:CJ)
. Remember to put the label in parentheses()
.General syntax for other environments:
\@ref(type:label)
wheretype
is the environment being referenced, andlabel
is the chunk label.
This is an equation redered using bookdown
\begin{equation} (\#eq:CJ)
y=\beta_0 + \beta_1x + e_t
\end{equation}
will render as
\[\begin{equation} y=\beta_0 + \beta_1x + e_t \tag{3.1} \end{equation}\]
You may refer to it using eqn \@ref(eq:CJ)
, e.g., see eqn (3.1).
Multilined equations.
\begin{equation}
\begin{aligned}
y_i &= f(x_{1i}, x_{2i}, \ldots, x_{Ki}) + \varepsilon_i \\
&= x_{1i} \beta_1 + x_{2i} \beta_2 + \cdots + x_{Ki} \beta_K + \varepsilon_i
\end{aligned}(\#eq:scalar-form)
\end{equation}
will render as
\[\begin{equation} \begin{aligned} y_i &= f(x_{1i}, x_{2i}, \ldots, x_{Ki}) + \varepsilon_i \\ &= x_{1i} \beta_1 + x_{2i} \beta_2 + \cdots + x_{Ki} \beta_K + \varepsilon_i \end{aligned}\tag{3.2} \end{equation}\]
You may refer to it using eqn \@ref(eq:scalar-form)
, e.g., see eqn (3.2) .
Note that
For HTML output, bookdown can only number the equations with labels.
Please make sure equations without labels are not numbered by either using the
equation*
environment or adding\nonumber
or\notag
to your equations.
Troubleshooting
Issue: Bad math environment delimiter on conversion to pdf when using equation or align.
Cause: The error happens because I enclosed \begin{equation}
environment in $$
. I did this as the dollar sings enable equation rendering and preview in file.
Fix: remove the double signs.
The following equation causes error. Need to remove the dollar signs.
$$
\begin{equation}
y=x+2
\end{equation}
$$
More examples:
Headers
Refer to Markdown: cross references for more examples.
Figures
Tables
Theorems
Equations
3.6.2 Using the LaTeX Way
The LaTeX way allows you to assign your own labels by \tag
. One drawback is that this does not allow preview of equations.
Add the following script at the beginning of your document body:
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ TeX: { equationNumbers: { autoNumber: "AMS" } } }); </script>
It configures MathJax to automatically number equations. Source.
In the text, use
label{eq:label}
. If you want to provide a specific number to the equation, you can use\tag{XX.XX}
.- Note that
\begin{equation}
is NOT inside$$ ...$$
!
- Note that
Cite using
$\ref{eq:label}$
(no parenthesis) or$\eqref{eq:label}$
(with parenthesis). The dollar sign$
here around\ref
and\eqref
is not essential. Commands work with or without$
.Without using the bookdown package. \begin{equation} \label{eq:test} \tag{my custom label} Y_i = \beta_0 + \beta_1 x_i + \epsilon_i \end{equation} Cite Equation $\eqref{eq:test}$ like this.
\[\begin{equation} \label{eq:test} \tag{my label} Y_i = \beta_0 + \beta_1 x_i + \epsilon_i \end{equation}\]
Refer to the eq \(\eqref{eq:test}\)
Reference:
https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#equations