简体   繁体   English

gitbook 和 split_by 的 bookdown html 格式问题

[英]bookdown html formatting issue with gitbook and split_by

I am generating a gitbook report with Rstudio the bookdown package.我正在生成一个 gitbook 报告,其中包含 Rstudio bookdown package。

It is fairly simple in terms of underlying R code, just a few recent tweaks for:就底层 R 代码而言,它相当简单,只是最近的一些调整:

  1. reducing text to 80% of page width将文本缩小到页面宽度的 80%
  2. using double columns and使用双列和
  3. adding line number in the R code displayed.在显示的 R 代码中添加行号。

Everything works well, except when I added " split_by: rmd " in the _output.yml.一切正常,除非我在 _output.yml 中添加了“ split_by: rmd rmd”。 When doing so the resulting output doesn't respect the margin around the text anymore.这样做时,生成的 output 不再考虑文本周围的边距。

I don't know much about html yet, but looking at the html inspector revealed that the sections are located outside the inner-page formatting when using " split_by: rmd "我对 html 还不太了解,但是查看 html 检查器发现使用“ split_by: rmd ”时这些部分位于内页格式之外

Default (no split_by argument):默认(没有 split_by 参数): 默认(不是 split_by 参数)

With split_by: rmd使用 split_by: rmd 与 split_by: rmd

This is a shot in the dark as I cannot share the code and I am not able to reproduce the error with the minimal bookdown example from Yihui: https://github.com/rstudio/bookdown-demo .这是在黑暗中拍摄的,因为我无法共享代码,而且我无法使用 Yihui 的最小 bookdown 示例重现错误: https://github.com/rstudio/bookdown-demo

Any leads to identify the origin of the error or even better propose a solution would be very welcomed!非常欢迎任何能够识别错误来源或更好地提出解决方案的线索!

Building the book from a R script:从 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 header:指数.rmd YAML header:

---
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: _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:样式.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 */

I found that the origin of the problem was the \newpage I added at the beginning of each rmd file for building the report in pdf format.我发现问题的根源是我在每个 rmd 文件开头添加的\newpage newpage,用于以 pdf 格式构建报告。

Solution 1: was to change split_by: rmd to split_by: section as my rmd files correspond mostly to level 2 sections.解决方案 1:将split_by: rmd rmd 更改为split_by: section ,因为我的 rmd 文件主要对应于 2 级部分。

Solution 2: was to put a wrapper around \newpage so that they are not evaluated when the output is html:解决方案 2:是在\newpage周围放置一个包装器,以便在 output 为 html 时不对其进行评估:

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

Other approaches to solution 2 have been described here: How to add \newpage in Rmarkdown in a smart way?此处描述了解决方案 2 的其他方法: How to add \newpage in Rmarkdown in a smart way?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM