latex.mkの推敲
[makefiles.git] / latex_mk / latex.mk
index d937b4e..19b15ad 100644 (file)
@@ -1,11 +1,44 @@
-.PHONY: clean-tex distclean-tex
+# latex.mk
+# LaTeX処理(コンパイル)を行う
+#
+# == 使い方 ==
+# 1. texソースファイルと同じディレクトリーに本ファイル(latex.mk)をコピーする
+# 2. Makefileに変数TEXTARGETS と「include latex.mk」を記述する
+# 3. texソースファイルと同じディレクトリーで、make コマンドを実行する
+#
+# == 機能 ==
+# - 読み込むべき中間ファイルがないことや相互参照未定義の警告がある場合、LaTeX処理を最大4回繰り返す
+# - \includegraphics命令がTeXファイルに含まれる場合、グラフィックファイルを挿入
+#   -- 挿入されたグラフィックファイルが更新されたときは、処理を開始
+#   -- 挿入されたグラフィックファイルがないときは、処理を中止
+#   -- 挿入されたグラフィックファイルに対するバウンディング情報ファイル(.xbb)を作成
+# - \include、\input命令がTeXファイルに含まれる場合、TeXファイルを挿入
+#   -- 挿入されたTeXファイルが更新されたときは、処理を開始
+#   -- 挿入されたTeXファイルがないときは、処理を中止
+# - \makeindex命令が含まれる場合、mendexで索引を作成
+# - \bibliography命令が含まれる場合、BiBTeXで文献一覧を作成
+#
+# == 擬似ターゲット ==
+# - tex-clean: TeX中間ファイル(auxなど)を削除。ターゲットに.dviが含まれていないときは.dviファイルを削除
+# - xbb-clean: バウンディング情報ファイル(.xbb)を削除
+# - tex-distclean: TeX中間ファイル、バウンディング情報ファイル、ターゲットファイル(PDF、.dvi)を削除
+#
+# === Makefile -- sample ===
+# TEXTARGETS := report.tex
+#
+# all: $(TEXTARGETS)
+#
+# include latex.mk
+.PHONY: tex-warning tex-clean tex-distclean
 
 # シェルコマンド
+CAT := cat
 CMP := cmp -s
 CP := cp
-ECHO := echo
+ECHO := /bin/echo
 GREP := grep
 SED := sed
+SEQ := seq
 
 # LaTeXコマンド
 LATEX := platex
@@ -15,126 +48,234 @@ BIBTEX := pbibtex
 MENDEX := mendex
 
 #LaTeXオプション
-interaction ?= nonstopmode
 LATEXFLAG ?=
 DVIPDFMXFLAG ?=
 EXTRACTBBFLAGS ?=
 BIBTEXFLAG ?=
 MENDEXFLAG ?=
 
