Skip to content

Instantly share code, notes, and snippets.

@kjhealy
Created January 21, 2011 14:14
Show Gist options
  • Save kjhealy/789717 to your computer and use it in GitHub Desktop.
Save kjhealy/789717 to your computer and use it in GitHub Desktop.
Use latexmk to manage pdf export in orgmode, choosing latex/pdflatex/xelatex as needed
(require 'org-latex)
(setq org-export-latex-listings t)
;; Originally taken from Bruno Tavernier: http://thread.gmane.org/gmane.emacs.orgmode/31150/focus=31432
;; but adapted to use latexmk 4.20 or higher.
(defun my-auto-tex-cmd ()
"When exporting from .org with latex, automatically run latex,
pdflatex, or xelatex as appropriate, using latexmk."
(let ((texcmd)))
;; default command: oldstyle latex via dvi
(setq texcmd "latexmk -dvi -pdfps %f")
;; pdflatex -> .pdf
(if (string-match "LATEX_CMD: pdflatex" (buffer-string))
(setq texcmd "latexmk -pdf %f"))
;; xelatex -> .pdf
(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(setq texcmd "latexmk -pdflatex=xelatex -pdf %f"))
;; LaTeX compilation command
(setq org-latex-to-pdf-process (list texcmd)))
(add-hook 'org-export-latex-after-initial-vars-hook 'my-auto-tex-cmd)
;; Default packages included in every tex file, pdflatex or xelatex
(setq org-export-latex-packages-alist
'(("" "graphicx" t)
("" "longtable" nil)
("" "float" nil)))
(defun my-auto-tex-parameters ()
"Automatically select the tex packages to include."
;; default packages for ordinary latex or pdflatex export
(setq org-export-latex-default-packages-alist
'(("AUTO" "inputenc" t)
("T1" "fontenc" t)
("" "fixltx2e" nil)
("" "wrapfig" nil)
("" "soul" t)
("" "textcomp" t)
("" "marvosym" t)
("" "wasysym" t)
("" "latexsym" t)
("" "amssymb" t)
("" "hyperref" nil)))
;; Packages to include when xelatex is used
;; (see https://github.com/kjhealy/latex-custom-kjh for the
;; non-standard ones.)
(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(setq org-export-latex-default-packages-alist
'(("" "fontspec" t)
("" "xunicode" t)
("" "url" t)
("" "rotating" t)
("" "memoir-article-styles" t)
("american" "babel" t)
("babel" "csquotes" t)
("" "listings" nil)
("" "listings-sweave-xelatex" nil)
("svgnames" "xcolor" t)
("" "soul" t)
("xetex, colorlinks=true, urlcolor=FireBrick, plainpages=false, pdfpagelabels, bookmarksnumbered" "hyperref" nil)
)))
(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(setq org-export-latex-classes
(cons '("article"
"\\documentclass[11pt,article,oneside]{memoir}
\\input{vc}
\\usepackage[style=authoryear-comp-ajs, abbreviate=true]{biblatex}
\\bibliography{socbib}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
org-export-latex-classes))))
(add-hook 'org-export-latex-after-initial-vars-hook 'my-auto-tex-parameters)¢
@bhyde
Copy link

bhyde commented Apr 8, 2012

(let ((texcmd))) --> (let ((texcmd)) ... ) ?

@trenkert
Copy link

Would it be possible to update this to org 8? I guess "org-latex" would need to becom "ox-latex" but other than that I have trouble understanding your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment