1

我正在尝试写一篇distill::distill_article需要使用 LaTeX 数学环境的博文,例如定理、引理、证明等。

我已尝试遵循这些说明以及这些说明theorem,但无论使用LaTeX块还是 rmarkdown 块,我都无法渲染环境。

我还注意到,有人问过一个类似的问题distill::distill_article,即专门使用in bookdown。此修复程序也不起作用。请注意,我的用例是在内部使用 bookdown 定理环境distill::distill_article,而不是相反。

这是该问题的代表:

---
title: "Test Title"
description: |
  Test description
author: Test author
date: 2021-12-31
output:
  distill::distill_article:
    self_contained: false
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(bookdown)
```

# TL;DR;

Trying to ensure that theorem environments are working in 
`distill::distill_article`.

# Main theorems

With the definitional background established, let's move onto the
main theorem of this article.

\begin{align} 
g(X_{n}) &= g(\theta)+g'({\tilde{\theta}})(X_{n}-\theta) \notag \\
\sqrt{n}[g(X_{n})-g(\theta)] &= g'\left({\tilde{\theta}}\right)
  \sqrt{n}[X_{n}-\theta ] (\#eq:align)
\end{align} 

<!-- # This  does not work, i.e., render in html -->
\begin{theorem}[Delta Method]\label{nthm:deltamethod}
g(X_{n}) &= g(\theta)+g'({\tilde{\theta}})(X_{n}-\theta) \notag \\
\sqrt{n}[g(X_{n})-g(\theta)] &= g'\left({\tilde{\theta}}\right)
  \sqrt{n}[X_{n}-\theta ] (\#eq:align)
\end{theorem} 

<!-- # This reference does not work, i.e., is not recognized -->
We have a labeled and named theorem below, and \@ref(nthm:deltamethod).

<!-- # This  does not work either, i.e., render in html -->
::: {.theorem #pythagoras name="Pythagorean theorem"}
For a right triangle, if $c$ denotes the length of the hypotenuse
and $a$ and $b$ denote the lengths of the other two sides, we have

$$a^2 + b^2 = c^2$$
:::

<!-- # This reference does not work either, i.e., is not recognized -->
We have a labeled and named theorem below, and \@ref(thm:pythagoras).

请注意,我加载bookdown了 rmarkdown,目的是呈现定理环境。但是它不起作用,即不呈现定理或交叉引用。有人可以帮忙解决这个问题吗?

会话信息


─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.1.2 (2021-11-01)
 os       macOS Monterey 12.0.1       
 system   x86_64, darwin17.0          
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2022-01-02                  

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version date       lib source             
 assertthat    0.2.1   2019-03-21 [1] standard (@0.2.1)  
 backports     1.2.1   2020-12-09 [1] standard (@1.2.1)  
 bookdown    * 0.24    2021-09-02 [1] CRAN (R 4.1.0)     
 broom         0.7.10  2021-10-31 [1] CRAN (R 4.1.0)     
 cachem        1.0.5   2021-05-15 [1] standard (@1.0.5)  
 cellranger    1.1.0   2016-07-27 [1] standard (@1.1.0)  
 cli           3.1.0   2021-10-27 [1] CRAN (R 4.1.0)     
 clipr         0.7.1   2020-10-08 [1] standard (@0.7.1)  
 colorspace    2.0-2   2021-06-24 [1] standard (@2.0-2)  
 crayon        1.4.1   2021-02-08 [1] standard (@1.4.1)  
 DBI           1.1.1   2021-01-15 [1] standard (@1.1.1)  
 dbplyr        2.1.1   2021-04-06 [1] standard (@2.1.1)  
 desc          1.4.0   2021-09-28 [1] CRAN (R 4.1.0)     
 details       0.2.1   2020-01-12 [1] CRAN (R 4.1.0)     
 digest        0.6.27  2020-10-24 [1] standard (@0.6.27) 
 distill       1.3     2021-10-13 [1] CRAN (R 4.1.0)     
 downlit       0.4.0   2021-10-29 [1] CRAN (R 4.1.0)     
 dplyr       * 1.0.7   2021-06-18 [1] standard (@1.0.7)  
 ellipsis      0.3.2   2021-04-29 [1] standard (@0.3.2)  
 evaluate      0.14    2019-05-28 [1] standard (@0.14)   
 fansi         0.5.0   2021-05-25 [1] standard (@0.5.0)  
 fastmap       1.1.0   2021-01-25 [1] standard (@1.1.0)  
 forcats     * 0.5.1   2021-01-27 [1] CRAN (R 4.1.0)     
 fs            1.5.2   2021-12-08 [1] CRAN (R 4.1.0)     
 generics      0.1.0   2020-10-31 [1] standard (@0.1.0)  
 ggplot2     * 3.3.5   2021-06-25 [1] standard (@3.3.5)  
 glue          1.4.2   2020-08-27 [1] standard (@1.4.2)  
 gtable        0.3.0   2019-03-25 [1] standard (@0.3.0)  
 haven         2.4.1   2021-04-23 [1] standard (@2.4.1)  
 hms           1.1.0   2021-05-17 [1] standard (@1.1.0)  
 htmltools     0.5.1.1 2021-01-22 [1] standard (@0.5.1.1)
 httr          1.4.2   2020-07-20 [1] standard (@1.4.2)  
 jsonlite      1.7.2   2020-12-09 [1] standard (@1.7.2)  
 knitr         1.37    2021-12-16 [1] CRAN (R 4.1.0)     
 lifecycle     1.0.0   2021-02-15 [1] standard (@1.0.0)  
 lubridate     1.7.10  2021-02-26 [1] standard (@1.7.10) 
 magrittr      2.0.1   2020-11-17 [1] standard (@2.0.1)  
 memoise       2.0.0   2021-01-26 [1] standard (@2.0.0)  
 modelr        0.1.8   2020-05-19 [1] standard (@0.1.8)  
 munsell       0.5.0   2018-06-12 [1] standard (@0.5.0)  
 pillar        1.6.1   2021-05-16 [1] standard (@1.6.1)  
 pkgconfig     2.0.3   2019-09-22 [1] standard (@2.0.3)  
 png           0.1-7   2013-12-03 [1] standard (@0.1-7)  
 purrr       * 0.3.4   2020-04-17 [1] standard (@0.3.4)  
 R6            2.5.0   2020-10-28 [1] standard (@2.5.0)  
 Rcpp          1.0.7   2021-07-07 [1] CRAN (R 4.1.0)     
 readr       * 1.4.0   2020-10-05 [1] standard (@1.4.0)  
 readxl        1.3.1   2019-03-13 [1] standard (@1.3.1)  
 reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.0)     
 rlang         0.4.11  2021-04-30 [1] standard (@0.4.11) 
 rmarkdown     2.11    2021-09-14 [1] CRAN (R 4.1.0)     
 rprojroot     2.0.2   2020-11-15 [1] standard (@2.0.2)  
 rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)     
 rvest         1.0.0   2021-03-09 [1] standard (@1.0.0)  
 scales        1.1.1   2020-05-11 [1] standard (@1.1.1)  
 sessioninfo   1.1.1   2018-11-05 [1] standard (@1.1.1)  
 stringi       1.6.2   2021-05-17 [1] standard (@1.6.2)  
 stringr     * 1.4.0   2019-02-10 [1] standard (@1.4.0)  
 tibble      * 3.1.2   2021-05-16 [1] standard (@3.1.2)  
 tidyr       * 1.1.3   2021-03-03 [1] standard (@1.1.3)  
 tidyselect    1.1.1   2021-04-30 [1] standard (@1.1.1)  
 tidyverse   * 1.3.1   2021-04-15 [1] CRAN (R 4.1.0)     
 utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.0)     
 vctrs         0.3.8   2021-04-29 [1] standard (@0.3.8)  
 withr         2.4.2   2021-04-18 [1] standard (@2.4.2)  
 xfun          0.29    2021-12-14 [1] CRAN (R 4.1.0)     
 xml2          1.3.2   2020-04-23 [1] standard (@1.3.2)  
 yaml          2.2.1   2020-02-01 [1] standard (@2.2.1)  

[1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

4

3 回答 3

2

bookdown功能可以使用此处base_format描述的参数与其他单一格式一起使用。

如果所有bookdown的特征都不是 100% 兼容distill格式,那么定理和证明处理应该是。所以这将起作用:


---
title: "Test Title"
description: |
  Test description
author: Test author
date: 2021-12-31
output:
  bookdown::html_document2:
    self_contained: false
    base_format: distill::distill_article
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# TL;DR;

Trying to ensure that theorem environments are working in `distill::distill_article`.

::: {#pythagoras .theorem name="Pythagorean theorem"}
For a right triangle, if $c$ denotes the length of the hypotenuse and $a$ and $b$ denote the lengths of the other two sides, we have

$$a^2 + b^2 = c^2$$
:::

We have a labeled and named theorem below, and \@ref(thm:pythagoras).

bookdown 和 distill 示例

您将获得与 bookdown 中相同的 HTML 渲染。

有关更多上下文,请参阅Github 上的这个相关问题。

于 2022-01-04T14:32:30.990 回答
1

我没有完成所有这些,但这是引理的一个。这使用了两个类:lemma 和 lemmaS。当您使用类 lemma 时,它会更改整数,当您使用 lemmaS 时,它会增加小数。此方法可应用于所有不同类型的公式块。

更新的样式:

<style>
h1 {
  counter-reset: lemma;
}
h2 {
  counter-reset: lemmaS;
}
.lemma, .lemmaS, .theorem { /* updated to add two types of lemma */
  display: block;
  font-style: italic;
}
.lemma:first-of-type::before, 
.lemmaS:first-of-type::before {
  counter-set: lemma 1;
}
.theorem::before{
  content:"Theorem. ";
  font-weight: bold; 
  font-style: normal;
}
.theorem[name]::before{
  content: "Theorem (" attr(name) ") ";
}
.lemma::before {    /* This will increment by whole numbers: 1, 2, 3 */
  counter-increment: lemma; 
  content: "Lemma " counter(lemma) "." counter(lemmaS);
  font-weight: bold;
  font-style: normal;
}
.lemmaS::before {   /* This will increment by decimals numbers: 2.1, 2.2 */
  counter-increment: lemmaS;
  content: "Lemma " counter(lemma) "." counter(lemmaS) " ";
  font-weight: bold;
  font-style: normal;
}
</style>

代码,使用h1#重置整数;<h2>##重置小数点。

# TL;DR;

::: {.theorem name="Pythagorean theorem"}
For a right triangle, if $c$ denotes the length of the hypotenuse
and $a$ and $b$ denote the lengths of the other two sides, we have
$$a^2 + b^2 = c^2$$
:::

<div class="lemma" name="pythag">
For a right triangle, if $c$ denotes the length of the hypotenuse
and $a$ and $b$ denote the lengths of the other two sides, we have
$$a^2 + b^2 = c^2$$
</div>

<div class="lemmaS" name="YetAnotherPythag">
For a right triangle, if $c$ denotes the length of the hypotenuse
and $a$ and $b$ denote the lengths of the other two sides, we have
$$a^2 + b^2 = c^2$$
</div>

## Tell me about lemmas

<div class="lemma" name="andMore">
For a right triangle, if $c$ denotes the length of the hypotenuse
and $a$ and $b$ denote the lengths of the other two sides, we have
$$a^2 + b^2 = c^2$$
</div>

在我打字的时候看着这个,我想到了一些可以让这个更干净的方法......但我只是想让它在那里工作!我确信有一种方法只使用一类引理,使用标题来表示何时增加整数。无论哪种方式,这都应该让你有其他选择。如果您不想要数字,请使用定理设置,只需将单词 theorem 更改为您想要查看的单词。如果要对它们进行编号,请将单词 lemma 更改为要出现的单词。如果您想要以上所有内容,请在 CSS 中附加名称。

例如,要使用编号,但添加推论 - 您可以在不使用“内容”一词的地方附加,在使用“内容”的地方,复制并更改该词:

.corrolary, 
.lemma, 
.lemmaS, 
.theorem {    /* updated to add two types of lemma */
  display: block;
  font-style: italic;
}
.corollary::before{. /* Copy of .theorem, updated for Corollary */
  content:"Corollary. ";
  font-weight: bold; 
  font-style: normal;
}

在此处输入图像描述

于 2022-01-04T04:08:16.123 回答
1

在 YAML 之后添加它,然后之间的方法:::将起作用:

<style>
.theorem{
  display: block;
  font-style: italic;
}
.theorem::before{
  content:"Theorem. ";
  font-weight: bold; 
  font-style: normal;
}
.theorem[name]::before{
  content: "Theorem (" attr(name) ") ";
}
</style>

::: {.theorem name="Pythagorean theorem"}
For a right triangle, if $c$ denotes the length of the hypotenuse
and $a$ and $b$ denote the lengths of the other two sides, we have
$$a^2 + b^2 = c^2$$
:::

在此处输入图像描述

如果您希望能够通过超链接引用该定理,您可以使用href,如下所示:

如果你想参考你的定理,你可以这样做。考虑到<a href="#Pythagorean theorem">勾股定理</a>,应该有办法把它联系起来。

在此处输入图像描述

于 2022-01-03T05:49:06.983 回答