# Dynamically including pandoc-transformed texfiles in latex template

I am currently running the following command to populate my latex template.

pandoc -s --top-level-division=chapter --template=template.tex metadata.md content/*.md -o frommarkdown.tex

The content of the latex-ized *.md files are populating the template via the $body$ variable. However this happens verbatim, the $body$variable gets in-place substituted with the tex code generated from the md files. This generated a huge latex file with all my thesis chapters in one big file concatenated.

My ideal case would look like that I can \include all of the on the fly generated tex-files in my document, allowing me to keep a clean latex document which can come in handy when debugging or altering a larger project (e.g thesis).

I would ideally like to have something like that:

$for(include-chapters)$ \include($include-chapters$) $endfor$

My hunch was now to achieve this with a two step solution by first generating standalone tex files per *.md file (without the usage of a template). Then I would like to dynamically include these generated tex files in my main tex "template".

Can this be achieved with pandoc or do i need to write a custom shell script for this?

• I don't think this can be achieved with pure pandoc. You can try pandoc-discuss for help building a custom filter for that. Have you considered working in your thesis entirely in markdown/pandoc instead of going back and forth to tex? – lf_araujo Mar 11 '17 at 11:43

I don't grasp what you are exactly trying to do but you can modify the template and make it include tex files.

1. Get the latex template and save it to template.tex: pandoc -D latex > template.tex

2. Open the template file and add the following code snippet below $body$:

$for(include-chapters)$
\include{$include-chapters$}
$endfor$

3. Now you can add the files you want to include to metadata.md:

---
include-chapters:
- content/chapter1
- content/chapter2
---

4. Run pandoc:

pandoc -s -S --top-level-division=chapter \
--template=template.tex \

 \begin{document}

Still I don't recommend doing this. There are very powerful tools out there, which can do more and better, e.g. make