Rmarkdown写作公式

bookdown输出.docx以及xmerit包使用

Author

胡华平

Published

October 18, 2023

Show the code
source("R/set-global-only.R", encoding = "UTF-8")
require(rmarkdown)
require(bookdown)
require(knitr)
require(xmerit)

测试说明

特别说明

本页面由源文档topic-equation-bookdown.qmd通过Quarto渲染为html格式。

实际测试文档为_topic-equation-bookdown.rmd(请查看源文档),测试渲染工具包为bookdown,渲染输出格式为.docx文档(请查看最终渲染得到的docx文件)。

Important

基于上述原因:

  • 本html页面仅当作对源.Rmd文档内容的展示之目的,这里渲染显示的公式、标号及引用的最终效果也仅作观摩参考。

  • bookdown输出.docx以及xmerit包使用的实际公式语法和效果,都需要以上述.Rmd文档和.docx文件为最终依据和参照。

测试环境

系统环境如下:

Show the code
R.version
session <- utils::sessionInfo()
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包版本如下:

Show the code
pkg <- list(rmarkdown = "rmarkdown", 
            bookdown = "bookdown", 
            xmerit = "xmerit")
sapply(pkg, packageVersion)
$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

bookdown包.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)目前所采用的语法规则):

  • 支持的有效单一LaTex公式环境(environment):

a)equation环境

\begin{equation}
  ...
\end{equation}

b)align环境

\begin{align}
  ...
\end{align}
  • 支持的有效嵌套LaTex公式环境(environment):

a)局部对齐排列的嵌套环境

\begin{align} 
  \begin{split}
   ... 
  \end{split} 
\end{align}

b)完全对齐排列的嵌套环境

\begin{equation} 
  \begin{alignedat}{999}
   ... 
  \end{alignedat} 
\end{equation}

latex公式测试

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)。

align+多公式(正常)

\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)。

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)。

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)。
\[\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)。

xmerit包公式语法

数据集

Show the code
library(xmerit)
Warning: replacing previous import 'stats::filter' by 'dplyr::filter' when
loading 'xmerit'
Warning: replacing previous import 'stats::lag' by 'dplyr::lag' when loading
'xmerit'
Show the code
data(mtcars)
df <- mtcars
mod <- mpg ~ cyl + disp + wt +gear
lm.fit <- lm(formula = mod, data = df)
summary(lm.fit)

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
Show the code
xvars <- all.vars(mod)[-1]
yvars <- all.vars(mod)[1]

xmerit::lx.psm(align+split)

xmerit::lx.psm()函数默认形式为嵌套结构\begin{align}内含\begin{split}

Show the code
lx.out <- xmerit::lx.psm(
  x = xvars, y = yvars,
  begin = 0,
  #greek.n = length(xvars)+1,
  n.row = 3,
  lm.label = "lx-psm",
  lm.tag = "(lx.psm)",
  no_dollar = TRUE)
\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(equation + alignedat)

xmerit::lx.est()函数默认形式为嵌套结构\begin{equation}内含\begin{alignedat}{999}

Show the code
lx.out <- lx.est(lm.mod = mod,lm.dt = df,lm.n = 3,lm.label = "lx-est", lm.tag = "lx.est",
                 no_dollar = TRUE)

\[\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)。