-# \makeindex命令
-makeindex = $(shell $(SED) -n -e '/^.*[^\]\{0,1\}%.*\\makeindex/!s/\\makeindex/&/p' $<)
-
-# \bibliography命令で読み込まれる文献データベースファイル
-bibdb = $(addsuffix .bib,$(basename $(strip $(shell \
- $(SED) -n -e '/^.*[^\]\{0,1\}%.*\\bibliography/!s/\\bibliography\(\[[^]]*\]\)\{0,1\}{\([^}]*\)}/&\n/gp' $< $(intex) | \
- $(SED) -e 's/.*{\([^}]*\)}.*/\1/' | \
- $(SED) -e 's/,/ /g'))))
-
-# ファイル名から拡張子を除いた部分
-BASE = $(basename $<)
-# .texファイル
-TEXFILE = $(addsuffix .tex,$(BASE))
-# .auxファイル
-AUXFILE = $(addsuffix .aux,$(BASE))
-# .prev_auxファイル
-PREV_AUXFILE = $(addsuffix .prev_aux,$(BASE))
-# .dviファイル
-DVIFILE = $(addsuffix .dvi,$(BASE))
-# .dファイル
-DFILE = $(addsuffix .d,$(BASE))
-# .logファイル
-LOGFILE = $(addsuffix .log,$(BASE))
-# .idxファイル
-IDXFILE = $(addsuffix .idx,$(BASE))
-# .prev_idxファイル。.idxファイルのコピー
-PREV_IDXFILE = $(addsuffix .prev_idx,$(BASE))
-# .indファイル
-INDFILE = $(addsuffix .ind,$(BASE))
-# .prev_indファイル。.indファイルのコピー
-PREV_INDFILE = $(addsuffix .prev_ind,$(BASE))
-# .ilgファイル
-ILGFILE = $(addsuffix .ilg,$(BASE))
-# .bblファイル
-BBLFILE = $(addsuffix .bbl,$(BASE))
-# .prev_bblファイル。.bblファイルのコピー
-PREV_BBLFILE = $(addsuffix .prev_bbl,$(BASE))
-# .bblファイル
-BLGFILE = $(addsuffix .blg,$(BASE))
+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))