I have installed pandoc
and MacTex
so I can convert markdown to pdf from the command line but I want to be able to edit and compile .md
files to pdf from the TexShop interface if possible. I looked inside ~/Library/TeXShop/Engines/Inactive/pandoc
and I have pandoc.engine
and markdown2pdf.engine
. I have tried moving both these files to ~/Library/TeXShop/Engines
so they appear as options when I open a file in TeXShop. However neither of these engines allows me to typeset a .md
file: the pandoc
engine creates a file $1.epub
in the same folder with the source file improperly typeset as an iBook, and the markdown2pdf
engine gives the error markdown2pdf: Command not found.
. Is there a way to create a .engine
file in ~/Library/TeXShop/Engines
that will do the equivalent of pandoc filename.md -s -o filename.pdf
(which is what I have been entering in the command line) so that I can typeset from TeXShop rather than the terminal? If not, are there other tools/applications I can use to this end?
EDIT:
Alan Munn's solution worked, with some modification, so now the code in pandoc-pdf.engine
reads
#!/bin/bash
# pandoc takes an input file (usually markdown) and writes an output file
(latex, epub, ...)
# There is a shortcut to create pdf from markdown via latex called
markdown2pdf.
# See http://johnmacfarlane.net/pandoc/
PATH=$PATH:/Library/TeX/texbin:/usr/texbin:/usr/local/bin
fname_with_ext=$(basename "$1")
fname="${fname_with_ext%.*}.pdf"
pandoc "$1" -s -o $fname
Turns out you don't have to open in TeXShop, as it refreshes automatically.
In the current state nothing prints to the console unless there is an error. Note that in the above code I fixed a file extension issue, so as to avoid a pdf called filename.md.pdf
.
Best Answer
The existing
pandoc
engine doesn't do what you want because it's passing the different parameters topandoc
. Thepandoc2pdf
engine doesn't work because there's nopandoc2pdf
script in TeXLive as far as I can see.But you can make your own
pandoc
Engine quite easily. Make a copy of the existingpandoc.engine
and place it in your Engines folder. I've called minepandoc-pdf.engine
. It has the following code: (If you make a brand new file (not a copy) you need to make sure the executable bit is set on the file. To do that, usechmod +x
on the file from the terminal.)Running this on a
.md
file will produce a PDF correctly..md
fileIf you want to use the TeXShop directive
% !TEX TS-program = pandoc-pdf
line in your file, to compile, you need to embed it in a markdown comment, sincepandoc
will process the line otherwise (it doesn't recognize%
as a comment in.md
). This can be done with:Here is a sample markdown file and its output.
Output PDF