【问题标题】:Bookdown generates index file with a chapter title instead of "index.html" when knittedBookdown在编织时生成带有章节标题而不是“index.html”的索引文件
【发布时间】:2020-02-08 23:05:20
【问题描述】:

我正在使用 Bookdown 将一份长报告编译成一个 html 文档,并且直到最近使用 bookdown::gitbook 编织它并没有任何问题。不知道发生了什么。

代码运行没有错误,所有的绘图和文件都是根据需要生成的,但是 index.rmd 被生成为以第一个标题的 id(“chapter00.html”)命名的 .html 文件,而不是“index.html”。 html”。这意味着网站在上传到 github 时无法运行。

如果手动将文件名更改为“index.html”,一切都会很好。

我的 index.rmd 文件如下所示:

--- 
title: "Eesti inimarengu aruanne 2019"
author: "Eesti Koostöö Kogu"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [bibliography/bib_chapter23.bib, bibliography/bib_chapterXY.bib]
biblio-style: authoryear
biblatexoptions: [refsegment=chapter]
csl: keel-ja-kirjandus.csl
link-citations: yes
description: "Eesti inimarengu aruanne 2019"
css: eia.css
lang: et
---

# Sissejuhatus {-#chapter00 .chapter_section .intro_section}

_bookdown.yml 文件如下:

book_filename: "EIA_2019_digi"
language:
  label:
    fig: 'Joonis '
    tab: 'Tabel '
    eq: 'Valem '
  ui:
    chapter_name: ""
delete_merged_file: true

_output.yml如下:

bookdown::gitbook:
  split_by: section
  split_bib: yes
  config:
    toc:
      collapse: subsection
      scroll_highlight: yes
      before: null
      after: null
      toolbar:
      position: fixed
      edit : null
      download: null
      search: yes
    sharing:
      facebook: no
      twitter: no
      google: no
      linkedin: no
      weibo: no
      instapaper: no
      vk: no
      all: ['facebook', 'google', 'twitter', 'linkedin', 'weibo', 'instapaper']
bookdown::pdf_book:
  includes:
    in_header: preamble.tex
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: yes
bookdown::epub_book: default

对可能导致问题的原因有任何见解吗?谢谢!

【问题讨论】:

  • 嗨,# Sissejuhatus {-#chapter00 .chapter_section .intro_section} 不应该在 index.rmd 中吗?
  • 嗨!感谢你的回复!我认为这不是问题,因为您应该能够将内容添加到索引文件中。 Bookdown 演示文件中的内容可在此处获得:link
  • 嗨,您找到解决方案了吗?
  • 嘿!不,我还没有。

标签: r r-markdown knitr bookdown


【解决方案1】:

当我从不在项目目录中的位置包含index.Rmd 时,这也发生在我身上。移动文件解决了命名问题。

例如,我最初将文件放在子目录中(例如,doc/index.Rmd)并将其包含在_bookdown.ymlrmd_files: ["doc/index.Rmd", "doc/introduction.Rmd"] 中。这导致输出文件为introduction.html

index.Rmd 移动到根目录并使用rmd_files: ["index.Rmd"] 导致输出文件为index.html

请注意,似乎从_bookdown.yml 中删除index.Rmd(即rmd_files: ["doc/introduction.Rmd"])也会导致输出introduction.html

【讨论】:

    【解决方案2】:

    如果第一章 (index.Rmd) 有多个一级标题 (#),也会发生这种情况

    【讨论】:

      【解决方案3】:

      我在一个项目中遇到了这个问题,我使用这种方法包含了用 .md 文件编写的章节:

      _bookdown.yml:

      book_filename: "PGDEPhysics"
      language:
        ui:
          chapter_name: ""
          appendix_name: "Appendix "
      delete_merged_file: true
      output_dir: "docs"
      
      rmd_files: [
        'index.Rmd'
        ,'content.Rmd'
        ,'appendix.Rmd'
        ,'bibliography.Rmd'
      ]
      
      

      文件content.Rmd

      ```{r content, echo=FALSE}
      library(fs)
      cvec = dir_ls("Content", regexp = "^(.+).md$")
      ```
      ```{r child = cvec}
      ```
      

      如果Content 文件夹中的第一个文件以顶级标题开头:

      # The first chapter
      

      一切正常,bookdown 根据需要输出一个index.html 文件。如果第一个标题第一个文件以子标题开头:

      ## The first chapter
      

      然后我们在输出中看到这个:

      Output created: docs/welcome.html
      

      而不是必需的:

      Output created: docs/index.html
      

      ('Welcome' 是index.Rmd 中的第一个顶级标题)

      对我来说,解决方案就是接受它,并让第一个 .md 文件以顶级标题开头。

      此功能的困难在于,除非您在每次构建本书时清除目标文件夹(在我的情况下为docs),否则在引入它时不会注意到问题,因为旧版本的index.html 可能会保留在那里,并且该站点在开发过程中看起来不错。构建过程只会添加文件,不会从目标文件夹中删除它们。

      因此,最佳实践工作流程是在构建之前删除目标文件夹。

      【讨论】:

        【解决方案4】:

        我遇到过这个问题,而且我实际上可以以特定方式重现它,这似乎与您的不完全相同。如果我index.Rmd 文件中保留任何实际内容或markdown 标头,则呈现的HTML 文件将以下一个Rmd 文件中的第一个标头命名。如果我在index.Rmd 文件中只放一个标题,那么它会呈现为 index.html

        【讨论】:

          【解决方案5】:

          问题是您正在编织您的 bookdown 项目,而您应该使用 bookdown 渲染它。在 R 控制台中:

          bookdown::render_book("index.Rmd")
          

          检查输出的最后一行是否类似于

          Output created: docs/index.html
          [1] "/my/path/mybook/docs/index.html"
          

          这应该会从您的 index.rmd 文件生成一个 index.html 文件

          (如果您使用 github.io 发布,请记住在 Settings > Github Pages 下设置 Source ma​​ster branch/docs 文件夹

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-02-06
            • 2020-04-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-03-06
            相关资源
            最近更新 更多