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.

tabbar mode rocks with customization!



Why doing this

I like tabbar mode, and it gives Emacs a “modern” feeling – similar to that available in most web browsers such as my
favorite Firefox.

However, I don’t like the default settings of tabbar mode. So here comes my customization:

color and separator

my current tabbar tabs looks:

And the customization code in .emacs file are:

(setq tabbar-background-color "#959A79") ;; the color of the tabbar background
 '(tabbar-default ((t (:inherit variable-pitch :background "#959A79" :foreground "black" :weight bold))))
 '(tabbar-button ((t (:inherit tabbar-default :foreground "dark red"))))
 '(tabbar-button-highlight ((t (:inherit tabbar-default))))
 '(tabbar-highlight ((t (:underline t))))
 '(tabbar-selected ((t (:inherit tabbar-default :background "#95CA59"))))
 '(tabbar-separator ((t (:inherit tabbar-default :background "#95CA59"))))
 '(tabbar-unselected ((t (:inherit tabbar-default)))))

key bindings:

I use C-S-< and C-S-> to jump among tabs in the same group, and use C-S-n and C-S-p to switch among groups. Here is the
code in .emacs file:

(global-set-key (kbd "C-S-p") 'tabbar-backward-group)
(global-set-key (kbd "C-S-n") 'tabbar-forward-group)
(global-set-key (kbd "C-<") 'tabbar-backward)
(global-set-key (kbd "C->") 'tabbar-forward) ;; tabbar.el, put all the buffers on the tabs.

my favorite tabbar customization: to toggle between 2 different grouping schemes

  • grouping tabs by major mode (the default)
  • grouping tabs by dir the files belonging to

here is the effect:

  • grouping by dir (see how the org, tex, html, and bbl buffers under the same publishing project are grouped together)

  • grouping by major modes (see how all org buffers are grouped together)

here is the code

(defun tabbar-buffer-groups-by-dir ()
        "Put all files in the same directory into the same tab bar"
        (with-current-buffer (current-buffer)
          (let ((dir (expand-file-name default-directory)))
            (cond ;; assign group name until one clause succeeds, so the order is important
             ((eq major-mode 'dired-mode)
              (list "Dired"))
             ((memq major-mode
                    '(help-mode apropos-mode Info-mode Man-mode))
              (list "Help"))
             ((string-match-p "\*.*\*" (buffer-name))
              (list "Misc"))
             (t (list dir))))))

(defun tabbar-switch-grouping-method (&optional arg)
  "Changes grouping method of tabbar to grouping by dir.
With a prefix arg, changes to grouping by major mode."
  (interactive "P")
    (if arg
      (setq tabbar-buffer-groups-function 'tabbar-buffer-groups) ;; the default setting
        (setq tabbar-buffer-groups-function 'tabbar-buffer-groups-by-dir))))

misc settings

(setq tabbar-cycle-scope (quote tabs))
(setq table-time-before-update 0.1)
(setq tabbar-use-images t)

Date: 2012-09-21

Author: Da Zhang

Org version 7.9.1 with Emacs version 24

Validate XHTML 1.0

LeechBlock – the killer of timekillers

Procrastination, the time-wasting and beating-around-the-bush type of behavior before any progress could be made on the real important things, is very typical among many people, including me.

I found that visiting my favorite websites before I start working is almost irresistible.

From Matt Might’s Productivity tips, tricks and hacks for academics and Boost your productivity: Cripple your technology, I learned about LeechBlock, which is perfect to block my favorite websites during hours I am supposed to work.

I can set up what to block (for example,, when to block (e.g., 0900-1730), how long to block (e.g., 1 min in every hour), what to show when a page is blocked (for me, I used the Journal of Medical Physics website for most recent articles)

To protect the disciplines from being violated, the LeechBlock is actually designed in a quite tricky and clever way:

  • I can set up password to the Option dialog box of LeechBlock for someone else, like my wife, to help them fighting procrastination together;
  • or I can let LeechBlock to require me to type in a 64 or 32 digit long randomly generated combination of letters and numbers before accessing its Option dialog box, which is really a pain. (see the pictures below)

When the path toward surfing online becomes more difficult to approach, it becomes easier for me to resist the desire to do so.

BTW, I don’t think I need to block IE, because I feel that using IE to surf online is as painful as by-passing the control of LeechBlock :-).

Date: 2012-03-25 17:38:16 Eastern Daylight Time

Author: Da Zhang

Org version 7.8.03 with Emacs version 24

Validate XHTML 1.0

My color theme color-theme-dawn-night is updated and uploaded to the marmalade repository (updated)

I have been spending time to tweak and design my own color themes since 2007, and published previous
versions (switch-color-theme-matlab-latex.el) on this blog (

I think now it’s a good time to move the package to the more popular host, marmalade, so anyone interested could use the
emacs package management tool (package.el) to obtain it. If marmalade is included in your package-archives list (see the
code below for this), you should be able to see the package “color-theme-dawn-night” after firing up list-packages.

(setq package-archives '(("gnu" . "")
                          ("marmalade" . "")))

You can also download the color-theme-dawn-night.el via

Here is a brief description of the color theme:

color-theme-dawn-night.el contains two customized color themes:

(1) cl-night
is a dark-background theme, designed for normal programming,
org/muse mode editing and LaTeX editing. The foreground colors are designed
to be somewhat aqua, with a bright and warm feeling.

(2) cl-dawn
is designed with a bright and warm background. The color setup was inspired
by the light yellow background and visual effect of the Matlab programming

Call function switch-dawn-night to change to the dark face, and call C-u
switch-dawn-night for the bright background face.

Install the theme:
(1)Place color-theme-dawn-night.el into your load-path.

(2)In your .emacs, add:
(require ‘color-theme-dawn-night)

(3)optional keybinding
(global-set-key [f6] ‘switch-dawn-night)


One good thing to tweak the color theme is that you can have a full control of the appearance of Emacs. For example, I adjusted different faces of tabbar to make them appear uniformly and as if in a real tabbar system.  Another example is the color of the line numbers on the left fringe — I made them slim and in a “dark gold” color.

Here are some screen shots of my color themes.

color theme dawn night:  night

color-theme-dawn-night: night

color theme dawn night:  dawn

color-theme-dawn-night: dawn

Date: 2012-03-24

Author: Da Zhang

Validate XHTML 1.0

Using org-mode as the ultimate note taking tool (updated)

Whenever there is something important to memorize, I always want to write it down. In fact, Emacs org-mode makes the process easy, pleasant, and in an organized manner. I will use an example to explain how Emacs and org fit into my note-taking process.

I have a dedicated folder containing all my notes, and IT, as one of the most important topic, is a sub-folder. Here is the tree structure of the IT folder.

|-- Acrobat
|   `--
|-- Emacs
|   |-- debug-lisp.html
|   |--
|   |-- emergency-elisp-steve-yegge.html
|   |-- emergency-lisp.html
|   |--
|   |-- lisp-programming.html
|   `--
|-- Excel
|   `-- group_formatting.txt
|-- Firefox
|   `-- Firefox Recovery Key.html
|-- HPC
|   |-- Setup_GATE_tool_chain_20100902.lnk
|   |-- bashrc_setup
|   |   `-- bashrc_HPC
|   |-- efficient-way-of-working.html
|   |--
|   |-- tricks.html
|   |--
|   `-- xinhua-hps-bashrc
|-- Linux_commands
|   |-- cat.html
|   |--
|   |-- count_files_in_dir.html
|   |--
|   |-- find.html
|   |--
|   |-- find.txt
|   |-- linux_commands.html
|   |--
|   |-- scp.txt
|   |-- sed.html
|   |--
|   |-- sed_commandline_editor.txt
|   `-- tar.txt
|-- VBA
|   `--
|-- X11_cygwin
|   `--
|-- c++
|   |-- ACM-UNIX-Tutorial-Compiler.html
|   |-- C++Quick-Ref.html
|   |-- C++Std-Lib-Quick-Ref.html
|   |-- Makefiles-short.html
|   `-- gcc-intro.html
|-- cygwin
|   `--
|-- g++
|   |-- g++compiling.txt
|   |-- gcc&g++.html
|   |-- gcc&
|   `-- note.txt
|-- gdb
|   |-- debug-tools.html
|   `--
|-- latex
|   |-- cygwin_latex_problem.html
|   |--
|   |-- sumatrapdf-make-emacs_integration.html
|   `--
|-- make
|   |-- note.html
|   `--
|-- python
|   |-- math.html
|   `--
|-- shell
|   |-- bashrc_and_others.htm
|   |-- bashrc_and_others_files
|   |   |-- css_2ad809fd00fb2c1d453a0d2b83bf7b57.css
|   |   |-- ga.js
|   |   |-- gumdrop.png
|   |   |-- index2.jpg
|   |   |-- js_957ff3e13d89532193faea68e21100f9.js
|   |   |-- js_c59e4e9c853d32975fd97f7345ddbabe.js
|   |   `-- show_ads.js
|   |-- notes.html
|   |--
|   |-- programming.html
|   `--
`-- ssh
    |-- How to set up SSH (for the beginner).pdf
    |-- no_password_access.html

As can be seen, almost every note has two forms: one org file, one html file. The reason that I am showing the IT folder here is to show how I organize my notes through a tree structure representing the hierarchy of related topics.

When I want to take a note, I always think first where does the topic fit into my hierarchal folder systems. Take the Linux command sed for example, I feel like it should be put in IT/Linux_commands/. Thus I enter a emacs function “notes”, which is defined in my .emacs file as

(defun notes ()
  "Switch to my work dir."
   (find-file "<PATH TO MY NOTES>")

This quickly bring me to my top level dir of all notes, in a dired buffer. Then I quickly follow the hierarchy to IT/Linux_commands/, where I open a file called “” via [C-x C-f]

When the file is open, I call another function “make-header”, which I customized from header2.el  for my own purpose. The function will generate a file header for me automatically, here is the result for the

#+TITLE: sed
#+AUTHOR: Da Zhang
#+OPTIONS: H:2 num:t toc:nil
#+OPTIONS: ^:nil
#+OPTIONS: <:nil todo:nil *:t ^:{} @:t ::t |:t TeX:t
#how to change image size: #+attr_html: width="40%"

Basically it will do the following:

  1. set the title to be the file name of the buffer, without its parent path and extension;
  2. set up the options for org mode
  3. insert a note for myself about changing the picture size in org-to-html export, which I still did not remember

Then I just start writing down the quick note about sed.

* how to use variable in a sed: use "" instead of ''
 - example: note the $1 in the double quotes of the 2nd sed

function sortcsvp() {
    cat $1 | sed '1d' | tr -d '"' | sort -n -t, -k 1 | sed "1 i  $1, , " > ./sort-by-protocol/$1;

In most cases I will use #+begin_src and #+end_src for enclose any code, for better appearance in the html output.

At the end, I will just press C-c C-e to export the entire buffer to html, which I will view more frequently from Firefox.

To conclude, Emacs and org-mode offered me a comfort environment for systematically taking notes, organize them, and exporting them into beautiful html pages.


Thanks to Irreal’s comment.
Here is something more I want to add about using org to take notes:

(1) the org-html process is almost perfect if you want to include pictures (for example, taken from your smart phone) in the note. Writing a org file that links to the local picture file using the [[][]] structure is so easy and natural. With the html publication, your output note file contains not only text, but also graphics and pictures that makes the note taking more efficient and direct. I used this feature so often that I need to put the following trick into my org header:

#how to change image size: #+attr_html: width="40%"

(2) the hack of header2.el I talked about making is essentially putting the following lines in header2.el:

(defsubst header-org-mode ()
  "Insert \"#+TITLE: ...\" at the beginning of org file."
  (insert "#+TITLE: " (file-name-nondirectory (file-name-sans-extension (buffer-file-name))) "\n") ;; filename only, without dir or extension
  (insert "#+AUTHOR: " (user-full-name) "\n")
  (insert "#+OPTIONS: H:2 num:t toc:nil\n")
  (insert "#+OPTIONS: ^:nil\n")
  (insert "#+OPTIONS: <:nil todo:nil *:t ^:{} @:t ::t |:t TeX:t\n")
  (insert "#how to change image size: #+attr_html: width=\"40%\"\n"))

Date: 2012-03-24 02:23:35 Eastern Daylight Time

Author: Da Zhang

Org version 7.8.03 with Emacs version 24

Validate XHTML 1.0

SumatraPDF integration in LaTeX-Emacs-make tool chain


SumatraPDF integration in LaTeX-Emacs-make tool chain


1.upgrade to the most recent SumatraPDF

2.make sure sumatrapdf is in the search path

3.update make file for the make integration





*.log   \
*.blg   \
*.bbl   \
*.aux   \
*.lof   \
*.lot   \

LATEXOPTIONS = -src-specials -interaction=nonstopmode

PDFVIEWER = sumatrapdf -reuse-instance -inverse-search "c:\emacs\bin\emacsclientw.exe +%l \"%f\""

BIBDIR = "C:/H/Bib"

BIBOPTIONS = -include-directory=$(BIBDIR)

vpath %.bib /cygdrive/c/H/Bib

viewpdf : $(MAIN).pdf
    $(PDFVIEWER) $(MAIN).pdf &

dvi : $(MAIN).dvi
    yap -1  $(MAIN).dvi

$(MAIN).pdf : $(MAIN).dvi
    pdfclosem $(MAIN).pdf ; dvipdfm $(MAIN).dvi

$(MAIN).dvi : $(MAIN).bbl $(EPS)
    latex $(LATEXOPTIONS) $(MAIN)
        if ( grep 'Rerun' $(MAIN).log > /dev/null ) ; then\
            latex $(LATEXOPTIONS) $(MAIN) ; \
        else :; fi

$(MAIN).bbl : $(TEX)
    rm -f $(MAIN).aux *.bbl; latex $(LATEXOPTIONS) $(MAIN); bibtex $(BIBOPTIONS) $(MAIN) 

clean :
    rm -f $(REBUILDABLES)

4.update .emacs for AucTeX integration

(setq TeX-view-program-list
      '(("sumatra" "sumatrapdf -reuse-instance \"%f\"")
        ("Yap" ("yap -1" (mode-io-correlate " -s %n%b") " %o"))
        ("dvips and start" "dvips %d -o && start \"\" %f")
        ("start" "pdfviewm %o")))

(setq TeX-view-program-selection
      '(((output-dvi style-pstricks)
         "dvips and start")
        (output-dvi "Yap")
        (output-pdf "sumatra")
        (output-html "start")))

if I need inverse search, pub /usepackage{pdfsync} before \begin{document}, or use pdflatex instead of latex

why using sumatrapdf? it does not lock the pdf file, so when restarting the make process, I don’t need to close the pdf file manually.

Date: 2012-02-16

Author: Da Zhang

Org version 7.8.03 with Emacs version 24

Validate XHTML 1.0

tried org2blog mode successfully

Following the blog of Gabriel Saldana “Post to WordPress blogs with Emacs & Org-mode” at\_source=feed&utm\_medium=feed&utm\_campaign=feed I tested the org2blog mode.

The set up is a little different from what was described in the above post: I put the following code in my .emacs file:

(require ‘org2blog-autoloads) (setq org2blog/wp-blog-alist ‘((“wordpress” :url “” :username “username” :tags-as-categories nil)))

You have to substitute the “username” with your actual username of wordpress.

To use the org2blog, you can M-x org2blog/wp-login first, and then M-x org2blog/wp-new-entry.