knitr::include_graphics() 不使用 `bookdown` 渲染图形

knitr::include_graphics() doesn't render figure with `bookdown`

我想写一份报告,只是想在我的报告中包含一张 .jpeg 图片。但是,每当我将我的文档编织在一起时,我都会收到以下错误:

! Missing $ inserted.
<inserted text> 
                $
l.128 ...tput/figures/chapter1/top100/top100_2000}

Try to find the following text in chp1.Rmd:
  ...tput/figures/chapter1/top100/top100_2000} 

You may need to add $ $ around a certain inline R expression `r ` in chp1.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Error: LaTeX failed to compile chp1.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See chp1.log for more info.
Execution halted

我不知道这里发生了什么。我以为是我使用了 here 包,但是当我改用整个文件路径时,它给了我同样的错误。我 运行 只是代码块,它加载了图像,所以这让我觉得我有一些设置不允许正确读取 R 块的输出,但我无法弄清楚。有什么建议吗?

这是 YAML header:

---
title: "Chapter 1: "
author: "Kasey Zapatka"
date: " lasted edited: `r format(Sys.time(), '%B %d, %Y')`"
output:
  bookdown::pdf_document2:
    toc: no
    fig_caption: yes
    latex_engine: xelatex
    keep_tex: true
bibliography: "references/references.bib"
link-citations: yes
fontsize: 12pt
header-includes:
    - \usepackage{rotating}
    - \usepackage{setspace}
    - \newcommand{\beginsupplement}{\setcounter{table}{0}  \renewcommand{\thetable}{A\arabic{table}} \setcounter{figure}{0} \renewcommand{\thefigure}{A\arabic{figure}}}
    - \usepackage{lineno}
    - \linenumbers
abstract: |
  This chapter describes the trends and patterns in the key variables in my analysis. First, I identify the top 100 metro areas by population in 2019, which serves as the unit of analysis for this chapter. I look at the changes over time, both in rank and relative population growth. Next, I investigate gentrification patterns in the top 100 metro areas in both 2013 and 2019, as well as change over time. I specifically focus on the patterns in metros where they see the most intense gentrification. I find.. Finally, I review patterns of rent and mortgage burden in the top 100 metros across the country, in 2019 and change over time. I find strikingly different patterns of burden growth by tenure status. 
---

设置代码块:


# chunk options ----------------------------------------------------------------
knitr::opts_chunk$set(
  fig.align = "center",        # center align figures
  warnings = FALSE,            # prevents warnings from appearing in code chunk
  message = FALSE,             # prevents messages from appearing in code chunk
  echo = FALSE,                 # do not echo code in file
  results = "markup",            # hide results
  fig.align = "center",        # center align figures
  fig.keep = "all"             # keep all figs
)


# load all packages using xfun, package2 will install if not available
xfun::pkg_attach2(c("tidyverse",
                    "tidycensus",
                    "tmap",
                    "tigris",
                    "sf",
                    "here",
                    "haven",
                    "ggalt",
                    "ggExtra",
                    "extrafont",
                    "showtext", 
                    "scales", 
                    "ggalluvial",
                    "kableExtra"))



# set relative file path to project directory
here()



# environment settings ---------------------------------------------------------

# set environment options
options(scipen=999)

## Loading Google fonts (https://fonts.google.com/)
font_add_google("Lato", "lato")
showtext_auto()

R 代码块:



knitr::include_graphics(here("output/figures/chapter1/top100/top100_2000.jpeg"))

我遇到了同样的问题,看来您需要传递绝对路径。参见

而不是使用 here 可以尝试在 include_graphics 中使用 normalizePath

所以省略你的调用 here(),它可能不会跨块传播(我不知道,因为我看不到你的块),然后做:

knitr::include_graphics(normalizePath("output/figures/chapter1/top100/top100_2000.jpeg"))

在 Windows 我添加 winslash = "/":

knitr::include_graphics(normalizePath("output/figures/chapter1/top100/top100_2000.jpeg", winslash = "/"))