Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative paths in pandoc HTML templates

Tags:

pandoc

I‘ve got a Pandoc (v1.19.2.1) HTML5 template that I’m loading from the default --data-dir. Within the template I need to load external resources, such as stylesheets and JavaScript. I’d like to load those resources relative to the path of the template, not the working directory or the source file. For example, on macOS, in ~/.pandoc/templates/hierarchical/hierarchical.html:

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

where hierarchical.css is located at ~/.pandoc/templates/hierarchical/hierarchical.css, in the same directory as the template itself.

Then invoked from the command line:

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"

I get the error:

pandoc: Could not fetch hierarchical.css
hierarchical.css: openBinaryFile: does not exist (No such file or directory)

I’ve tried various other relative paths to the CSS file. The only thing that works is the absolute path /Users/jmakeig/.pandoc/templates/hierarchical/hierarchical.css, which, of course, will only work on my laptop.

Is there any way to resolve external resources in Pandoc templates relative to the template itself, so that the templates are portable? I don’t see an obvious external variable that I could use in my template or a command line option.

like image 972
Justin Makeig Avatar asked Nov 13 '17 18:11

Justin Makeig


1 Answers

I'm pasting the work-around I gave to the issue that I created in github a while ago.

Coming back to the topic more than two years after I created this issue, I've found a not-so-bad workaround.

Apparently, latex uses the environment variable TEXINPUTS as a sort ot PATH for resources. So, you can just configure an environment variable once in your system (linux, windows, wherever) and just refer to resources relative to that path.

This link provides some explanation about how to use it: https://tex.stackexchange.com/questions/93712/definition-of-the-texinputs-variable

For example I have the following files:

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

In my system I set the environment variable (example with Windows, although I actually just save it under the system config):

set TEXINPUTS=SOME_PATH_TO/templates/

In the template my_latex_template.tex I have something like:

%...
\includegraphics{img/my_img.png}
%...

And I call the template like so:

pandoc file.txt -t pdf --template=SOME_PATH_TO/templates/my_latex_template.tex --output=output.pdf
like image 178
pchtsp Avatar answered Jan 02 '23 12:01

pchtsp