1

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.

  • I assume that when you say Library/TeXShop/Engines you really mean ~/Library/TeXShop/Engines. Do those engines appear in the Source Window's engine popup menu? Where do you have pandoc? Do you have markup2pdf and where is it? The pandoc engine does what you want but it has to find it along the PATH. I believe you can place the line % !TEX program = pandoc at the top of your .md file and then just Typeset->Typeset (Cmd-T) if pandoc is somewhere along the PATH. – Herb Schulz Aug 15 '17 at 16:43
  • Thanks for testing this. It seems that it was the double extension that was causing the file not to open automatically. I'll update my answer to use your code. – Alan Munn Aug 16 '17 at 19:04
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.