-# LaTeX処理(コンパイル)
-COMPILE.tex = $(LATEX) -interaction=$(interaction) $(LATEXFLAG) $(addsuffix .tex,$(basename $<))
+tex-warning:
+       @$(ECHO) "check current directory, or set TEXTARGET in Makefile."
+
+# ファイル名から拡張子を除いた部分
+BASE = $(basename $<)
+
+# TeX中間ファイルの拡張子
+#   .aux: 相互参照
+#   .fls: tex -recorderで生成されるファイルリスト
+#   .lof: 図リスト(\tableoffigures)
+#   .lot: 表リスト(\tableoftables)
+#   .out: hyperrefパッケージ
+#   .toc: 目次(\tableofcontents)
+#   .log: ログ
+TEX_INT := .aux .fls .lof .lot .out .toc .log
+# 索引中間ファイルの拡張子
+#   .idx: auxから作成
+#   .ind: idxから作成
+#   .ilg: 索引ログ
+IND_INT := .idx .ind .ilg
+# BiBTeX中間ファイルの拡張子
+#   .bbl: auxから作成
+#   .blg: BiBTeXログ
+BIB_INT := .bbl .blg
+
+ALL_INTERFILES = $(addprefix *,$(TEX_INT) $(IND_INT) $(BIB_INT) .d .*_prev)
+
+.SECONDARY: $(wildcard ALL_INTERFILES)
+
+# \tableofcontents命令をTeXファイルから検索する
+toc = \
+  $(shell \
+    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
+    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+    $(SED) -n -e 's/.*\(\\tableofcontents\)/\1/p' \
+  )
+
+# \listoffigures命令をTeXファイルから検索する
+lof = \
+  $(shell \
+    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
+    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+    $(SED) -n -e 's/.*\(\\listoffigures\)/\1/p' \
+  )
+
+# \listoftables命令をTeXファイルから検索する
+lot = \
+  $(shell \
+     $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
+     $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+     $(SED) -n -e 's/.*\(\\listoftables\)/\1/p' \
+   )
+
+# \makeindex命令をTeXファイルから検索する
+makeindex = \
+  $(shell \
+    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
+    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+    $(SED) -n -e 's/.*\(\\makeindex\)/\1/p' \
+  )
+
+# \bibliography命令で読み込まれる文献データベースファイルをTeXファイルから検索する
+bibdb = \
+  $(addsuffix .bib,$(basename $(strip $(shell \
+     $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
+     $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+     $(SED) -n -e 's/\\bibliography\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
+     $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' | \
+     $(SED) -e 's/,/ /g' \
+   ))))
+
+# hyperrefパッケージ読み込みをTeXファイルから検索する
+hyperref = \
+  $(shell \
+    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
+    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+    $(SED) -n -e 's/.*\(\\usepackage\(\[[^]]*\]\)\{0,1\}{hyperref}\)/\1/p'\
+  )
+
+# $(BASE).texで使われるLaTeX中間ファイル
+INTERFILES = \
+  $(strip \
+    $(if $(toc),$(BASE).toc) \
+    $(if $(lof),$(BASE).lof) \
+    $(if $(lot),$(BASE).lot) \
+    $(if $(makeindex),$(BASE).ind) \
+    $(if $(bibdb),$(BASE).bbl) \
+    $(if $(hyperref),$(BASE).out) \
+  )
 
-# 相互参照の警告
-WARN_REF := 'Label(s) may have changed. Rerun to get cross-references right.'
+INTERFILES_PREV = $(addsuffix _prev,$(INTERFILES))
 
-# 相互参照未定義の警告。2回目以降の処理で出る場合は、参照エラー
-WARN_UNDEFREF := 'There were undefined references.'
+# \include命令で読み込まれるTeXファイル
+includetex = \
+  $(strip $(addsuffix .tex,$(shell \
+    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex | \
+    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+    $(SED) -n -e 's/\\include\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
+    $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
+  )))
 
-# auxやtocなどのファイルがない警告
-WARN_NOFILE := 'No file'
+# \input命令で読み込まれるTeXファイル
+define get_inputtex
+  $(strip $(shell \
+    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $1 | \
+    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+    $(SED) -n -e 's/\\input\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
+    $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
+  ))
+endef
 
-# LaTeX処理の最大回数
-MAX_CNT := 10
+inputtex = $(call get_inputtex,$(BASE).tex $(includetex))
 
-# logファイルに相互参照または目次ファイルなしの警告がある場合、LaTeX処理を繰り返す
-# 2回目以降の処理で相互参照未定義の警告がある場合と、
-# 繰り返しの回数がMAX_CNTになった場合は、警告を表示してエラー終了
+# \include命令または\input命令で読み込まれるTeXファイル
+intex = $(strip $(includetex) $(inputtex))
+
+# \includegraphics命令で読み込まれるグラフィックファイル
+ingraphics = \
+  $(strip $(shell \
+    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
+    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+    $(SED) -n -e 's/\\includegraphics\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
+    $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
+  ))
+
+# LaTeX処理(コンパイル)
+LATEXCMD = $(LATEX) -interaction=batchmode $(LATEXFLAG) $(BASE).tex
+COMPILE.tex = $(ECHO) $(LATEXCMD); $(LATEXCMD) >/dev/null 2>&1 || ($(CAT) $(BASE).log; exit 1)
+
+# 相互参照未定義の警告
+WARN_UNDEFREF := 'There were undefined references\.'
+
+# LaTeX処理
+# ログファイルに警告がある場合は警告がなくなるまで、最大CNTで指定された回数分、処理を実行する
+CNT := 3
 COMPILES.tex = \
-  cnt=0; \
-  while $(GREP) -F -e $(WARN_REF) -e $(WARN_NOFILE) $(addsuffix .log,$(basename $<)); do \
-    if test $$cnt -ge $(MAX_CNT); then \
-      $(ECHO) "LaTeX compile is over $$cnt times, but warnings exist."; \
-      exit 1; \
-    fi; \
-    $(COMPILE.tex); \
-    if test $$cnt -eq 1 && $(GREP) -F $(WARN_UNDEFREF) $(addsuffix .log,$(basename $<)); then \
-      exit 1; \
-    fi; \
-    cnt=`expr $$cnt + 1`; \
-  done
-
-# \includeコマンドで読み込まれるtexファイル
-intex = $(addsuffix .tex,$(basename $(strip $(shell \
-  $(SED) -n -e 's/\\\(include\|input\)\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/gp' $<  | \
-  $(SED) -e 's/.*{\([^}]*\)}.*/\1/'))))
-
-# \includegraphicsコマンドで読み込まれるグラフィックファイル
-ingraphics = $(strip $(shell \
-  $(SED) -n -e 's/\\includegraphics\(\[[^]]*\]\)\{0,1\}\({[^}]*}\)/&\n/gp' $< $(intex) | \
-  $(SED) -e 's/.*{\([^}]*\)}.*/\1/'))
-
-# \bibliographyコマンドで読み込まれる文献データベースファイル
-bibdb = $(addsuffix .bib,$(basename $(strip $(shell \
-  $(SED) -n -e 's/\\bibliography\(\[[^]]*\]\)\{0,1\}{\([^}]*\)}/&\n/gp' $< $(intex) | \
-  $(SED) -e 's/.*{\([^}]*\)}.*/\1/' | \
-  $(SED) -e 's/,/ /g'))))
-
-# texファイルに索引作成の指定があるか確認
-MAKEINDEX := '\makeindex'
-GREP-makeindex = $(GREP) -F $(MAKEINDEX) $<
-
-# 依存関係を自動生成し、dファイルに格納
+  @(for i in `$(SEQ) 1 $(CNT)`; do \
+      if test -s $@ -a -s $(BASE).log; then \
+        $(GREP) -e $(WARN_UNDEFREF) $(BASE).log || exit 0; \
+      else \
+        $(ECHO) '$@ and/or $(BASE).log does not exist.'; \
+      fi; \
+      $(COMPILE.tex); \
+    done)
+
+# DVI -> PDF
+# 出力結果は.logファイルへ出力
+DVIPDFCMD = $(DVIPDFMX) $(DVIPDFMXFLAG) $(BASE).dvi
+COMPILE.dvi = $(ECHO) $(DVIPDFCMD); $(DVIPDFCMD) >>$(BASE).log 2>&1 || ($(CAT) $(BASE).log; exit 1)
+
+# 索引中間ファイル(.ind)作成
+MENDEXCMD = $(MENDEX) $(MENDEXFLAG) $(BASE).idx
+COMPILE.idx = $(ECHO) $(MENDEXCMD); $(MENDEXCMD) >/dev/null 2>&1 || ($(CAT) $(BASE).ilg; exit 1)
+
+# 文献リスト中間ファイル(.bbl)作成
+BIBTEXCMD = $(BIBTEX) $(BIBTEXFLAG) $(BASE).aux
+COMPILE.bib = $(ECHO) $(BIBTEXCMD); $(BIBTEXCMD) >/dev/null 2>&1 || ($(CAT) $(BASE).blg; exit 1)
+
+# ターゲットファイルと必須ファイルを比較し、内容が異なる場合はターゲットファイルの内容を必須ファイルに置き換える
+CMPPREV = $(CMP) $@ $< || $(CP) -p -v $< $@
+
+# 依存関係を.dファイルに書き出す
 %.d: %.tex
-       @$(ECHO) '$@ is created by scanning $^.'
-  # texファイルの依存関係
-       @($(ECHO) '$(subst .tex,.dvi,$<) $(subst .tex,.aux,$<) $(subst .tex,.d,$<): $<' >$@; \
-      $(ECHO) >>$@; \
-      $(ECHO) '$(subst .tex,.prev_aux,$<):' >>$@)
-  # Include/Inputファイルの依存関係
-       $(if $(intex),@( \
+       @$(ECHO) '$@ is created by scanning $<.'
+# .dファイルの依存関係
+       @$(ECHO) '$(BASE).d: $(BASE).tex' >$@
+# 中間ファイルの依存関係
+       $(if $(INTERFILES),@( \
       $(ECHO); \
-      $(ECHO) '# Include/Input Files - tex'; \
-      $(ECHO) '$(subst .tex,.dvi,$<) $(subst .tex,.aux,$<): $(intex)') >>$@)
-  # 画像ファイルの依存関係
+      $(ECHO) '# LaTeX Intermediate Files'; \
+      $(ECHO) '$(BASE).dvi:: $(INTERFILES_PREV)'; \
+      $(ECHO) '        @$$(COMPILE.tex)'; \
+      $(ECHO); \
+      $(ECHO) '$(BASE).dvi:: $(BASE).aux'; \
+      $(ECHO) '        @$$(COMPILES.tex)'; \
+    ) >>$@)
+# 画像ファイルの依存関係
        $(if $(ingraphics),@( \
       $(ECHO); \
-      $(ECHO) '# IncludeGraphic Files - pdf, jpeg/jpg, png & xbb'; \
-      $(ECHO) '$(subst .tex,.dvi,$<) $(subst .tex,.aux,$<): $(ingraphics)'; \
+      $(ECHO) '# IncludeGraphic Files - .pdf, .eps, .jpeg/.jpg, .png'; \
+      $(ECHO) '#           .xbb Files - .pdf, .jpeg/.jpg, .png'; \
+      $(ECHO) '$(BASE).aux: $(ingraphics)'; \
+      $(if $(filter-out %.eps,$(ingraphics)), \
+        $(ECHO); \
+        $(ECHO) '$(BASE).aux: $(addsuffix .xbb,$(basename $(filter-out %.eps,$(ingraphics))))'; \
+      ) \
+    ) >>$@)
+       $(if $(intex),@( \
       $(ECHO); \
-      $(ECHO) '$(subst .tex,.dvi,$<) $(subst .tex,.aux,$<): $(addsuffix .xbb,$(basename $(ingraphics)))') >>$@)
-  # 文献処理用ファイルの依存関係
+      $(ECHO) '# Files called from \include or \input - .tex'; \
+      $(ECHO) '$(BASE).aux: $(intex)'; \
+    ) >>$@)
+# 文献処理用ファイルの依存関係
        $(if $(bibdb),@( \
-       $(ECHO); \
-       $(ECHO) '# Bibliography Files - bbl & bib'; \
-       $(ECHO) '$(subst .tex,.dvi,$<): $(subst .tex,.bbl,$<)'; \
-       $(ECHO); \
-       $(ECHO) '$(subst .tex,.bbl,$<): $(bibdb)') >>$@)
-  # 索引作成用ファイルの依存関係
-       $(if $(strip $(shell $(GREP-makeindex))),@( \
-       $(ECHO); \
-       $(ECHO) '# MakeIndex Files - ind'; \
-       $(ECHO) '$(subst .tex,.idx,$<):'; \
-       $(ECHO); \
-       $(ECHO) '$(subst .tex,.ind,$<):'; \
-       $(ECHO); \
-       $(ECHO) '$(subst .tex,.dvi,$<): $(subst .tex,.ind,$<)') >>$@)
-
-# 変数TARGETSで指定されたターゲットファイルに対応するdファイルをインクルード
-# .dファイルからヘッダファイルの依存関係を取得する
-# ターゲットに clean が含まれている場合は除く
-ifeq (,$(filter %clean,$(MAKECMDGOALS)))
-  -include $(addsuffix .d,$(basename $(TARGETS)))
+      $(ECHO); \
+      $(ECHO) '# Bibliography files: .aux, BIBDB -> .bbl -> .div'; \
+      $(ECHO) '$(BASE).bbl: $(bibdb) $(BASE).tex'; \
+    ) >>$@)
+
+# 変数TEXTARGETSで指定されたターゲットファイルに対応する
+# .dファイルをインクルードし、依存関係を取得する
+# ターゲット末尾に clean、.xbb、.tex、.d が含まれている場合は除く
+ifeq (,$(filter %clean %.xbb %.tex %.d,$(MAKECMDGOALS)))
+  -include $(addsuffix .d,$(basename $(TEXTARGETS)))
 endif
 
