我想要一个包含任意数量的 .tex 文件的文件夹,我将在其中拥有一个 Makefile,它会在make
运行时为每个 .tex 文件生成一个 .pdf 文件。我的限制是一些但不是所有的 .tex 文件都有参考书目。带有参考书目的 .tex 文件将有条件地使用 bibtex 工具生成 .bbl 文件,以在最终 .pdf 文件中构建参考。这是必需的,因为如果 bibtex 没有找到文本中引用的参考书目,它将返回错误代码并中止构建过程。下面是生成文件的建议解决方案,它不起作用,因为 ifneq 没有评估 FILE_BIBS 的值。
Makefile(注意:四个空格应该用制表符代替,否则解释器将无法正常工作)
SOURCES:=$(wildcard $(SRC_DIR)*.tex)
BIBS:=$(wildcard $(SRC_DIR)*.bib)
OUTPUT:=$(patsubst %.tex, %.pdf, $(SOURCES))
.PHONY: all
all: $(OUTPUT)
# Build procedure for all .tex files
%.pdf: %.tex $(BIBS)
pdflatex $(patsubst %.tex, %, $<) > /dev/null
# Scan the current .tex file for the phrase \bibliography and put the text
# into a variable called FILE_BIBS, if there is no bibliography phrase in
# the file, FILE_BIBS will be empty
$(eval FILE_BIBS=$(shell grep -m 1 '^[[:space:]]*\\bibliography[[:space:]]*{' $< | sed 's/\\//g' | sed 's/{//g' | sed 's/}//g'))
echo $(FILE_BIBS)
# If there are .bib files in the project
ifneq ($(BIBS),)
echo "there are bibs FILE_BIBS: $(FILE_BIBS)"
# See if FILE_BIBS is not empty (the file uses a bibliography)
ifneq ($(FILE_BIBS),)
# This should print out for outline.tex and not for outline2.tex
# This does not print out in either case
echo "file has bib"
bibtex $(patsubst %.tex, %, $<) > /dev/null
pdflatex $(patsubst %.tex, %, $<) > /dev/null
endif
endif
pdflatex $(patsubst %.tex, %, $<) > /dev/null
.PHONY: clean
clean:
-rm -f *.pdf *.log *.out *.aux *.dvi *.blg *.bbl
大纲.tex
\documentclass{article}
\usepackage{natbib}
\bibliographystyle{apa}
\begin{document}
Types of integration \cite[81-82]{INCOSE-Handbook}.
\bibliography{references}{}
\end{document}
大纲2.tex
\documentclass{article}
\begin{document}
Hello.
\end{document}
参考文献.bib
@BOOK{INCOSE-Handbook,
TITLE = {Systems Engineering Handbook},
SUBTITLE = {A guide for system life cycle processes and activities},
AUTHOR = {Walden, David D. and Roedler, Gerry J. and Forsberg, Kevin J. and Hamelin, R. Douglas and Shortell, Thomas M.},
YEAR = {2015},
PUBLISHER = {Wiley},
EDITION = 4,
ADDRESS = {7670 Opportunity Rd., Suite 220 San Diego, CA, USA 92111-2222}
}
输出
$ make
pdflatex outline2 > /dev/null
echo
echo "there are bibs FILE_BIBS: "
there are bibs FILE_BIBS:
pdflatex outline2 > /dev/null
pdflatex outline > /dev/null
echo bibliographybibliography
bibliographybibliography
echo "there are bibs FILE_BIBS: bibliographybibliography"
there are bibs FILE_BIBS: bibliographybibliography
pdflatex outline > /dev/null
我们希望在构建大纲时看到 bibtex 调用,但我们没有。这表明第二个 ifneq 工作不正常。
Makefile 不起作用,因为第二个ifneq
不起作用,当FILE_BIBS
非空时评估 true。建议应允许用户通过运行在文件夹中构建所有 .tex 文件,make
建议解决方案之外的新解决方案应仅使用命令行interface、make、pdftex、bibtex 和 Unix/Linux 环境中的标准工具,例如 awk、sed、grep。