【问题标题】:Pandoc - Inserting pages before generated Table of ContentsPandoc - 在生成目录之前插入页面
【发布时间】:2020-06-28 04:18:58
【问题描述】:

我一直在玩 Pandoc。

有没有在生成的目录之前插入页面?

例如:

  • 标题页
  • 插入自定义页面 001
  • 插入自定义页面 002
  • (生成的)目录

非常感谢!

【问题讨论】:

标签: markdown epub pandoc tableofcontents


【解决方案1】:

我猜你想创建一个带有标题页或标题信息的 HTML 文件。解决方案是通过不止一个步骤来完成。

首先将标题页与任何其他页面一起编译为一个 html。

$ pandoc .\01_HEADER.markdown -o header.html

然后在正文之前包含该 header.html:

$ pandoc -s -S --toc -B .\header.html .\02_Document.markdown -o complete_doc.html

完成。


pandoc 帮助说明了使多部分文档成为可能的可能性,-B 和 -H 都可以工作,但我认为 -B 在我的情况下更正确。

  -H FILENAME           --include-in-header=FILENAME                    
  -B FILENAME           --include-before-body=FILENAME                  
  -A FILENAME           --include-after-body=FILENAME                  

在我的例子中,我使用样式表并得到一个完美的文档:

$ pandoc -s -S -c .\res\github-pandoc.css .\01_HEADER.markdown -o header.html
$ pandoc -s -S --toc --toc-depth=2 -c .\res\github-pandoc.css -B .\header.html .\02_Document.markdown -o complete_document.html

【讨论】:

  • -S 是什么意思?在 pandoc 文档中找不到它。
  • -S, --smart 产生印刷正确的输出,将直引号转换为弯引号,--- 转换为 em-dashes,-- 转换为 en-dashes,以及 ... 转换为省略号。在某些缩写之后插入不间断空格,例如“先生”。 (注:此选项仅在输入格式为 markdown 或 Textile 时有效。当输入格式为 Textile 或输出格式为 Latex 或 context 时自动选择,除非使用 --no-tex-ligatures。)@987654321 @这将帮助您在pandoc.org/MANUAL.html中找到它
【解决方案2】:

对于这个答案,我将假设您正在生成降价,尽管该过程对于其他文件格式也是相同的。

关键的见解是 Pandoc 使用模板来确定最终位置。它有默认模板,如果你修改它们,你可以改变部分的顺序。

  1. 找到默认模板

    > pandoc -D markdown
    $if(titleblock)$
    $titleblock$
    
    $endif$
    $for(header-includes)$
    $header-includes$
    
    $endfor$
    $for(include-before)$
    $include-before$
    
    $endfor$
    $if(toc)$
    $toc$
    
    $endif$
    $body$
    $for(include-after)$
    
    $include-after$
    $endfor$
    

    下一步您将不需要这个,但如果您对这些文件的位置感到好奇,可以询问无意义的文件类型(尽管我确信有更好的方法):

    > pandoc -D nonsense
    pandoc: Could not find data file /usr/local/.../templates/default.nonsense
    
  2. 复制并修改默认模板

    > pandoc -D markdown > modified.markdown
    

    使用您喜欢的文本编辑器,您可以打开 modified.markdown 将$body$ 放在$toc$ 之前。

    $body$
    $if(toc)$
    $toc$
    $endif$
    
  3. 使用修改后的模板

    > pandoc --template modified.markdown <... rest of your command ...>
    

【讨论】:

  • 我不确定这是 OP 要求的:他们想在 TOC 之前添加一些自定义页面,但不是整个正文,它仍应出现在之后。
  • 是的,这个答案并没有真正考虑到正文仍应 TOC 之后。我想在 Markdown 中创建一个标题页,它应该在 TOC 之前。这可能吗?
【解决方案3】:

使用你可以在pandoc --print-default-template=markdown的输出中观察到的include-before标签

---
title: My Way
toc: true
include-before: |
    See these lines
    Come __before__ the [toc](/dev/null)
---

# I've lived a life that's full
# I traveled each and every highway
# But more, much more than this
# I did it my way

【讨论】:

  • 太好了,谢谢!您还可以将 toc: true 添加到 YAML 部分,而不是添加 --toc 参数。对于多行(和多段)文本,使用include-before: |,并在后面添加一组相同缩进的行。
  • 谢谢你,@CrisLuengo,我不知道这一切。我更新了帖子。
【解决方案4】:

这些解决方案都不适合我,一个顶级评论建议使用 Pandoc 模板,但没有详细信息。所以这里有一个简单的修复方法。

  • 从这里下载默认的 Pandoc 模板:https://github.com/jgm/pandoc-templates,例如我使用了default.tex,但将其重命名为template.tex

  • 修改模板以插入更多内容、创建新页面等,例如:

    \input{title.tex}
    \newpage
    \input{acknowledgements.tex}
    \newpage
    
    $if(toc)$
    $if(toc-title)$
    \renewcommand*\contentsname{$toc-title$}
    $endif$
    
    \newpage
    
  • 使用这个修改过的模板而不是默认模板,例如

    pandoc --template=template.tex -o document.pdf document.tex`.
    

就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多