[Tex/LaTex] How to make `latexmk` run the appropriate amount of times

auctexcompilingemacslatexmk

System:

Ubuntu 13.04, Texlive2012, Emacs 24.something, I run Emacs from the terminal and not the GUI, and I have server mode on in Emacs.

When compiling my latex documents, I use AucTeX in emacs to run latexmk but latexmk only runs once and will say you should run latexmk again and then reason whether it be references or something else. Isn't the point of latexmk to run the appropriate amount of times from the get go?

How can I have latexmk do this with out me telling emacs to run it again?

My latex portion of my .emacs is below in case there is something I need to add or remove from it to get the desired results.

;;============AucTex===========                                                 
(require 'tex-site)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)
(setq reftex-plug-into-AUCTex t)
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)

;;============ Latexmk setup==============                                        
(defun run-latexmk ()
  (interactive)
  (let ((TeX-save-query nil)
        (TeX-process-asynchronous nil)
        (master-file (TeX-master-file)))
    (TeX-save-document "")
    (TeX-run-TeX "latexmk"
                (TeX-command-expand "latexmk -pdflatex='pdflatex -file-line-error  \
                                  -synctex=1' -pdf %s" 'TeX-master-file)
                 master-file))
    (if (plist-get TeX-error-report-switches (intern master-file))
        (TeX-next-error t)
      (progn
        (demolish-tex-help)
        (minibuffer-message "latexmk: done."))))

(add-hook 'LaTeX-mode-hook (lambda ()
    (push
     '("Latexmk" "latexmk -pdf %s" TeX-run-TeX nil t
       :help "Run Latexmk on file")
     TeX-command-list)))

;; Set okular to open with C-c C-v view option                                      
(defun Okular-make-url () (concat
     "file://"
     (expand-file-name (funcall file (TeX-output-extension) t)
                       (file-name-directory (TeX-master-file))
                       "#src:"
                       (TeX-current-line)
                       (expand-file-name (TeX-master-directory))
                       "./"
                       (TeX-current-file-name-master-relative))))

(add-hook 'LaTeX-mode-hook '(lambda ()
        (add-to-list 'TeX-expand-list
                     '("%u" Okular-make-url))))
(setq TeX-view-program-list
      '(("Okular" "okular --unique %u")))
(setq TeX-view-program-selection '((output-pdf "Okular")))

(custom-set-variables
 ;; custom-set-variables was added by Custom.                                       
 ;; If you edit it by hand, you could mess it up, so be careful.                    
 ;; Your init file should contain only one such instance.                           
 ;; If there is more than one, they won't work right.                               
 '(TeX-PDF-mode t)
 '(TeX-newline-function (quote newline-and-indent))
 '(TeX-source-correlate-method (quote synctex))
 '(TeX-source-correlate-mode t)
 '(TeX-source-correlate-start-server t)
 '(TeX-view-program-list (quote (("Okular" "okular -unique %o#src:%n%b"))) t)
 '(TeX-view-program-selection (quote ((output-pdf "Okular"))) t)
 '(compilation-auto-jump-to-first-error t))

Below is my .latexmkrc file

$pdf_mode = 1;
$pdflatex = 'pdflatex -interaction=nonstopmode -file-line-error -synctex=1';

Edit

Per John Collins' request, here is the message I received when running latexmk

You should run LaTeX again to get references right, {9} pages

Even though it says LaTeX, latexmk was ran here is the output to show this:

Running `Latexmk' on `TidalForce' with ``latexmk -pdf TidalForce''
Latexmk: This is Latexmk, John Collins, 11 Nov. 2012, version: 4.35.
**** Report bugs etc to John Collins <collins at phys.psu.edu>. ****
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex': File changes, etc:
   Changed files, or newly in use since previous run(s):
      'TidalForce.tex'
------------
Run number 1 of rule 'pdflatex'

As John Collins mentioned, here is the better format for the .emacs and .latexmkrc file.

;; ============ Latexmk setup ==============                                        
(defun run-latexmk ()
  (interactive)
  (let ((TeX-save-query nil)
        (TeX-process-asynchronous nil)
        (master-file (TeX-master-file)))
    (TeX-save-document "")
    (TeX-run-TeX "latexmk"
                 (TeX-command-expand "latexmk -pdf %s" 'TeX-master-file)
                 master-file))
    (if (plist-get TeX-error-report-switches (intern master-file))
        (TeX-next-error t)
      (progn
        (demolish-tex-help)
        (minibuffer-message "latexmk: done."))))

(add-hook 'LaTeX-mode-hook (lambda ()
    (push
     '("Latexmk" "latexmk -pdf %s" TeX-run-TeX nil t
       :help "Run Latexmk on file")
     TeX-command-list)))

.latexmkrc setup:

$pdflatex = 'pdflatex --shell-escape -interaction=nonstopmode -file-line-error -synctex=1 %O %S';

Best Answer

In your definitions of the command lines for latexmk, change %s to %O %S. (Both the O and S are upper case.) The %S is the correct placeholder for the name of the source file, and the %O is for options that latexmk may place on the command line. With the lower case s that you have, latexmk doesn't do the substitution for the name of the source file, and the command line passed to pdflatex is not the intended one.

Note added

It turned out later that the above answer was misleading/misinpretable. There is a configuration of emacs to call latexmk correctly, and a configuration of latexmk to call pdflatex as is desired by the OP. Although latexmk can be configured through its command line, which was done in the emacs configuration file .emacs in the question, it is normally easier and safer to configure latexmk in one of its own configuration files. See TikZ: compiling a standalone picture for a better configuration.

Related Question