XiangyunHuang/ElegantBookdown

bookdown.post.latex 里正则替换可能部分失效了

XiangyunHuang opened this issue · 15 comments

今天准备将测试环境切换到 Github Action,本地编译项目的时候,突然发现已经编译不过了,报如下的错误:

! Extra \fi.
l.372 \textbackslash iffalse\{\} {证明 } \fi
                                          {}The characteristic function of \...

通过查看编译 Rmd 文件生成的中间文件 elegantbookdown.tex 发现「证明、注、练习、解」几个环境多出了一些东西

\textbackslash iffalse\{\} {证明 } \fi{}

\textbackslash iffalse\{\} {注 } \fi{}

\textbackslash iffalse (Characteristic Function of the Sample Mean) \fi{}

查看 bookdown.post.latex 感觉是正则替换没有起作用,@fyemath 能帮忙看看吗?

# https://github.com/fyemath/elegantbookdown-demo/blob/6e2e6e7afd0bba80312f47bc5c4ad93f92c050fe/index.Rmd#L18-L36
options(bookdown.post.latex = function(x) {
  # x is the content of the LaTeX output file
  # The following codes is another way to removes amsthm environments

  start_row <- grep("package\\{amsthm", x)
  end_row <- grep("newtheorem\\*\\{solution", x)
  x <- x[-c(start_row:end_row)]

  # Remove \iffalse \fi\\
  x <- gsub("^\\\\iffalse.\\\\fi\\{\\}+?(\\\\)", "", x)
  x <- gsub("^\\\\tightlist", "", x)
  # fix theorem environments for elegantbook. Remove or modify if other book classes are using!!!
  x <- gsub("(^\\\\BeginKnitrBlock\\{)+(definition|axiom|theorem|lemma|corollary|proposition|postulate)+(\\}$)", "\\1\\2\\3\\{\\}\\{\\}", x)
  x <- gsub("(^\\\\BeginKnitrBlock\\{)+(definition|axiom|theorem|lemma|corollary|proposition|postulate)+(\\})+\\[(.*)\\]", "\\1\\2\\3\\{\\4\\}\\{\\}", x)
  x <- gsub("(^\\\\\begin\\{)+(definition|axiom|theorem|lemma|corollary|proposition|postulate)+(\\}$)", "\\1\\2\\3\\{\\}\\{\\}", x)
  x <- gsub("(^\\\\begin\\{)+(definition|axiom|theorem|lemma|corollary|proposition|postulate)+(\\})+(.*)", "\\1\\2\\3\\{\\4\\}\\{\\}", x)
})

运行环境

xfun::session_info(packages = c('knitr','rmarkdown','bookdown'))
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.6, RStudio 2021.9.0.351

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  base64enc_0.1.3 bookdown_0.24   digest_0.6.28   evaluate_0.14   fastmap_1.1.0  
  glue_1.4.2      graphics_4.1.1  grDevices_4.1.1 highr_0.9       htmltools_0.5.2
  jquerylib_0.1.4 jsonlite_1.7.2  knitr_1.36      magrittr_2.0.1  methods_4.1.1  
  rlang_0.4.11    rmarkdown_2.11  stats_4.1.1     stringi_1.7.4   stringr_1.4.0  
  tinytex_0.34    tools_4.1.1     utils_4.1.1     xfun_0.26       yaml_2.2.1     

Pandoc version: 2.14.2

新版的 bookdown 里加了一个取消默认定理环境的开关

options(bookdown.theorem.preamble = FALSE)

把正则替换的改成这个就可以了。
你再运行一下试试。

替换了,证明等环境还是不行,我把它们从正文中给去掉了

替换了,证明等环境还是不行,我把它们从正文中给去掉了

我试了好像没有问题啊。你是说 Github Action 不行还是本地不行?另外我没有adobe的字体,所以全部换成有的字体了。

替换了,证明等环境还是不行,我把它们从正文中给去掉了

我试了好像没有问题啊。你是说 Github Action 不行还是本地不行?另外我没有adobe的字体,所以全部换成有的字体了。

都不行

@fyemath 我把 repo 转移给你吧,我感觉自己对付不了 LaTeX 😭

@fyemath 我把 repo 转移给你吧,我感觉自己对付不了 LaTeX 😭

不用转给我。我可以直接编辑。另外,可以 fork 了,试过再给你 pull request。

刚安装了 Noto 的字体试了,本地生成 PDF 没有问题。

session 的信息如下。忘记说了,我是在 windows 上试的。

xfun::session_info(c("rmarkdown", "bookdown", "knitr"), dependencies = FALSE)
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043), RStudio 1.4.1717

Locale:
  LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
  LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
  LC_TIME=English_United States.1252    
system code page: 936

Package version:
  bookdown_0.24  knitr_1.34     rmarkdown_2.11

Pandoc version: 2.12

我想我找到问题了。安装到 user 账户下的字体,TinyTeX 需要手动添加路径到 font.conf 才能被搜索到。Github 系统里用 Songti SC, Heiti SC, PingFang SC,以及 Hiragino Sans GB 这几款中文字体可用。试了一下,好像 Github Action 可以过去。

需要怎么编辑 font.conf 呢?可以举个例子吗?

需要怎么编辑 font.conf 呢?可以举个例子吗?
可以参加 https://tex.stackexchange.com/questions/45478/how-to-make-xelatex-recognize-linked-fonts-with-windows

刚看到另外一个方法,原来 setmainfont 可以指定路径。我回头试试。如果你有时间也可以试试。详情参见
https://tex.stackexchange.com/questions/320096/xelatex-fontspec-cannot-find-fonts

用上 Noto CJK 了。另外给定理环境加了 CSS Styles。

厉害👍,CSS 定理等 样式和LaTeX定理等的配色保持一致就更完美了

厉害👍,CSS 定理等 样式和LaTeX定理等的配色保持一致就更完美了

昨天太累了。有空的时候,再修改。准备把几个颜色主题都加上去。用 js 实现一键替换。

期待 🎉 🎉

定理环境也基本上和elegantbook一样了。还有些细微差别,回头再处理吧。

好的👌