I'm trying to make a function that compiles my latex documents from the command line. My problem is that the references never show up on the first run so I usually end up running this function twice... it's usually not a big deal unless I'm working on a large document.

Anyone know why the references might not be showing up first time through?

I based my function off what I think TexMaker is doing: Options/Configure TexMaker/Quick Build

PdfLaTeX + Bib(la)tex + PdfLaTeX (x2) + View Pdf

any thoughts?

generatePDF() {
NC='\033[0m' # No color                                                                                                

    echo -e "${GREEN}Running pdflatex ${NC}"
    pdflatex -synctex=1 -interaction=nonstopmode -enable-write18 -shell-escape $1.tex

    # TODO: needs more error testing for next commands...                                                              
    echo -e "${GREEN}Running bib(la)tex...${NC}"
    bibtex $1.aux

    echo -e "${GREEN}Running pdflatex again...${NC}"
    pdflatex -synctex=1 -interaction=nonstopmode -enable-write18 -shell-escape $1.tex

    echo -e "${GREEN}Opening pdf... ${NC}"
    gnome-open $1.pdf

cleanPDFdir() {
NC='\033[0m' # No color                                                                                                
    echo -e "${GREEN}Removing auxiliary files...${NC}"
    rm -v $1.pdf
    rm -v $1.aux
    rm -v $1.bbl
    rm -v $1.bbg
    rm -v $1.log
    rm -v $1.synctex*

alias pdfmake='generatePDF'
alias pdfclean='cleanPDFdir'

I run the function by just specifying the main file's name without the extension:

pdfmake my_tex_file

additional info from comments: I'm using emacs so started putting this script together so I could quickly compile like I did when using TexMaker's quick build.

The individual commands I just found here and on Stack Overflow so I don't really understand what each is doing.

  • 2
    The references (.bib or \ref - like) always need two runs. I suggest to use a Makefile and ordinary make. You should also mention that this a shell-script?
    – user31729
    Oct 21, 2015 at 17:50
  • 2
    specifying the separate independent steps within the main .tex file with arara might also be a possibility. (texdoc arara or go to texdoc.net and search for arara.) Oct 21, 2015 at 18:05
  • 1
    why do you want a script that runs pdflatex three times and bibtex as well?: you should hardly ever need to do that, after a typical edit you just need to run pdflatex once and not bibtex at all. Oct 21, 2015 at 19:10
  • 1
    You are reinventing the wheel: There are a lot of programs that solve your problem, e.g. arara or make or latexmk or ... Oct 21, 2015 at 19:57
  • 2
    also do you really want nonstopmode when running from the commandline? editors do that so they can parse the log and highlight errors etc but from the commandline seems more natural just to stop at the error so you can fix it and re-run. Oct 21, 2015 at 22:31

1 Answer 1


try latexmk my_tex_file or latexmk -xelatex my_tex_file

  • 1
    And what should it do? Please do not only tell a command, explain it! As it stands this is more a comment than an answer.
    – Mensch
    Oct 21, 2015 at 18:12
  • Well, people can just look up what latexmk is. If the user is using texlive, I'd say this is a good solution, as I'm a heavy latexmk user myself.
    – daleif
    Oct 21, 2015 at 18:43

You must log in to answer this question.

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