通过 Pandoc 设置几何 API
Setting geometry through Pandoc API
我有一个非常宽的 table,我将其转换为 pandoc table 格式,并使用 pandoc 命令行工具将 paperwidth 设置为 20in,如下所示(用于 pdf 输出)- 只是一个使用的原型pandoc 命令行工具:
<processed input piped to pandoc here> | pandoc -V geometry:margin=.1in
-V geometry:paperwidth=20in <other options>
有没有办法使用 Pandoc haskell api 设置这些变量?我想将 CSV 解析为 pandoc(使用 Bayhac 2014 中的 Fuel.hs
,其中还包括 pandoc 生成的默认乳胶模板),然后在设置纸张宽度后导出为 pdf(根据 pdf 中各行的最大长度计算)和利润。
使用 pandoc 作为库
参见this answer for details, but basically you use the writeLaTeX
function with the right WriterOptions
。
makePDF "xelatex" $ writeLaTeX opts document
where
opts = def {
writerVariables = [("geometry","paperwidth=20in"), ("geometry", "margin=.1in")]
}
它是如何工作的
首先,geometry
是一个模板变量,不是文档元数据的一部分(参见 this answer). As you can see in the LaTeX Writer,最终输出生成:
renderTemplate' template context
这使用了 pandoc's own templating system 中的 renderTemplate'
函数。在该文件的开头有一个广泛的示例和文档,说明如何以编程方式使用它。
但基本上是这样的:
import Text.Pandoc.Writers.Shared (defField)
import Text.Pandoc.Templates (renderTemplate')
import Data.Aeson (Value(Object))
import GHC.Exts (fromList)
let context = defField "geometry:paperwidth" "20in"
$ defField "geometry:margin" ".1in"
$ Object $ fromList []
renderTemplate' template context
有必要吗?
我仍然不确定为什么要在 haskell 代码中执行所有这些操作,因为 pandoc filters were explicitly designed to let you easily include data (like CSV with e.g. the pandoc-placetable filter) 在文档中,而不必重新实现 pandoc 及其模板系统等
现已修复。关键是以如下格式传递变量:
makePDF "xelatex" $ writeLaTeX opts document
where
opts = def {
writerVariables = [("geometry","paperwidth=30in"), ("geometry", "margin=.1in")]
}
对于Fuel.hs
,export
代码变为:
export :: (MonadIO m) => String -> Pandoc -> m (Either BL.ByteString BL.ByteString)
export tmpl pdoc = liftIO $ makePDF "xelatex"
writeLaTeX (def { writerStandalone = True, writerTemplate = tmpl,
writerVariables = [("geometry","paperwidth=30in"), ("geometry",
"margin=.1in")]}) pdoc
我已经修复了@mb21 的答案以反映修复。
我有一个非常宽的 table,我将其转换为 pandoc table 格式,并使用 pandoc 命令行工具将 paperwidth 设置为 20in,如下所示(用于 pdf 输出)- 只是一个使用的原型pandoc 命令行工具:
<processed input piped to pandoc here> | pandoc -V geometry:margin=.1in
-V geometry:paperwidth=20in <other options>
有没有办法使用 Pandoc haskell api 设置这些变量?我想将 CSV 解析为 pandoc(使用 Bayhac 2014 中的 Fuel.hs
,其中还包括 pandoc 生成的默认乳胶模板),然后在设置纸张宽度后导出为 pdf(根据 pdf 中各行的最大长度计算)和利润。
使用 pandoc 作为库
参见this answer for details, but basically you use the writeLaTeX
function with the right WriterOptions
。
makePDF "xelatex" $ writeLaTeX opts document
where
opts = def {
writerVariables = [("geometry","paperwidth=20in"), ("geometry", "margin=.1in")]
}
它是如何工作的
首先,geometry
是一个模板变量,不是文档元数据的一部分(参见 this answer). As you can see in the LaTeX Writer,最终输出生成:
renderTemplate' template context
这使用了 pandoc's own templating system 中的 renderTemplate'
函数。在该文件的开头有一个广泛的示例和文档,说明如何以编程方式使用它。
但基本上是这样的:
import Text.Pandoc.Writers.Shared (defField)
import Text.Pandoc.Templates (renderTemplate')
import Data.Aeson (Value(Object))
import GHC.Exts (fromList)
let context = defField "geometry:paperwidth" "20in"
$ defField "geometry:margin" ".1in"
$ Object $ fromList []
renderTemplate' template context
有必要吗?
我仍然不确定为什么要在 haskell 代码中执行所有这些操作,因为 pandoc filters were explicitly designed to let you easily include data (like CSV with e.g. the pandoc-placetable filter) 在文档中,而不必重新实现 pandoc 及其模板系统等
现已修复。关键是以如下格式传递变量:
makePDF "xelatex" $ writeLaTeX opts document
where
opts = def {
writerVariables = [("geometry","paperwidth=30in"), ("geometry", "margin=.1in")]
}
对于Fuel.hs
,export
代码变为:
export :: (MonadIO m) => String -> Pandoc -> m (Either BL.ByteString BL.ByteString)
export tmpl pdoc = liftIO $ makePDF "xelatex"
writeLaTeX (def { writerStandalone = True, writerTemplate = tmpl,
writerVariables = [("geometry","paperwidth=30in"), ("geometry",
"margin=.1in")]}) pdoc
我已经修复了@mb21 的答案以反映修复。