Lacking Natural Simplicity

Random musings on books, code, and tabletop games.

Making digest-sized pages with pandoc and ms output

Note

After I wrote this post, I made a last minute check of the options to pandoc and found that the --pdf-engine-opt= option does exactly what I want. How embarrassing. So I changed this post, showing both the easy way and the hard way to do it.

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.

1

Heirloom Troff (originally at H1, but now I think more up to date at H2 and H3) has the .mediasize and .papersize commands for that.

2

Amusingly, pandoc passes the --pdf-engine-opt=-P-p8.5i,5.5i argument to pdfroff, which passes the -P-p8.5i,5.5i part to groff, which passes the -p8.5i,5.5i part to the (final) post processor — I'm not sure if that is grops or gropdf.

Print Friendly and PDF

Comments

Comments powered by Disqus