Rmarkdown - 将带有浮动 TOC 的 HTML 打印为 PDF 时小部件大小会发生变化 第 1 步:将 Rmd 渲染为 pdf第 2 步:将 html 转换为 pdf

问题描述

问题

我必须将包含浮动 TOC 的 Rmarkdown 文档呈现为 HTML 输出,然后通过 pagedown::chrome_print() 将此 HTML 转换为 PDF,这要归功于 Chromium。

我无法弄清楚如何以两种格式(html 和 pdf)将 html 小部件(此处为绘图)正确呈现为 100 宽度。这种行为似乎来自浮动 TOC。

这是Rmd的代码

test.Rmd

---
title: "My Rmarkdown"
output:
  html_document:
    toc: true
    toc_float: true
---

```{r setup,include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(plotly)
require(ggplot2)
```

```{css,echo = F}
@media print{
  .plotly.html-widget.html-widget-static-bound.js-plotly-plot {
       border: 1rem solid !important;
    }
}
```

## Title 1

```{r,out.width="100%"}
data("mtcars")
p <- ggplot(mtcars,aes(x = cyl,y = hp)) + geom_point() 
ggplotly(p)
```

## Title 2
## Title 3
## Title 4
## Title 5
## Title 6

第 1 步:将 Rmd 渲染为 pdf

rmarkdown::render(input = "test.Rmd")

第 2 步:将 html 转换为 pdf

pagedown::chrome_print(input = "test.html",output = "test.pdf")

但是输出因小部件的​​大小而不同。
在 HTML 中确定,由 R 块选项 output.width = '100%' 控制,占据容器的整个宽度。 (预期行为)
在 pdf 中不行。似乎大小已被“重新计算”。 (我加了一个边框来强调对应的div)。每当通过 CSS 中的 display: none; 打印或隐藏 TOC 时,就会发生这种情况。

test.html test.pdf

enter image description here

enter image description here

尝试

不幸的是,使用

ggplotly(p,width = ...) %>% layout(autosize = FALSE)

不是一个好的选择,因为 width 参数必须是一个绝对值,而不是像“100%”这样的相对值

我也在css chunk中尝试了几个与宽度相关的参数,应用于.plotly.html-widget.html-widget-static-bound.js-plotly-plot多重类,但这似乎没有用。

有什么想法吗?

注意:直接将 Rmd 渲染为 pdf 输出不是一种选择(它是一个非常长且复杂的 Rmd)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...