Following up on this article I use GPP to empower Markdown parser pandoc with some macros. Unfortunately, gpp seems to copy all whitespace into the result.

For example, consider file test.md

% Title
% Raphael
% 2012

\lorem \ipsum

with test.gpp


Now, calling gpp -T --include test.gpp test.md yields

<empty line>
% Title
% Raphael
% 2012

Lorem ipsum...

This breaks the metadata extraction of pandoc. The extra linebreak is indeed the one between the definitions; if I use


with the extra option +c "@@@" "\n", the empty line is gone. But this workaround is not only ugly, is also has two fatal flaws.

First, it treats @@@ as comment indicator in the source file, too. As @@@ is not forbidden in Markdown, that can have unintended consequences when @@@ (or any other chosen delimiter) happens to occur in the source file.

Second, it does not cover whitespaces at line beginnings as caused by proper indentation. For example,

  \if{a == a}@@@
    ![some image](test.png)@@@

will cause all such image tags to be indented by four spaces, causing pandoc to typeset it as code (as specified).

So, short of writing gpp files in one line or introducing ugly line-end comments and not indenting, what can you do to prevent gpp from plastering superfluous whitespaces all over the place?


Assuming all the junk is in the include file, and therefore before the start of the document, you could just post-process it:


----- cut here ------

Then do:

gpp -T --include test.gpp test.md | sed '1,/----- cut here ------/d'

(Does gpp output to stdout? Otherwise just run sed on the output file.)

  • That's nice for the "static" junk from the include file. It doesn't fix the "dynamic" indentation problem, or does it? (gpp does output to stdout by default, yes.) – Raphael Aug 28 '12 at 16:52
  • 1
    I used your answer, added some more brute force and now it works. Thanks! – Raphael Aug 28 '12 at 18:02

One possibility is to preprocess the included macro file and shrink it to one line (using sed). In combination with ams approach, this makefile fragment solves the problem:

sed 's/^\s*//;s/\s*?$$//;H;$$!d;:e;x;/^$$/d;s/\n//g' $(MACROFILE) > $(BUILDPATH)/$(MACROFILE);

gpp -T -x -Dtarget=pdf --include $(BUILDPATH)/$(MACROFILE) $(MAINFILE) | \
  sed '1,/$(MFENDMARKER)/d' | \
  pandoc -S -R --toc -f markdown -o $(DISTPATH)/$(NAME).pdf;

Now the nastiness is hidden behind the scenes.

  • What is MFENDMARKER? – aggsol Aug 8 '19 at 7:02
  • @aggsol It's been a while; from its usage here, an arbitrary (but unique?) end marker for the macro file. Not sure why it is needed. – Raphael Aug 9 '19 at 22:16

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.