5 # 1. texソースファイルと同じディレクトリーに本ファイル(latex.mk)をコピーする
6 # 2. Makefileに変数TEXTARGETS と「include latex.mk」を記述する
7 # 3. texソースファイルと同じディレクトリーで、make コマンドを実行する
10 # - 読み込むべき中間ファイルがないことや相互参照未定義の警告がある場合、LaTeX処理を最大4回繰り返す
11 # - \includegraphics命令がTeXファイルに含まれる場合、グラフィックファイルを挿入
12 # -- 挿入されたグラフィックファイルが更新されたときは、処理を開始
13 # -- 挿入されたグラフィックファイルがないときは、処理を中止
14 # -- 挿入されたグラフィックファイルに対するバウンディング情報ファイル(.xbb)を作成
15 # - \include、\input命令がTeXファイルに含まれる場合、TeXファイルを挿入
16 # -- 挿入されたTeXファイルが更新されたときは、処理を開始
17 # -- 挿入されたTeXファイルがないときは、処理を中止
18 # - \makeindex命令が含まれる場合、mendexで索引を作成
19 # - \bibliography命令が含まれる場合、BiBTeXで文献一覧を作成
22 # - tex-clean: TeX中間ファイル(auxなど)を削除。ターゲットに.dviが含まれていないときは.dviファイルを削除
23 # - xbb-clean: バウンディング情報ファイル(.xbb)を削除
24 # - tex-distclean: TeX中間ファイル、バウンディング情報ファイル、ターゲットファイル(PDF、.dvi)を削除
26 # === Makefile -- sample ===
27 # TEXTARGETS := report.tex
32 .PHONY: tex-warning tex-clean tex-distclean
46 EXTRACTBB := extractbb
58 @$(ECHO) "check current directory, or set TEXTARGET in Makefile."
65 # .fls: tex -recorderで生成されるファイルリスト
66 # .lof: 図リスト(\tableoffigures)
67 # .lot: 表リスト(\tableoftables)
69 # .toc: 目次(\tableofcontents)
71 TEX_INT := .aux .fls .lof .lot .out .toc .log
76 IND_INT := .idx .ind .ilg
82 ALL_INTERFILES = $(addprefix *,$(TEX_INT) $(IND_INT) $(BIB_INT) .d .*_prev)
84 .SECONDARY: $(wildcard ALL_INTERFILES)
86 # \tableofcontents命令をTeXファイルから検索する
89 $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
90 $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
91 $(SED) -n -e 's/.*\(\\tableofcontents\)/\1/p' \
94 # \listoffigures命令をTeXファイルから検索する
97 $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
98 $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
99 $(SED) -n -e 's/.*\(\\listoffigures\)/\1/p' \
102 # \listoftables命令をTeXファイルから検索する
105 $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
106 $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
107 $(SED) -n -e 's/.*\(\\listoftables\)/\1/p' \
110 # \makeindex命令をTeXファイルから検索する
113 $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
114 $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
115 $(SED) -n -e 's/.*\(\\makeindex\)/\1/p' \
118 # \bibliography命令で読み込まれる文献データベースファイルをTeXファイルから検索する
120 $(addsuffix .bib,$(basename $(strip $(shell \
121 $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
122 $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
123 $(SED) -n -e 's/\\bibliography\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
124 $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' | \
125 $(SED) -e 's/,/ /g' \
128 # hyperrefパッケージ読み込みをTeXファイルから検索する
131 $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
132 $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
133 $(SED) -n -e 's/.*\(\\usepackage\(\[[^]]*\]\)\{0,1\}{hyperref}\)/\1/p'\
136 # $(BASE).texで使われるLaTeX中間ファイル
139 $(if $(toc),$(BASE).toc) \
140 $(if $(lof),$(BASE).lof) \
141 $(if $(lot),$(BASE).lot) \
142 $(if $(makeindex),$(BASE).ind) \
143 $(if $(bibdb),$(BASE).bbl) \
144 $(if $(hyperref),$(BASE).out) \
147 INTERFILES_PREV = $(addsuffix _prev,$(INTERFILES))
149 # \include命令で読み込まれるTeXファイル
151 $(strip $(addsuffix .tex,$(shell \
152 $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex | \
153 $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
154 $(SED) -n -e 's/\\include\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
155 $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
158 # \input命令で読み込まれるTeXファイル
161 $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $1 | \
162 $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
163 $(SED) -n -e 's/\\input\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
164 $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
168 inputtex = $(call get_inputtex,$(BASE).tex $(includetex))
170 # \include命令または\input命令で読み込まれるTeXファイル
171 intex = $(strip $(includetex) $(inputtex))
173 # \includegraphics命令で読み込まれるグラフィックファイル
176 $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
177 $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
178 $(SED) -n -e 's/\\includegraphics\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
179 $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
183 LATEXCMD = $(LATEX) -interaction=batchmode $(LATEXFLAG) $(BASE).tex
184 COMPILE.tex = $(ECHO) $(LATEXCMD); $(LATEXCMD) >/dev/null 2>&1 || ($(CAT) $(BASE).log; exit 1)
187 WARN_UNDEFREF := 'There were undefined references\.'
190 # ログファイルに警告がある場合は警告がなくなるまで、最大CNTで指定された回数分、処理を実行する
193 @(for i in `$(SEQ) 1 $(CNT)`; do \
194 if test -s $@ -a -s $(BASE).log; then \
195 $(GREP) -e $(WARN_UNDEFREF) $(BASE).log || exit 0; \
197 $(ECHO) '$@ and/or $(BASE).log does not exist.'; \
204 DVIPDFCMD = $(DVIPDFMX) $(DVIPDFMXFLAG) $(BASE).dvi
205 COMPILE.dvi = $(ECHO) $(DVIPDFCMD); $(DVIPDFCMD) >>$(BASE).log 2>&1 || ($(CAT) $(BASE).log; exit 1)
208 MENDEXCMD = $(MENDEX) $(MENDEXFLAG) $(BASE).idx
209 COMPILE.idx = $(ECHO) $(MENDEXCMD); $(MENDEXCMD) >/dev/null 2>&1 || ($(CAT) $(BASE).ilg; exit 1)
211 # 文献リスト中間ファイル(.bbl)作成
212 BIBTEXCMD = $(BIBTEX) $(BIBTEXFLAG) $(BASE).aux
213 COMPILE.bib = $(ECHO) $(BIBTEXCMD); $(BIBTEXCMD) >/dev/null 2>&1 || ($(CAT) $(BASE).blg; exit 1)
215 # ターゲットファイルと必須ファイルを比較し、内容が異なる場合はターゲットファイルの内容を必須ファイルに置き換える
216 CMPPREV = $(CMP) $@ $< || $(CP) -p -v $< $@
220 @$(ECHO) '$@ is created by scanning $<.'
222 @$(ECHO) '$(BASE).d: $(BASE).tex' >$@
224 $(if $(INTERFILES),@( \
226 $(ECHO) '# LaTeX Intermediate Files'; \
227 $(ECHO) '$(BASE).dvi:: $(INTERFILES_PREV)'; \
228 $(ECHO) ' @$$(COMPILE.tex)'; \
230 $(ECHO) '$(BASE).dvi:: $(BASE).aux'; \
231 $(ECHO) ' @$$(COMPILES.tex)'; \
234 $(if $(ingraphics),@( \
236 $(ECHO) '# IncludeGraphic Files - .pdf, .eps, .jpeg/.jpg, .png'; \
237 $(ECHO) '# .xbb Files - .pdf, .jpeg/.jpg, .png'; \
238 $(ECHO) '$(BASE).aux: $(ingraphics)'; \
239 $(if $(filter-out %.eps,$(ingraphics)), \
241 $(ECHO) '$(BASE).aux: $(addsuffix .xbb,$(basename $(filter-out %.eps,$(ingraphics))))'; \
246 $(ECHO) '# Files called from \include or \input - .tex'; \
247 $(ECHO) '$(BASE).aux: $(intex)'; \
252 $(ECHO) '# Bibliography files: .aux, BIBDB -> .bbl -> .div'; \
253 $(ECHO) '$(BASE).bbl: $(bibdb) $(BASE).tex'; \
256 # 変数TEXTARGETSで指定されたターゲットファイルに対応する
257 # .dファイルをインクルードし、依存関係を取得する
258 # ターゲット末尾に clean、.xbb、.tex、.d が含まれている場合は除く
259 ifeq (,$(filter %clean %.xbb %.tex %.d,$(MAKECMDGOALS)))
260 -include $(addsuffix .d,$(basename $(TEXTARGETS)))
279 # pdf、jpeg/jpg、pngファイルに対応
281 $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
284 $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
287 $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
290 $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
294 @$(MAKE) -s $(BASE).aux
301 @$(MAKE) -s $(BASE).aux
308 @$(MAKE) -s $(BASE).aux
315 @$(MAKE) -s $(BASE).aux
328 @$(MAKE) -s $(BASE).aux
336 @$(MAKE) -s $(BASE).aux
343 $(RM) $(ALL_INTERFILES)
344 ifeq (,$(filter %.dvi,$(TEXTARGETS)))
353 tex-distclean: tex-clean xbb-clean
354 ifneq (,$(filter %.dvi,$(TEXTARGETS)))