pandoc - pandoc HTML 模板中的相对路径

标签 pandoc

我有一个从默认加载的 Pandoc (v1.19.2.1) HTML5 模板 --data-dir .在模板中,我需要加载外部资源,例如样式表和 JavaScript。我想相对于模板的路径加载这些资源,而不是工作目录或源文件。例如,在 macOS 上,在 ~/.pandoc/templates/hierarchical/hierarchical.html :

…
<link rel="stylesheet" href="hierarchical.css">
…

哪里hierarchical.css位于 ~/.pandoc/templates/hierarchical/hierarchical.css , 在与模板本身相同的目录中。

然后从命令行调用:
pandoc \
  --from=markdown_strict+header_attributes+yaml_metadata_block+pipe_tables\
  --to=html5 \
  --self-contained \
  --template="hierarchical/template.html" \
  --section-divs \
  --output="$1.html" \
  --toc \
  --toc-depth=6 \
  "$1.md"

我收到错误:
pandoc: Could not fetch hierarchical.css
hierarchical.css: openBinaryFile: does not exist (No such file or directory)

我已经尝试了各种其他的 CSS 文件相对路径。唯一有效的是绝对路径 /Users/jmakeig/.pandoc/templates/hierarchical/hierarchical.css ,当然,这只能在我的笔记本电脑上使用。

有没有什么方法可以解决 Pandoc 模板中相对于模板本身的外部资源,以便模板是可移植的?我没有看到可以在模板或命令行选项中使用的明显外部变量。

最佳答案

我正在粘贴我为 created in github 提供的问题的解决方法不久以前。

在我创建这个问题两年多后回到这个话题,我发现了一个不错的解决方法。

显然, latex 使用环境变量 TEXINPUTS作为资源的排序路径。因此,您只需在系统(linux、windows 等)中配置一次环境变量,然后引用与该路径相关的资源即可。

此链接提供了有关如何使用它的一些说明:
https://tex.stackexchange.com/questions/93712/definition-of-the-texinputs-variable

例如我有以下文件:

SOME_PATH_TO/templates/my_latex_template.tex
SOME_PATH_TO/templates/img/my_img.png

在我的系统中,我设置了环境变量(以 Windows 为例,虽然我实际上只是将它保存在系统配置下):
set TEXINPUTS=SOME_PATH_TO/templates/

在模板my_latex_template.tex我有类似的东西:
%...
\includegraphics{img/my_img.png}
%...

我这样称呼模板:
pandoc file.txt -t pdf --template=SOME_PATH_TO/templates/my_latex_template.tex --output=output.pdf

关于pandoc - pandoc HTML 模板中的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47271344/

相关文章:

docx - pandoc 跳过 latex 环境

pandoc - 如何更改 pandoc 的 markdown 到 html 转换的输出

r - 使用pandoc.table()减少表格的单元格宽度和字体大小

python - 将嵌入的 Excel 对象从 do​​cx 文件转换为图像

html - Rmarkdown HTML 模板产生 pandoc 错误 61

markdown - 在HTML页面的底部或底部是否有pandoc脚注?

yaml - RMarkdown的YAML中的作者和隶属关系

linux - pcmanfm-qt 自定义操作中的多个命令?

markdown - pandoc:未知读者:gfm

rstudio - Rmarkdown 编织错误 : Fault/access violation in generated code