Lacking Natural Simplicity

Random musings on books, code, and tabletop games.

Making digest-sized pages with pandoc and ms output

I use pandoc to produce digest-sized pages (5½×8½ inches) in PDF documents from ReStructuredText. I have a ms output template, digest2.ms in ~/pandoc/templates that sets the page width and page height correctly. Unfortunately, there is no way to set the papersize properly in the template. [1]

Luckily, pandoc has the --pdf-engine-opt= option to pass options to the PDF engine that pandoc is using, in this case pdfroff. In the case of ms output the thing to do is pass the argument --pdf-engine-opt=-P-p8.5i,5.5i [2], like below.

GNUmakefile.pandoc-digest-pdf-engine-opt (Source)

%.digest.ms.pdf : %.rst
        pandoc -s -r rst -w ms --template=digest2 $(VARIABLES) \
                --output=$@ --pdf-engine-opt=-P-p8.5i,5.5i $<

Unfortunately when I originally had this need for digest pages I hadn't realized that the --pdf-engine-opt= option existed.

So I used pandoc --verbose and found the pdfroff invocation pandoc uses, and made my pandoc invocation output ms instead of PDF, then passed it through a pdfroff command with the added -P-p8.5i,5.5i argument. Here's what I put in my GNUmakefile:

GNUmakefile.pandoc-digest-save-ms (Source)

%.digest.ms.pdf : %.rst
        pandoc -s -r rst -w ms -s --template=digest2 $(VARIABLES) \
               --output=$*.ms $<
        pdfroff -ms -mpdfmark -e -t -k -KUTF-8 --no-toc-relocation \
                -P-p8.5i,5.5i $*.ms > $@

Or, if you don't care about having the ms output for debugging, you could do it as a pipeline:

GNUmakefile.pandoc-digest-pipeline (Source)

%.digest.ms.pdf : %.rst
        pandoc -s -r rst -w ms -s --template=digest2 $(VARIABLES) \
               --output=- $< | \
        pdfroff -ms -mpdfmark -e -t -k -KUTF-8 --no-toc-relocation \
                -P-p8.5i,5.5i - >$@

There are still some instances where this technique of having pandoc output the ms source directly and pass it through pdfroff yourself. For instance, if you have to do some massaging of the ms source, like changing .RS/.RE to .QS/.QE to get around the problem with block quotes in ms output in the current pandoc release, discussed here.

Print Friendly and PDF

Comments

Comments powered by Disqus