0

I want to have a folder full of any number of .tex files where I will have a single Makefile, which will generate a .pdf file for each .tex file when make is run. The constraint I have is that some, but not all of the .tex files will have a bibliography. .tex files with a bibliography will conditionally be built with the bibtex tool to generate the .bbl files to construct the references in the final .pdf file. This is required because bibtex will return an error code and abort the build process if it does not find a bibliography referenced in the text. Below is a proposed solution for the makefile, which does not work because ifneq is not evaluating on the value for FILE_BIBS.

Makefile (note: four spaces should be replaced with tabs or the interpreter will not function properly)

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 

outline.tex

\documentclass{article}
\usepackage{natbib}
\bibliographystyle{apa}

\begin{document}

Types of integration \cite[81-82]{INCOSE-Handbook}.

\bibliography{references}{}

\end{document}

outline2.tex

\documentclass{article}

\begin{document}

Hello.

\end{document}

references.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}
}

Output

$ 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

We expect to see a bibtex call when building outline, but we do not. This indicates that the second ifneq is not working correctly.

The Makefile is not working because the second ifneq is not working, evaluating true when FILE_BIBS is non-empty.The proposed should allow a user to build all .tex files in a folder by running make, new solutions outside the proposed solution should only use a command line interface, make, pdftex, bibtex, and standard tools in a Unix/Linux environment, such as awk, sed, grep.

  • 2
    See if you can install latexmk on your Unix. That would build your documents correctly without a Makefile. Trying to run LaTeX from Makefiles is known to be very difficult. – Kusalananda Jul 27 at 9:08
  • 1
    another good automation tool which can automatically determine if any bibtex runs are necessary is github.com/reitzig/ltx2any – samcarter Jul 27 at 12:58
  • @Kusalananda I do have access to latexmk and it does satisfy all the requirements of what I'm ultimately trying to accomplish. Thank you. – user1509669 Jul 27 at 15:30
1

Composing Makefiles for typesetting LaTeX documents is notoriously complicated. It is better to, if at all possible, use a tool such as latexmk which will automate running latex, bibtex etc. as needed.

Obviously, the running of latexmk could be put into a Makefile, especially if the documents are being built as part of a set of documents or as part of some software package that you are writing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.