テスト用ドキュメントの修正
[makefiles.git] / latex_mk / latex.mk
index a0d4028..19b15ad 100644 (file)
-.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
 
 # シェルコマンド
-ECHO := echo
+CAT := cat
+CMP := cmp -s
+CP := cp
+ECHO := /bin/echo
 GREP := grep
 SED := sed
+SEQ := seq
 
 # LaTeXコマンド
-BIBTEX := pbibtex
+LATEX := platex
 DVIPDFMX := dvipdfmx
 EXTRACTBB := extractbb
-LATEX := platex
+BIBTEX := pbibtex
 MENDEX := mendex
 
 #LaTeXオプション
-BIBTEXFLAG ?=
+LATEXFLAG ?=
 DVIPDFMXFLAG ?=
 EXTRACTBBFLAGS ?=
-LATEXFLAG ?=
+BIBTEXFLAG ?=
 MENDEXFLAG ?=
 
-# LaTeXで処理
-COMPILE.tex = $(LATEX) -interaction=batchmode $(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) \
+  )
+
+INTERFILES_PREV = $(addsuffix _prev,$(INTERFILES))
+
+# \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' \
+  )))
+
+# \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
+
+inputtex = $(call get_inputtex,$(BASE).tex $(includetex))
+
+# \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)
 
-# logファイルに未定義参照の警告があるか確認
-REFWARN := 'LaTeX Warning: There were undefined references.'
+# 相互参照未定義の警告
+WARN_UNDEFREF := 'There were undefined references\.'
 
-# \includeコマンドで読み込まれるtexファイル
-intex = $(addsuffix .tex,$(basename $(strip $(shell \
-  $(SED) -n -e 's/\\\(include\|input\)\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/gp' $<  | \
-  $(SED) -e 's/.*{\([^}]*\)}.*/\1/'))))
+# LaTeX処理
+# ログファイルに警告がある場合は警告がなくなるまで、最大CNTで指定された回数分、処理を実行する
+CNT := 3
+COMPILES.tex = \
+  @(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)
 
-# \includegraphicsコマンドで読み込まれるグラフィックファイル
-ingraphics = $(strip $(shell \
-  $(SED) -n -e 's/\\includegraphics\(\[[^]]*\]\)\{0,1\}\({[^}]*}\)/&\n/gp' $< $(intex) | \
-  $(SED) -e 's/.*{\([^}]*\)}.*/\1/'))
+# DVI -> PDF
+# 出力結果は.logファイルへ出力
+DVIPDFCMD = $(DVIPDFMX) $(DVIPDFMXFLAG) $(BASE).dvi
+COMPILE.dvi = $(ECHO) $(DVIPDFCMD); $(DVIPDFCMD) >>$(BASE).log 2>&1 || ($(CAT) $(BASE).log; exit 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'))))
+# 索引中間ファイル(.ind)作成
+MENDEXCMD = $(MENDEX) $(MENDEXFLAG) $(BASE).idx
+COMPILE.idx = $(ECHO) $(MENDEXCMD); $(MENDEXCMD) >/dev/null 2>&1 || ($(CAT) $(BASE).ilg; exit 1)
 
-# texファイルに索引作成の指定があるか確認
-MAKEINDEX := '\makeindex'
-GREP-makeindex = $(GREP) -F $(MAKEINDEX) $<
+# 文献リスト中間ファイル(.bbl)作成
+BIBTEXCMD = $(BIBTEX) $(BIBTEXFLAG) $(BASE).aux
+COMPILE.bib = $(ECHO) $(BIBTEXCMD); $(BIBTEXCMD) >/dev/null 2>&1 || ($(CAT) $(BASE).blg; exit 1)
 
-# 依存関係を自動生成し、dファイルに格納
+# ターゲットファイルと必須ファイルを比較し、内容が異なる場合はターゲットファイルの内容を必須ファイルに置き換える
+CMPPREV = $(CMP) $@ $< || $(CP) -p -v $< $@
+
+# 依存関係を.dファイルに書き出す
 %.d: %.tex
-       @$(ECHO) '$@ is created by scanning $^.'
-  # texファイルの依存関係
-       @($(ECHO) '$(subst .tex,.dvi,$<) $(subst .tex,.aux,$<) $(subst .tex,.d,$<): $<' >$@)
-  # 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,.dvi,$<): $(subst .tex,.ind,$<)') >>$@)
-
-# 変数TARGETSで指定されたターゲットファイルに対応するdファイルをインクルード
-# .dファイルからヘッダファイルの依存関係を取得する
-# ターゲットに clean が含まれている場合は除く
-ifeq (,$(filter %clean,$(MAKECMDGOALS)))
-  -include $(addsuffix .d,$(basename $(TARGETS)))
+      $(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)
+       @$(COMPILE.tex)
 
-# dviファイル作成
 %.dvi: %.aux
-  # logファイルに未定義参照の警告がある場合、LaTeXで処理
-       while $(GREP) -F $(REFWARN) $(addsuffix .log,$(basename $<)); do $(COMPILE.tex); done
+       @$(COMPILES.tex)
 
-# 文献処理用ファイル作成
-# BiBTeXで文献処理するときに使用される
-%.bbl: %.aux
-       $(BIBTEX) $(BIBTEXFLAG) $(addsuffix .aux,$(basename $<))
+%.dvi: %.tex
+       @$(COMPILE.tex)
+       @$(COMPILES.tex)
+
+# PDFファイル作成
+%.pdf: %.dvi
+       @$(COMPILE.dvi)
 
 # バウンディング情報ファイル作成
-# dvipdfmxで図を挿入するときに使用される
 # pdf、jpeg/jpg、pngファイルに対応
 %.xbb: %.pdf
        $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
@@ -111,21 +289,69 @@ endif
 %.xbb: %.png
        $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
 
-# ç´¢å¼\95ファイル作成
-# 索引を作成するときに使用される
-%.idx: %.aux
+# ç\9b®æ¬¡ä¸­é\96\93ファイル作成
+%.toc: %.tex
+       @$(MAKE) -s $(BASE).aux
 
-%.ind: %.idx
-       $(MENDEX) $(MENDEXFLAG) $<
+%.toc_prev: %.toc
+       @$(CMPPREV)
 
-# PDFファイル作成
-%.pdf: %.dvi
-       $(DVIPDFMX) $(DVIPDFMXFLAG) $<
+# 図リスト中間ファイル作成
+%.lof: %.tex
+       @$(MAKE) -s $(BASE).aux
+
+%.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 *.d *.idx *.ilg *.ind *.lof *.log *.lot *.out *.toc *.xbb
+       $(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)