How to achieve auto-compilation for the org–>latex–>pdf publishing chain — configurations around latexmk


1 Summary:

  1. how to set up latexmk, a perl program that monitors the status of a tex and run compilation automatically
  2. my Emacs, bash/zsh, and latexmkrc configurations around this tool
  3. my workflow of generating org–>latex–>pdf using this tool
  4. a few notes/tricks

3 installation/configuration of latexmk

  1. no need to install, latexmk is included within MikTex package, CTex, and TexLive
    which latexmk
    latexmk is /cygdrive/c/CTEX/MiKTeX/miktex/bin/latexmk
  2. but the INSTALL file in the downloaded package is quite info-rich
  3. check availability and if it is working properly:
    latexmk -v

4 Workflow

  1. in the org file, have the following headers (the following uses beamer)
    +LATEX_CLASS: beamer
  2. in Emacs, edit org file, then C-c C-e l l to export to latex
  3. open a mintty shell (consolez will not work properly)
    1. type
      latexmk -pvc -pdf -view=none paper.tex
    2. a latexmk process will run, and keeps monitoring the status of the tex file “paper.tex”
    3. you can then type
      sumatrapdf -reuse-instance paper.pdf
    4. the tex will be automatically compiled into pdf, and sumatrapdf will update the view automatically
  4. why consolez does not work
    1. the $TERM type needs to be xterm-256 or xterm, for properly stopping the latexmk program by typing Control-C
    2. in zsh or bash of consolez, the $TERM is “cygwin”, and Control-C will not work

5 Configurations

  1. my .latexmkrc file in ~
    1. note the default location of .latexmkrc is in your $HOME directory
    2. the language of this configuration file is perl
    3. I specified sumatrapdf as the viewer, because Adobe will lock the file
    4. I added additional extensions for clean_ext
      $pdf_previewer = 'sumatrapdf -reuse-instance %O %S';
      $clean_ext = 'paux lox pdfsync out';
  2. my aliases in bash/zsh:
    # for viewing pdf but not locking the file
    alias pdfview='sumatrapdf -reuse-instance'
    # for easily use latexmk
    alias mklatex='latexmk -pvc -pdf'
    # -f for non stop
    alias mktex='latexmk -f -pvc -pdf -view=pdf'
  3. my set up for different latex classes in init-org:
    (add-to-list 'org-latex-classes
                   "\\documentclass [letterpaper, 10pt]{article}
                   ("\\section{%s}" . "\\section*{%s}")
                   ("\\subsection{%s}" . "\\subsection*{%s}")
                   ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                   ("\\subsubsubsection{%s}" . "\\subsubsubsection*{%s}")))
    (add-to-list 'org-latex-classes
                   "\\documentclass [letterpaper, 11pt]{article}\n
    \\usepackage[pdfpagemode = colorlinks, pdffitwindow = true, bookmarks = true, pdftoolbar = false, pdfmenubar = true, pdfnewwindow = true]{hyperref}\n
                   ("\\section{%s}" . "\\section*{%s}")
                   ("\\subsection{%s}" . "\\subsection*{%s}")
                   ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                   ("\\paragraph{%s}" . "\\paragraph*{%s}")
                   ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
    (add-to-list 'org-latex-classes
         \\setbeamertemplate{navigation symbols}{}\n
         \\tikzstyle{block}=[draw opacity=0.7,line width=1.4cm]\n
                   ("\\section{%s}" . "\\section*{%s}")

6 How to run this file as a demo

  1. C-c C-e l l to create the tex file
  2. invoke the latexmk command in a mintty terminal
mktex latex_auto_compilation.tex
  1. open the resultant pdf file in sumatrapdf
pdfview latex_auto_compilation.pdf
  1. change the option line #+LATEX_CLASS:compact, among report, article, MP (not working well now), beamer
  2. repeat step 1 and you will see the latexmk and sumatrapdf working automatically for you.