我正在使用 HTML 格式的四开本写论文,其中有一些动态内容(传单 map 、动态图表)。然而,最终,我需要将这本书导出为 PDF/LaTeX,或者至少导出为 Word(然后我可以复制并粘贴到 LaTeX 中)。
当我尝试导出为 PDF 时,我当然遇到了这个错误:
Functions that produce HTML output found in document targeting pdf output. Please change the output type of this document to HTML. Alternatively, you can allow HTML output in non-HTML formats by adding this option to the YAML front-matter of your rmarkdown file:
always_allow_html: true
Note however that the HTML output will not be visible in non-HTML formats.
我确实尝试在我的 YAML 文件中添加 always_allow_html: true
,但我得到了完全相同的错误。我也试过 conditional rendering使用 {.content-hidden unless-format="pdf"}
,但我似乎无法让它工作。
有人遇到过同样的问题吗?
最佳答案
使用 .content-visible when-format="html"
和 .content-visible when-format="pdf"
工作非常顺利。
---
title: "Conditional Rendering"
format:
html: default
pdf: default
---
## Conditional Content in Quarto
::: {.content-visible when-format="html"}
```{r}
#| message: false
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point(aes(colour = factor(cyl)))
ggplotly(p)
```
```{r}
#| message: false
#| fig-pos: "H"
#| fig-width: 4
#| fig-height: 3
library(leaflet)
# took this example from leaflet docs
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
```
:::
::: {.content-visible when-format="pdf"}
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point(aes(colour = factor(cyl)))
p
```
:::
关于r-markdown - 如何 PDF 呈现具有动态内容的四开本书籍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73551667/