我想要一个包含任意数量的 .tex 文件的文件夹,其中我将有一个 Makefile,它将在make
运行时为每个 .tex 文件生成一个 .pdf 文件。我的限制是一些(但不是所有).tex 文件都有参考书目。带有参考书目的 .tex 文件将有条件地使用 bibtex 工具构建,以生成 .bbl 文件以构建最终 .pdf 文件中的参考文献。这是必需的,因为如果 bibtex 找不到文本中引用的参考书目,则 bibtex 将返回错误代码并中止构建过程。下面是建议的 makefile 解决方案,该解决方案不起作用,因为 ifneq 没有评估 FILE_BIBS 的值。
生成文件(注意:四个空格应替换为制表符,否则解释器将无法正常运行)
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。