【问题标题】:bookdown html formatting issue with gitbook and split_bygitbook 和 split_by 的 bookdown html 格式问题
【发布时间】:2020-10-09 17:16:23
【问题描述】:

我正在用 Rstudio 的 bookdown 包生成一个 gitbook 报告。

就底层 R 代码而言,它相当简单,只是最近对以下方面进行了一些调整:

  1. 将文本缩小到页面宽度的 80%
  2. 使用双列和
  3. 在显示的 R 代码中添加行号。

一切正常,除了我在 _output.yml 中添加了“split_by: rmd”。这样做时,结果输出不再尊重文本周围的边距。

我对 html 还不太了解,但是查看 html 检查器显示,当使用“split_by: rmd”时,这些部分位于内页格式之外

默认(无 split_by 参数):

使用 split_by: rmd

这是在黑暗中拍摄的,因为我无法共享代码,也无法通过 Yihui 的最小 bookdown 示例重现错误:https://github.com/rstudio/bookdown-demo

非常欢迎任何能够确定错误来源甚至更好地提出解决方案的线索!

从 R 脚本构建书籍:

bookdown::render_book(
  input = "index.Rmd",
  output_format = "bookdown::gitbook",
  output_dir = paste0("gitbook-", format(Sys.time(), format = "%Y-%m-%d-%H%M%S"))
)

index.rmd YAML 标头:

---
title: "blahblah"
subtitle: "blahblahblah"
author: "DRAFT"
date: "August 2020"
documentclass: article
fontsize: 12pt
geometry: margin=2cm
link-citations: yes
#mainfont: Arial
bibliography: packages.bib
site: bookdown::bookdown_site
biblio-style: apalike
urlcolor: blue
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE, eval = FALSE, attr.source='.numberLines')

table_format <- knitr::opts_knit$get('rmarkdown.pandoc.to')

if (table_format %in% c("html", "latex")) {
  library(kableExtra)
  knitr::opts_chunk$set(fig.pos='H', fig.align='center', out.width='80%')
}

## Automatically create a bib database for R packages
knitr::write_bib(c(.packages(), 'bookdown', 'knitr', 'rmarkdown', 'Hmisc'), 'packages.bib')
```

_output.yml:

bookdown::gitbook:
      css: style.css
      config:
        toc:
          before: |
            <li><a href="./">My book title</a></li>
          #after: |
          #  <li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
        edit: null
        download: null
        sharing: null
        info: null
      split_bib: FALSE
      split_by: rmd

style.css:

p.caption {
  color: #777;
  margin-top: 10px;
}
p code {
  white-space: inherit;
}
pre {
  word-break: normal;
  word-wrap: normal;
}
pre code {
  white-space: inherit;
}

/* watermark for draft report
.watermark {
  opacity: 0.2;
  position: fixed;
  top: 45%;
  left: 45%;
  font-size: 500%;
  color: #606099;
  z-index: 1000000;
}
*/

.book .book-body .page-wrapper .page-inner {
  max-width: 80% !important;
}

/* Increase space to display line number in R chunks correctly */
pre.numberSource code > span > a:first-child::before {
  left: -0.3em;
}

/* for multi cols */
/*.cols {display: flex; } /* uncomment for flex column size */
.cols {display: grid; grid-template-columns: 30% 50% 20%;} /* for fixed column size */

【问题讨论】:

    标签: html r formatting bookdown


    【解决方案1】:

    我发现问题的根源是我在每个rmd文件开头添加的\newpage,用于构建pdf格式的报告。

    解决方案 1:将 split_by: rmd 更改为 split_by: section,因为我的 rmd 文件主要对应于 2 级部分。

    解决方案 2:在 \newpage 周围放置一个包装器,以便在输出为 html 时不会评估它们:

    `r if (knitr::opts_knit$get('rmarkdown.pandoc.to') != "html") '
    \\newpage
    '`
    

    此处描述了解决方案 2 的其他方法: How to add \newpage in Rmarkdown in a smart way?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2020-12-10
      相关资源
      最近更新 更多