-# 相互参照ファイル作成。dviファイルも同時に作成される
+# auxファイル作成
 %.aux: %.tex
-       $(COMPILE.tex)
-
-%.prev_aux: %.aux
-       -$(CMP) $@ $< || $(CP) $< $@
+       @$(COMPILE.tex)
 
-# dviファイル作成
 %.dvi: %.aux
-       $(COMPILES.tex)
+       @$(COMPILES.tex)
+
+%.dvi: %.tex
+       @$(COMPILE.tex)
+       @$(COMPILES.tex)
 
-# 文献処理用ファイル作成
-# BiBTeXで文献処理するときに使用される
-%.bbl: %.prev_aux
-       $(BIBTEX) $(BIBTEXFLAG) $(subst .prev_aux,.aux,$<)
+# PDFファイル作成
+%.pdf: %.dvi
+       @$(COMPILE.dvi)
 
 # バウンディング情報ファイル作成
-# dvipdfmxで図を挿入するときに使用される
 # pdf、jpeg/jpg、pngファイルに対応
 %.xbb: %.pdf
        $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
@@ -148,25 +289,69 @@ endif
 %.xbb: %.png
        $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
 
-# 索引ファイル作成
-# 索引を作成するときに使用される
-%.idx: %.aux
-       $(COMPILE.tex)
+# 目次中間ファイル作成
+%.toc: %.tex
+       @$(MAKE) -s $(BASE).aux
 
