首页 > 解决方案 > 从 knitr/rmarkdown 打印裁剪图像并引入换行符

问题描述

我正在创建一个调查问卷,并希望让用户将结果保存为pdf.

看起来html不错!但是printing裁剪图像,到目前为止没有任何效果。

在此处输入图像描述

编辑:我添加了关于 pagedown 包的合适引用,可能描述了问题:

“[...] 尝试从您的一个 Rmarkdown 文件生成 PDF,我所说的 PDF 是指那种将图像从一页到另一页分成两半并且书面文本没有在页面末尾完成的文档……(是的,我知道你知道我的意思,但是……)”

# YAML
---
output: 
    pdf_document:   #html?
        fig_crop: false                         # doesn't work
---

---
output:
  pagedown::html_paged:                         # doesn't work
    css: ["default-fonts", "default-page", "default"]
---

# CSS line-breaks
<style media="print">
    html, body, p, hr, img { 
    break-after:  avoid !important;             # doesn't work
    break-before: avoid !important;             # doesn't work
    break-inside: always !important;            # doesn't work
    } 
</style>

# Button
<input type="button" value="Print this page" onClick="window.print()">
# Example Plot
     ```{r echo=FALSE, message=FALSE}
    library(ggplot2)
    Plot <- ggplot(mpg, aes(displ, cty)) + geom_point()
    Plot + facet_grid(rows = vars(drv))
    plot.height <- 45
     ```


# Calling the plot (with fig.height)
    ```{r, fig.height=plot.height, strip.white = TRUE} # doesn't work
     plot(Plot)
    ```

这是实现的示例: https ://exampleruntest.formr.org 出于某种原因,示例图没有在图之前引入换行符,而是在之后。

我希望有人有一个想法

标签: rr-markdownknitr

解决方案


回答我的问题:

  1. 图像裁剪原来是 Firefox 特定的问题。 解决方案是thisthis,而后者通过添加display:block;到 CSS对我的情况有效

  2. 通过使用解决方案用 a (覆盖默认值)包装绘图图像来删除标题后的分页符。<div><p>


推荐阅读