yegle
yegle

Reputation: 5865

LaTeX reference and Makefile

I'm using Makefile to generate PDF from .tex files.

When references was used in my LaTeX files. sometimes I get something like

LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

I know that re-run LaTeX compile command can fix this reference problem, but in my Makefile, %.pdf only depends on %.tex,thus just run make again doesn't fix the problem (nothing changed in .tex file). I need to do a make clean to re-generate PDF again.

Here's my Makefile

TEX := $(wildcard *.tex)
default: $(TEX:.tex=.pdf)
%.pdf: %.tex
    xelatex $<
.PHONY: clean
clean:
    rm -v *.aux *.toc *.log *.out

How to solve this problem? Thank you.

UPDATE:

Here's some thought I found from Google

  1. Change default target to be a .PHONY. Which is not a very good solution (because there's so may latex file there, and I just need to re-compile a single file)
  2. Change %.pdf's dependency to include %.aux. But I don't know if it's possible in GNU make? (depends on %.aux file if it exists, otherwise ignore the dependency on %.aux)
  3. Do a grep to the .log file and find the specific warning. If it exists, re-run compile command.

Upvotes: 9

Views: 3940

Answers (2)

Markus Kuhn
Markus Kuhn

Reputation: 1042

I use in all my LaTeX makefiles the simple rule

.DELETE_ON_ERROR:

%.pdf %.aux %.idx: %.tex
        pdflatex $<
        while grep 'Rerun to get ' $*.log ; do pdflatex $< ; done

This repeats pdflatex as often as necessary. I found that all the different LaTeX messages that call for a rerun contain the common string "Rerun to get " in the log file, so you can just test for its presence with grep in a while loop.

The ".DELETE_ON_ERROR:" setting is important: it ensures that make automatically deletes any remaining incomplete pdf/aux/idx files whenever TeX aborts with an error, such that they cannot confuse make when you call it next time.

When I use DVI rather than PDF as the output format, I use equivalently

%.dvi %.aux %.idx: %.tex
        latex $<
        while grep 'Rerun to get ' $*.log ; do latex $< ; done
        -killall -USR1 -r xdvi || true

The last line causes any running xdvi to reload its input file, for instant visual inspection.

Upvotes: 12

Jost
Jost

Reputation: 5840

Either make "default" a phony target (Add "default" to the line starting with .PHONY), or build a more complex dependency structure, which reruns automatically (can't say how to do that, sorry).

Upvotes: 0

Related Questions