最近我开始使用Pandoc Markdown,它似乎是LaTeX的一个很好的替代品,因为我的文档没有很多数学公式,并且我没有任何关于LaTeX的经验,再加上不到2周的提交截止日期,这是一个很好的解决方案。
但有一件事情我还没有解决,就是如何强制让它留下页面剩余部分为空,请问是否有人可以帮忙解决?
看起来 pandoc markdown 使用标准 LaTeX 标签来实现这一目的:
\newpage
和 \pagebreak
TL;DR: 使用Lua过滤器中的\newpage
或\pagebreak
(或者这里)可以在许多格式中获得分页。对于R Markdown用户,无需进行任何额外操作,该过滤器已经包含在默认设置中。
\newpage
。当输出LaTeX(或通过LaTeX创建的PDF)时,这种方法非常有效。但是,如果目标格式不同,例如HTML或docx,则会遇到问题。\newpage
来指示分页符:lorem ipsum
\newpage
more text
\newpage
将被解析为包含原始TeX的RawBlock。只有目标格式可以包含原始TeX(即LaTeX、Markdown、Org等)时,该块才会包含在输出中。--- Return a block element causing a page break in the given format.
local function newpage(format)
if format == 'docx' then
local pagebreak = '<w:p><w:r><w:br w:type="page"/></w:r></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
elseif format:match 'html.*' then
return pandoc.RawBlock('html', '<div style=""></div>')
elseif format:match 'tex$' then
return pandoc.RawBlock('tex', '\\newpage{}')
elseif format:match 'epub' then
local pagebreak = '<p style="page-break-after: always;"> </p>'
return pandoc.RawBlock('html', pagebreak)
else
-- fall back to insert a form feed character
return pandoc.Para{pandoc.Str '\f'}
end
end
-- Filter function called on each RawBlock element.
function RawBlock (el)
-- check that the block is TeX or LaTeX and contains only \newpage or
-- \pagebreak.
if el.text:match '\\newpage' then
-- use format-specific pagebreak marker. FORMAT is set by pandoc to
-- the targeted output format.
return newpage(FORMAT)
end
-- otherwise, leave the block unchanged
return nil
end
latex
转换为docx
,您必须将源语言设置为latex+raw_tex
,以便pandoc AST将其传递github问题。\newpage
来进行分页。要使用此过滤器,请将此答案中的代码保存为例如 pagebreak.lua
,然后通过指定参数--lua-filter=pagebreak.lua
来调用pandoc。 - Christian Long我发现这种方法不适用于.doc和.odt格式。我找到的一个解决方法是在文本编辑器(例如我的LibreOffice)中插入一条水平线-----------------
并将“水平线”样式设置为分页后不可见。
hr {opacity:0;page-break-after: always;}
可以胜任此工作。如果您想将<hr>
用于其他用途,则可以牺牲其他元素。 - Joaquin无法编辑LucasSeveryn的答案,提示队列已满,因此在此添加一些信息。
\newpage
和 \pagebreak
需要开启 raw_tex
扩展。
// 使用 pandoc 2.9.2.1,不能与 docx 或 html 输出一起使用,--verbose 显示如下
[INFO] Not rendering RawBlock (Format "tex") "\\pagebreak"
[INFO] Not rendering RawBlock (Format "tex") "\\newpage"
https://pandoc.org/MANUAL.html#extension-raw_attribute
```{=openxml}
<w:p>
<w:r>
<w:br w:type="page"/>
</w:r>
</w:p>
```
// 在gfm输入格式中也不支持。
// 这适用于docx输出,但不适用于html输出。
这需要+raw_tex
格式扩展。
这并不适用于pandoc中的所有markdown变体。
https://pandoc.org/MANUAL.html#markdown-variants
Note, however, that commonmark and gfm have limited support for extensions.
Only those listed below (and smart, raw_tex, and hard_line_breaks) will work.
The extensions can, however, all be individually disabled.
Also, raw_tex only affects gfm output, not input.
所以 -f markdown
是有效的,但是 -f gfm
无效。
https://pandoc.org/MANUAL.html#option--from
Extensions can be individually enabled or disabled by appending
+EXTENSION or -EXTENSION to the format name.
-t html+raw_tex
:输出启用 raw_tex。
-f markdown-raw_tex-raw_attribute
:输入禁用 raw_tex 和 raw_attribute。
<div style="page-break-before:always;"></div>