[Tex/LaTex] How to add markdown engine to TeXShop

editorsmarkdownpandocscriptstexshop

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 to pandoc. The pandoc2pdf engine doesn't work because there's no pandoc2pdf script in TeXLive as far as I can see.

But you can make your own pandoc Engine quite easily. Make a copy of the existing pandoc.engine and place it in your Engines folder. I've called mine pandoc-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, use chmod +x on the file from the terminal.)

#!/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

Running this on a .md file will produce a PDF correctly.

.md file

If 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, since pandoc will process the line otherwise (it doesn't recognize % as a comment in .md). This can be done with:

[//]: % !TEX TS-program = pandoc-pdf

Here is a sample markdown file and its output.

A First Level Header
====================

A Second Level Header
---------------------

Now is the time for all good men to come to
the aid of their country. This is just a
regular paragraph.

The quick brown fox jumped over the lazy
dog's back.

### Header 3

> This is a blockquote.
> 
> This is the second paragraph in the blockquote.
>
> ## This is an H2 in a blockquote

Output PDF

output of code

Related Question