gpt4 book ai didi

r-markdown - 如何解决涉及 tikz 设备的 rmarkdown to latex 错误

转载 作者:行者123 更新时间:2023-12-05 03:57:43 25 4
gpt4 key购买 nike

我正在尝试将包含地理绘图的 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' 的 block 时,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}};

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

<code>nctikz-1_ras1.png</code>

环境

我在 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.

最佳答案

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

  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 处理生成的 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)

关于r-markdown - 如何解决涉及 tikz 设备的 rmarkdown to latex 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58401825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com