3

I have a makefile that I use to convert files in markdown into .pdf through a latex template. At the moment, this only works with one file at a time. However, I'd like the makefile to run on any markdown file in the active directory and output to a .pdf with the same name with a single make command. For example, I might have the following:

Foo.md ---> Foo.pdf

Bar.md ---> Bar.pdf

My current makefile is here:

TEX = pandoc
MEXT = md
src = template.tex $(wildcard *.$(MEXT))
FLAGS = --latex-engine=xelatex

letter.pdf : $(src)
$(TEX) $(filter-out $<,$^ ) -o $@ --template=$< $(FLAGS)

.PHONY: clean
clean :
rm output.pdf

Thank you for any pointers...

4

Try this:

TEX = pandoc
MEXT = md
SRC = $(wildcard *.$(MEXT))
PDFS = $(SRC:.md=.pdf)
TMP = template.tex
FLAGS = --latex-engine=xelatex

all:    ${PDFS}

%.pdf:  %.md ${TMP}
        ${TEX} $(filter-out $<,$^ ) -o $@ --template=${TMP} $(FLAGS) $<


.PHONY: clean
clean:
        rm *.pdf
  • Getting "missing separator" error from the makefile...what causes that? – Linter Jun 22 '16 at 23:33
  • @LarryTate Replace the 8-spaces indents by a tab. – Gilles Jun 23 '16 at 0:05
  • Which lines need the tab? I've tried each line but I get the error. Sorry! – Linter Jun 23 '16 at 0:38
  • All indentation should be tabs. – Edward Falk Jun 23 '16 at 1:38
  • 1
    your clean target could match the all target and rm -f ${PDFS}. – meuh Jun 23 '16 at 7:18
1

I did not test this completely but I think it should work with some modification. Using for loop:

TEX = pandoc
MEXT = md
SRC = $(wildcard *.$(MEXT))
TMP = template.tex
FLAGS = --latex-engine=xelatex

letter.pdf : 
    $(foreach i, $(SRC), $(TEX) -o $(i).pdf --template=$(TMP) $(FLAGS) $(i);)

.PHONY: clean
clean :
    rm -f *.pdf

Also note that this solution will not produce pdf files with exactly the same name as md files:

foo.md -> foo.md.pdf

Should not be hard to fix though.

0
# List files to be made by finding all *.md files and appending .pdf

PDFS := $(patsubst %.md,%.md.pdf,$(wildcard *.md))

# The all rule makes all the PDF files listed
all : $(PDFS)

# This generic rule accepts PDF targets with corresponding Markdown 
# source, and makes them using pandoc
%.md.pdf : %.md
    pandoc --latex-engine=xelatex $< -o $@

# Remove all PDF outputs
clean :
    rm $(PDFS)

# Remove all PDF outputs then build them again
rebuild : clean all

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.