latex/pdf 的 bookdown 转换为枚举中的自定义块中的问题

Problem in custom blocks in bookdown conversion to in enumerate for latex/pdf

我正在使用 RStudio 中的 bookdown 将一系列讲义编译成一篇手稿。我有一个自定义块,其中包含一个枚举列表。

    ```{r, echo=FALSE}
    instructorVersion<-TRUE
    ```
    What is 1+1?

    ```{block, type='ANSWER', echo=instructorVersion, purl=FALSE}
    1. This answer is easy
    2. Yes, really, that's all that I was asking.
    ```

    What is the meaning of life?

    ```{block, type='ANSWER', echo=instructorVersion, purl=FALSE}
    The Answer is only for Instructor's Eyes

    1. The answer is hard
    2. You won't get it right
    3. But try anyway
    ```

第一个块在乳胶中表现良好:

\begin{ANSWER}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
  This answer is easy
\item
  Yes, really, that's all that I was asking.
\end{enumerate}
\end{ANSWER}

但出于某种原因,第二个块具有不同的乳胶代码,这会在编译时产生问题

\begin{ANSWER}
The Answer is only for Instructor's Eyes

\begin{enumerate}
\def\arabic{enumi}.{\arabic{enumi}.} %%% This line is different
\tightlist
\item
  The answer is hard
\item
  You won't get it right
\item
  But try anyway
\end{enumerate}
\end{ANSWER}

我收到以下错误:

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.181   T
         he answer is hard 

删除上面标记的行(使用 %%%)或用第一块中的版本替换它可以使 latex 正常编译。

如果我使用 ::: Div 块,我没有问题(乳胶代码看起来像上面第一个正确编译的代码)。

    ```{r, echo=FALSE}
    instructorVersion<-TRUE
    ```
    What is 1+1?

    ::: {.ANSWER}
    1. This answer is easy
    2. Yes, really, that's all that I was asking.
    :::

    What is the meaning of life?

    ::: {.ANSWER}
    The Answer is only for Instructor's Eyes

    1. The answer is hard
    2. You won't get it right
    3. But try anyway
    :::

但我使用的是 ```block 样式而不是 ::: .{ANSWER},因为我希望能够打开和关闭是否显示文本,如上所示。我不知道我可以用 ::: 格式做到这一点。 (我会很高兴看到一个答案,它向我展示了如何使用 ::: 版本,但像我现在这样使用 on/off 开关。)

这是我在 latex 中的环境命令,它基本上使文本变红(我认为这不重要,我把它变成了一个空的环境并得到了同样的东西,但只是为了完整性)

\newenvironment{ANSWER}{%
\envspace\itshape\color{red}

}
{}

此外,我在 RStudio 中 运行 使用以下命令:

render_book('index.Rmd',envir=new.env(),clean=FALSE,output_dir='test', output_format=c('bookdown::pdf_book') )

R Studio 1.3.959,

> sessionInfo()
R version 4.0.1 (2020-06-06)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bookdown_0.20

loaded via a namespace (and not attached):
 [1] compiler_4.0.1  magrittr_1.5    htmltools_0.5.0 tools_4.0.1    
 [5] rstudioapi_0.11 yaml_2.2.1      tinytex_0.24    stringi_1.4.6  
 [9] rmarkdown_2.3   highr_0.8       knitr_1.29      stringr_1.4.0  
[13] xfun_0.15       digest_0.6.25   rlang_0.4.7     evaluate_0.14  

我不知道你问题中的 block 引擎有什么问题,但如果你想要的只是切换内容的能力,你可以使用 the asis engine 并将 Div 在 asis 块内:

```{r, echo=FALSE}
instructorVersion <- TRUE
```
What is 1+1?

```{asis, echo=instructorVersion}
::: {.ANSWER data-latex=""}
1. This answer is easy
2. Yes, really, that's all that I was asking.
:::
```

What is the meaning of life?

```{asis, echo=instructorVersion}
::: {.ANSWER data-latex=""}
The Answer is only for Instructor's Eyes

1. The answer is hard
2. You won't get it right
3. But try anyway
:::
```

请注意data-latex="" will be required in the next version of rmarkdown