【问题标题】:How to resolve rmarkdown to latex errors involving tikz devices如何解决 rmarkdown 到涉及 tikz 设备的乳胶错误
【发布时间】:2020-02-12 13:21:55
【问题描述】:

我正在尝试将包含地理图的rmarkdown 文档编译为 PDF 文件。这是一个 MWE:

---
title: "Problems with maps in tikz"
output: pdf_document
---

```{r setup, include=FALSE}
library(ggplot2)
library(sf)
```

## sf:png
Builds find when `nctikz` chunk is excluded.
```{r nc}
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
ggplot(nc) +
  geom_sf(aes(fill = AREA))
```

## sf:tikz
This graphic fails to build.
```{r nctikz, dev = 'tikz'}
ggplot(nc) +
  geom_sf(aes(fill = AREA)) 
```

我正在使用 RStudio 中的“Knit”按钮编译此文档。我将讨论我在编译文档时遇到的每一个错误/警告,以防一个导致另一个。

小特克斯

每次我尝试运行包含dev = 'tikz' 的块时,tinytex 都会尝试重新安装pgf TeX 包,然后发现它已经存在,然后放弃尝试链接它。但是,我能够使用非地理 tikz 输出构建文档,所以我基本上只是接受了这个事实。

tlmgr search --file --global '/tikzlibrarytopaths.code.tex'
Trying to automatically install missing LaTeX packages...
tlmgr install pgf
tlmgr: package repository http://mirror.utexas.edu/ctan/systems/texlive/tlnet (not verified: gpg unavailable)
tlmgr install: package already present: pgf
tlmgr path add
add_link_dir_dir: /usr/local/share/info/dir exists; not making symlink.

无效字符

Tikz(在 knitr 中)似乎无法将度数符号作为 tex 文件的一部分来处理。请注意,nctikz-1.tex 对象可以由pdflatex 构建而不会出现问题。

! Package inputenc Error: Invalid UTF-8 byte "B0.

Quitting from lines 24-27 (test.Rmd) 
Error: Failed to compile test_files/figure-latex/nctikz-1.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See nctikz-1.log for more info.

文件路径下划线

我可以通过在我的绘图中抑制轴标题theme(axis.text = element_blank()) 来解决无效字符错误,但这会引入另一个处理引入文件的错误。

! Missing $ inserted.
<inserted text> 
                $
l.3975 ...72.27pt,interpolate=true]{nctikz-1_ras1}
                                                  }; 

Quitting from lines 24-27 (test.Rmd) 

产生这个错误的整行是

\node[inner sep=0pt,outer sep=0pt,anchor=south west,rotate=  0.00] at (423.16, 120.24) {
    \pgfimage[width= 14.45pt,height= 72.27pt,interpolate=true]{nctikz-1_ras1}};

它试图引用的图像几乎不是图像(我认为它是为了传说)。

环境

我正在使用一大堆库在 MacOS 上构建此文档。为了隔离这个问题,我还尝试在一个全新的项目中在 Rstudio.cloud (Ubuntu) 上构建文档。在这种环境下,只会出现数学环境/下划线的问题。

! Missing $ inserted.
<inserted text> 
                $
l.4049 ...72.27pt,interpolate=true]{nctikz-1_ras1}
                                                  }; 

Quitting from lines 26-28 (test.Rmd) 
Error: Failed to compile test_files/figure-latex/nctikz-1.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See nctikz-1.log for more info.

【问题讨论】:

    标签: r-markdown tinytex tikzdevice


    【解决方案1】:

    不是一个完整的答案,但我想记录一下我目前的状态:

    1. 通过添加启用调试是有意义的

      ```{r, include=FALSE}
      options(tinytex.verbose = TRUE)
      ```
      

      到Rmd 文件as suggested。

    2. pgf 问题有点像红鲱鱼。通过调试,我们看到它发生在发生其他一些错误(文件路径中的非法字符或下划线)之后。对应的正则表达式是recently introduced,应该可以扩展以检查来自 Tikz/pgf 的实际错误消息。

    3. 我可以在 Debian Linux 上重现非法字符。不知何故,° 被写成0xB0,即用 Latin-1 编码而不是 UTF-8。我不确定为什么/在哪里发生这种情况。顺便说一句,我可以直接使用 RStudio 中的 pdflatex only 处理生成的 tex 文件,因为这会将 0xB0 重新编码为问号。如果我通过 Emacs 或直接在命令行上使用 pdflatex,我会收到相同的错误消息。

    4. 从输出中删除度数符号

      ```{r nctikz, dev = 'tikz'}
      ggplot(nc) + 
        theme(axis.text = element_blank()) +
        geom_sf(aes(fill = AREA)) 
      ```
      

      我可以重现“文件路径中的下划线”问题。打开调试后,您会发现问题又出在其他地方:

      Package pgf Warning: File "nctikz-1_ras1" not found when defining image "pgflas
      timage". Tried all extensions in ".pdf:.jpg:.jpeg:.png:" on input line 3975.
      
      ! Missing $ inserted.
      <inserted text> 
                      $
      l.3975 ...72.27pt,interpolate=true]{nctikz-1_ras1}
                                                        };
      !  ==> Fatal error occurred, no output PDF file produced!
      

      找不到与nctikz-1.tex 位于同一目录中的文件nctikz-1_ras1.png。可以使用直接重现此问题

      tinytex::latexmk("<path>/nctikz-1.tex", install_packages = FALSE, clean = FALSE)
      

      但是,如果先更改目录,则不会出现此类错误:

      setwd("<path>")
      tinytex::latexmk("./nctikz-1.tex", install_packages = FALSE, clean = FALSE)
      

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-02
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多