-%.prev_idx: %.idx
-       -$(CMP) $@ $< || $(CP) $< $@
+%.toc_prev: %.toc
+       @$(CMPPREV)
 
-%.ind: %.prev_idx
-       $(MENDEX) $(MENDEXFLAG) $(subst .prev_idx,.idx,$<)
+# 図リスト中間ファイル作成
+%.lof: %.tex
+       @$(MAKE) -s $(BASE).aux
 
-# PDFファイル作成
-%.pdf: %.dvi
-       $(DVIPDFMX) $(DVIPDFMXFLAG) $<
+%.lof_prev: %.lof
+       @$(CMPPREV)
+
+# 表リスト中間ファイル作成
+%.lot: %.tex
+       @$(MAKE) -s $(BASE).aux
+
+%.lot_prev: %.lot
+       @$(CMPPREV)
+
+# 索引中間ファイル作成
+%.idx: %.tex
+       @$(MAKE) -s $(BASE).aux
+
+%.idx_prev: %.idx
+       @$(CMPPREV)
+
+%.ind: %.idx_prev
+       @$(COMPILE.idx)
+
+%.ind_prev: %.ind
+       @$(CMPPREV)
+
+# BiBTeX中間ファイル作成
+%.bbl: %.tex
+       @$(MAKE) -s $(BASE).aux
+       @$(COMPILE.bib)
+
+%.bbl_prev: %.bbl
+       @$(CMPPREV)
+
+# hyperref中間ファイル作成
+%.out: %.tex
+       @$(MAKE) -s $(BASE).aux
+
+%.out_prev: %.out
+       @$(CMPPREV)
 
 # tex-cleanターゲット
 tex-clean:
-       $(RM) *.aux *.bbl *.blg *.idx *.ilg *.ind *.lof *.lot *.out *.toc *.xbb *.log *.d
+       $(RM) $(ALL_INTERFILES)
+ifeq (,$(filter %.dvi,$(TEXTARGETS)))
+       $(RM) *.dvi
+endif
+
+# xbb-cleanターゲット
+xbb-clean:
+       $(RM) *.xbb
 
 # tex-distcleanターゲット
-tex-distclean: tex-clean
-       $(RM) $(addsuffix .dvi,$(basename $(TARGETS))) $(addsuffix .pdf,$(basename $(TARGETS)))
+tex-distclean: tex-clean xbb-clean
+ifneq (,$(filter %.dvi,$(TEXTARGETS)))
+       $(RM) *.dvi
+endif
+       $(RM) $(TEXTARGETS)