Show the code
source("R/set-global-only.R", encoding = "UTF-8")
require(rmarkdown)
require(bookdown)
require(knitr)
require(xmerit)
bookdown输出.docx以及xmerit包使用
胡华平
October 18, 2023
本页面由源文档topic-equation-bookdown.qmd
通过Quarto渲染为html格式。
实际测试文档为_topic-equation-bookdown.rmd
(请查看源文档),测试渲染工具包为bookdown
,渲染输出格式为.docx
文档(请查看最终渲染得到的docx文件)。
基于上述原因:
本html页面仅当作对源.Rmd
文档内容的展示之目的,这里渲染显示的公式、标号及引用的最终效果也仅作观摩参考。
bookdown输出.docx以及xmerit包使用的实际公式语法和效果,都需要以上述.Rmd
文档和.docx
文件为最终依据和参照。
系统环境如下:
platform x86_64-w64-mingw32
arch x86_64
os mingw32
crt ucrt
system x86_64, mingw32
status
major 4
minor 3.1
year 2023
month 06
day 16
svn rev 84548
language R
version.string R version 4.3.1 (2023-06-16 ucrt)
nickname Beagle Scouts
相关R包版本如下:
$rmarkdown
[1] 2 24
$bookdown
[1] 0 35
$xmerit
[1] 0 0 11
.Rmd
文档的yaml参数output:
bookdown::word_document2:
fig_caption: yes
toc: no
toc_depth: 4
number_sections: true
global_numbering: true
.docx
输出的公式语法bookdown
解决的Rmarkdown
生态下公式书写中自动标号和交叉引用的问题。
对于.docx
格式输出,bookdown
下公式语法规则如下:
添加公式标签(label):(\#eq:your-label)
公式引用(cross-reference):正文中\@ref(eq:your-label)
公式自动标号(numbering):为确保公式自动标号,请不要使用双美元符号对$$...$$
环境
经验规则:
bookdown::word_document2
输出.docx
使用情境下,任何时候都不建议使用双美元符号对$$...$$
环境。
经测试,如下嵌套环境规则是受到支持的(也正是自定义R包xmerit
(版本0.0.11
)目前所采用的语法规则):
a)equation
环境
\begin{equation}
...
\end{equation}
b)align
环境
\begin{align}
...
\end{align}
a)局部对齐排列的嵌套环境
\begin{align}
\begin{split}
...
\end{split}
\end{align}
b)完全对齐排列的嵌套环境
\begin{equation}
\begin{alignedat}{999}
...
\end{alignedat}
\end{equation}
\begin{equation}
S_n = \frac{X_1 + X_2 + \cdots + X_n}{n}
= \frac{1}{n}\sum_{i}^{n} X_i +5
\quad \text{(equation)}
(\#eq:equation)
\end{equation}
引用语法 `\@ref(eq:equation)`显示为 \@ref(eq:equation)。
\[\begin{equation} S_n = \frac{X_1 + X_2 + \cdots + X_n}{n} = \frac{1}{n}\sum_{i}^{n} X_i +5 \quad \text{(equation)} (\#eq:equation) \end{equation}\]
引用语法 \@ref(eq:equation)
显示为 @ref(eq:equation)。
\begin{align}
a & = b + c && \text{(nodollar-align-1)} (\#eq:align1) \\
c & = d + e && \text{(nodollar-align-2)} (\#eq:align2)
\end{align}
引用语法 `\@ref(eq:align1)`显示为 \@ref(eq:align1);引用语法 `\@ref(eq:align2)`显示为 \@ref(eq:align2)。
\[\begin{align} a & = b + c && \text{(align-nodollar1)} (\#eq:align1) \\ c & = d + e && \text{(align-nodollar2)} (\#eq:align2) \end{align}\]
引用语法 \@ref(eq:align1)
显示为 @ref(eq:align1);引用语法 \@ref(eq:align2)
显示为 @ref(eq:align2)。
$$
常规公式(显示公式和引用,但不显示公式标号)$$
S_n = \frac{X_1 + X_2 + \cdots + X_n}{n}
= \frac{1}{n}\sum_{i}^{n} X_i
\quad \text{(dollar)}
(\#eq:dollar)
$$
引用语法 `\@ref(eq:dollar)`显示为 \@ref(eq:dollar)。
\[ S_n = \frac{X_1 + X_2 + \cdots + X_n}{n} = \frac{1}{n}\sum_{i}^{n} X_i \quad \text{(dollar)} (\#eq:dollar) \]
引用语法 \@ref(eq:dollar)
显示为 @ref(eq:dollar)。
$$
+aligned$$
\begin{aligned}
S_n = \frac{X_1 + X_2 + \cdots + X_n}{n}
= \frac{1}{n}\sum_{i}^{n} X_i +5
\text{(dollar-aligned)}
(\#eq:dollar-aligned)
\end{aligned}
$$
引用语法 `\@ref(eq:dollar-aligned)`显示为 \@ref(eq:dollar-aligned)。
\[ \begin{aligned} S_n = \frac{X_1 + X_2 + \cdots + X_n}{n} = \frac{1}{n}\sum_{i}^{n} X_i +5 \text{(dollar-aligned)} (\#eq:dollar-aligned) \end{aligned} \]
引用语法 \@ref(eq:dollar-aligned)
显示为 @ref(eq:dollar-aligned)。
\begin{aligned}
S_n &= \frac{X_1 + X_2 + \cdots + X_n}{n} \\
&= \frac{1}{n}\sum_{i}^{n} X_i +5
\text{(aligned)}
(\#eq:aligned)
\end{aligned}
引用语法 `\@ref(eq:aligned)`显示为 \@ref(eq:aligned)。
\[\begin{aligned}
S_n &= \frac{X_1 + X_2 + \cdots + X_n}{n} \\
&= \frac{1}{n}\sum_{i}^{n} X_i +5
\text{(aligned)}
(\#eq:aligned)
\end{aligned}\]
引用语法 \@ref(eq:aligned)
显示为 @ref(eq:aligned)。
\begin{alignedat}{999}
&\widehat{mpg}=&&+43.54&&-1.78cyl_i&&+0.01disp_i\\
&(s)&&(4.8601)&&(0.6139)&&(0.0120)\\
&(t)&&(+8.96)&&(-2.91)&&(+0.58)\\
&(cont.)&&-3.79wt_i&&-0.49gear_i &&\\
&(s)&&(1.0818)&&(0.7903) &&\\
&(t)&&(-3.51)&&(-0.62) &&
\quad \text{(alignedat)}\quad
(\#eq:alignedat)
\end{alignedat}
引用语法 `\@ref(eq:alignedat)`显示为 \@ref(eq:alignedat)。
\[\begin{alignedat}{999}
&\widehat{mpg}=&&+43.54&&-1.78cyl_i&&+0.01disp_i\\
&(s)&&(4.8601)&&(0.6139)&&(0.0120)\\
&(t)&&(+8.96)&&(-2.91)&&(+0.58)\\
&(cont.)&&-3.79wt_i&&-0.49gear_i &&\\
&(s)&&(1.0818)&&(0.7903) &&\\
&(t)&&(-3.51)&&(-0.62) &&
\quad \text{(alignedat)}\quad
(\#eq:alignedat)
\end{alignedat}\]
引用语法 \@ref(eq:alignedat)
显示为 @ref(eq:alignedat)。
Warning: replacing previous import 'stats::filter' by 'dplyr::filter' when
loading 'xmerit'
Warning: replacing previous import 'stats::lag' by 'dplyr::lag' when loading
'xmerit'
Call:
lm(formula = mod, data = df)
Residuals:
Min 1Q Median 3Q Max
-4.9159 -1.2484 -0.3566 1.4719 5.9253
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 43.539847 4.860059 8.959 1.42e-09 ***
cyl -1.784296 0.613889 -2.907 0.00722 **
disp 0.006944 0.012007 0.578 0.56782
wt -3.792867 1.081819 -3.506 0.00161 **
gear -0.490445 0.790285 -0.621 0.54007
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 2.624 on 27 degrees of freedom
Multiple R-squared: 0.835, Adjusted R-squared: 0.8105
F-statistic: 34.15 on 4 and 27 DF, p-value: 3.36e-10
xmerit::lx.psm()
函数默认形式为嵌套结构\begin{align}
内含\begin{split}
。
\begin{align}
\begin{split}
mpg_i=&+\beta_{0}+\beta_{1}cyl_i+\beta_{2}disp_i\\&+\beta_{3}wt_i+\beta_{4}gear_i+u_i
\end{split}
\quad \text{(lx.psm)}\quad
(\#eq:lx-psm)
\end{align}
引用语法 `\@ref(eq:lx-psm)`显示为 \@ref(eq:lx-psm)。
\[\begin{align} \begin{split} mpg_i=&+\beta_{0}+\beta_{1}cyl_i+\beta_{2}disp_i\\&+\beta_{3}wt_i+\beta_{4}gear_i+u_i \end{split} \quad \text{(lx.psm)}\quad (\#eq:lx-psm) \end{align}\]
引用语法 \@ref(eq:lx-psm)
显示为 @ref(eq:lx-psm)。
xmerit::lx.est()
函数默认形式为嵌套结构\begin{equation}
内含\begin{alignedat}{999}
。
\[\begin{equation} \begin{alignedat}{999} &\widehat{mpg}=&&+43.54&&-1.78cyl_i&&+0.01disp_i\\ &(s)&&(4.8601)&&(0.6139)&&(0.0120)\\ &(t)&&(+8.96)&&(-2.91)&&(+0.58)\\ &(cont.)&&-3.79wt_i&&-0.49gear_i &&\\ &(s)&&(1.0818)&&(0.7903) &&\\ &(t)&&(-3.51)&&(-0.62) && \end{alignedat} \quad \text{(lx.est)}\quad (\#eq:lx-est) \end{equation}\]
引用语法 \@ref(eq:lx-est)
显示为 @ref(eq:lx-est)。