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
    pdflatex $(patsubst %.tex, %, $<) > /dev/null

.PHONY: clean
    -rm -f *.pdf *.log *.out *.aux *.dvi *.blg *.bbl 




Types of integration \cite[81-82]{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 "there are bibs FILE_BIBS: "
there are bibs FILE_BIBS: 
pdflatex  outline2 > /dev/null
pdflatex  outline > /dev/null
echo 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, 2019 at 9:08
  • 1
    another good automation tool which can automatically determine if any bibtex runs are necessary is github.com/reitzig/ltx2any
    – user360448
    Jul 27, 2019 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. Jul 27, 2019 at 15:30

1 Answer 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.

  • You can also add the flag -bibtex-cond.
    – zyy
    Aug 15, 2021 at 4:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .