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
                 '("compact"
                   "\\documentclass [letterpaper, 10pt]{article}
    \\usepackage{fullpage}
    \\usepackage{setspace}
    \\singlespacing
    \\usepackage{times}
    \\usepackage[compact]{titlesec}
    \\titlespacing{\\section}{0pt}{*0}{*0}
    \\titlespacing{\\subsection}{0pt}{*0}{*0}
    \\titlespacing{\\subsubsection}{0pt}{*0}{*0}
    \\usepackage{mdwlist}
    \\usepackage[T1]{fontenc}
    \\usepackage[left=2cm,top=1cm,right=2cm,nohead,nofoot]{geometry}"
                   ("\\section{%s}" . "\\section*{%s}")
                   ("\\subsection{%s}" . "\\subsection*{%s}")
                   ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                   ("\\subsubsubsection{%s}" . "\\subsubsubsection*{%s}")))
    
    (add-to-list 'org-latex-classes
                 '("article"
                   "\\documentclass [letterpaper, 11pt]{article}\n
    \\usepackage{fullpage}\n
    \\usepackage{setspace}\n
    \\doublespacing\n
    \\usepackage{amssymb}\n
    \\usepackage{graphicx}\n
    \\usepackage{subfigure}\n
    \\usepackage{float}\n
    \\usepackage{amsmath}\n
    \\usepackage{cite}\n
    \\usepackage{times}\n
    \\usepackage[T1]{fontenc}\n
    \\usepackage[pdfpagemode = colorlinks, pdffitwindow = true, bookmarks = true, pdftoolbar = false, pdfmenubar = true, pdfnewwindow = true]{hyperref}\n
    \\usepackage[left]{lineno}\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
                 '("beamer"
                   "\\documentclass[red]{beamer}\n
         \\usetheme{Darmstadt}\n
         \\usefonttheme[onlylarge]{structurebold}\n
         \\setbeamerfont*{frametitle}{size=\\normalsize,series=\\bfseries}\n
         \\setbeamertemplate{navigation symbols}{}\n
         \\usepackage[english]{babel}\n
         \\usepackage{times}\n
         \\usepackage[T1]{fontenc}\n
         \\usepackage{graphicx}\n
         \\usepackage{color}\n
         \\usepackage{tikz}\n
         \\usetikzlibrary{arrows}\n
         \\tikzstyle{block}=[draw opacity=0.7,line width=1.4cm]\n
         \\usepackage{listings}
         \\lstset{numbers=none,language=[ISO]C++,tabsize=4,
         frame=single,
         basicstyle=\\small,
         showspaces=false,showstringspaces=false,
         showtabs=false,
         keywordstyle=\\color{blue}\\bfseries,
         commentstyle=\\color{red},
         }\n
         \\institute{BIDMC}\n"
                   ("\\section{%s}" . "\\section*{%s}")
                   ("\\begin{frame}[fragile]\\frametitle{%s}"
                    "\\end{frame}"
                    "\\begin{frame}[fragile]\\frametitle{%s}"
                    "\\end{frame}")))
    

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.