I'd like to include Markdown in a LaTeX file
For ConTeXt, I have written a module, t-filter
, that provides a nice user-interface for running external programs on a file. Using that module, you can write:
\usemodule[filter]
\defineexternalfilter
[markdown]
[filter={pandoc -t context -o \externalfilteroutputfile}]
after which you can use
\processmarkdownfile{....}
to convert a markdown file to context and input the resulting back to tex. (The \defineexternalfilter
command also creates an environment \startmarkdown ... \stopmarkdown
. The module writes the contents of this environment to an external file, processes file through pandoc
, and inputs the result.)
This is essentially a glorified wrapper around \write18
. For a one-off task, you can use:
\newcommand\processmarkdownfile[1]
{\immediate\write18{pandoc -t latex -o #1.tex #1}%
\input{#1.tex}}
and then use \processmarkdownfile{...}
to include a markdown file in LaTeX. You need to enable write18
for this to work. The easiest method is to pass -shell-escape
to pdflatex
:
pdflatex -shell-escape <filename>
The t-filter
module provides other goodies as well (use an sub directory to store temp files, do not rerun the filter if the file has not changed, etc.). If you want, it is easy to add an interface for them in LaTeX as well.
There is a markdown parser written in lua, which can output LaTeX, ConTeXt and other format. It should not be hard to write a small wrapper to use it to parse markdown files in luatex (ConTeXt already includes a parser based on it).
This thread is quite old now, but in case someone will come here in the future looking for the solution there is now a dedicated LaTeX markdown package.
This seems to do exactly what was needed in the initial question.