-# ファイル名から拡張子を除いた部分
-FILEBASE = $(basename $<)
-# .texファイル
-TEXFILE = $(addsuffix .tex,$(FILEBASE))
-# .auxファイル
-AUXFILE = $(addsuffix .aux,$(FILEBASE))
-# .prev_auxファイル。.auxファイルのコピー
-PREV_AUXFILE = $(addsuffix .prev_aux,$(FILEBASE))
-# .dviファイル
-DVIFILE = $(addsuffix .dvi,$(FILEBASE))
-# .dファイル
-DFILE = $(addsuffix .d,$(FILEBASE))
-# .logファイル
-LOGFILE = $(addsuffix .log,$(FILEBASE))
-# .idxファイル
-IDXFILE = $(addsuffix .idx,$(FILEBASE))
-# .prev_idxファイル。.idxファイルのコピー
-PREV_IDXFILE = $(addsuffix .prev_idx,$(FILEBASE))
-# .indファイル
-INDFILE = $(addsuffix .ind,$(FILEBASE))
-# .bblファイル
-BBLFILE = $(addsuffix .bbl,$(FILEBASE))
+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))