Spacemacs and LaTeX
In the previous post, I
introduced several useful tips for editing LaTeX file using Vim. In a nutshell,
there are shortcuts (=
and gq
family), commands and plugins. In addition, I
also introduced latexmk
tool in the texlive
. By using -pvc
flag, the PDF
file will automatically generated once you save a tex file. However, long time
ago, I change my main editor from Vim to Emacs, more specifically, Spacemacs. If
you are interested in the reasons why I turned to Spacemacs. This post will give
you enough answers. I will mainly focus on the experience of editing LaTeX files
in Spacemacs.
Spacemacs
First of all, what’s is Spacemacs? It is an Emacs advanced Kit focused on Evil: The best editor is neither Emacs nor Vim, it's Emacs and Vim! Briefly speaking, you can get the powerful functions of Emacs along with the efficient editing experience of Vim. For me, the selling point may be the great documents. You don’t need to searching in the internet and hope to find posts from experienced users. The documents of Spacmeacs is like a real software manual including various introductions and handful examples. I recommend you to read through the features section in the documents. Here is an official screenshot.
There are plenty of configuration layers in the Spacemacs. For LaTeX editing,
two layers are enough. They are auto-completion
and latex
. If you include
these layers in the .spacemacs
file. It will automatically load related
packages when you open an tex
file. The first layer, auto-completion
is for
auto-completion. For example, you can simply type a e
and then M-/
to expand
to \emph{}
command in LaTeX. Besides tex file, this layer can serve other
functions when editing other kinds of files. Let’s mainly look into latex
layer.
AucTeX
The core of latex
layer is actually auctex
package. This package is
extremely powerful. I am sure I can’t list all functions in one post. I will
list the most useful functions in my view.
Build and View
The keybingings of build is SPC m b
and view is SPC m v
. If you press build
keybinding, it will build your tex file using latex
and generate a pdf
file.
By default, it will use pdflatex
. However, I use xelatex
sometimes to build
beamer source code. To do this, add a local variable in the end of a tex file
can tell auctex
to build as xetex.
%%% Local Variables:
%%% coding: utf-8
%%% mode: latex
%%% TeX-master: t
%%% TeX-command-extra-options: "-shell-escape"
%%% TeX-engine: xetex
%%% End:
By typing the view key binding, you can open the pdf file and tex file side by
side to preview the result. I recommend to use Skim
in OS X and Okular
in
Linux. Because both of them can sync pdf and tex file at runtime. Add following
lines in the dot file to automatically select preview.
(cond
((string-equal system-type "darwin")
(progn (setq TeX-view-program-selection '((output-pdf "Skim")))))
((string-equal system-type "gnu/linux")
(progn (setq TeX-view-program-selection '((output-pdf "Okular"))))))
Sync
Sometimes, when you editing a long tex file or beamer presentation, you want to
know the correlation between pdf file and tex file. The sync function provided
by auctex
can help you jump from tex to pdf and pdf to tex. Add the following
lines into the dot file
(setq TeX-source-correlate-mode t)
(setq TeX-source-correlate-start-server t)
(setq TeX-source-correlate-method 'synctex)
(setq TeX-view-program-list
'(("Okular" "okular --unique %o#src:%n`pwd`/./%b")
("Skim" "displayline -b -g %n %o %b")
("Zathura"
("zathura %o"
(mode-io-correlate
" --synctex-forward %n:0:%b -x \"emacsclient +%{line} %{input}\"")))))
After that, when you type SPC m v
the pdf viewer will highlight the line which
you are editing with yellow color. Furthermore, you can press shift
and
command
together and click on the pdf file, it will automatically jump to
corresponding line in the tex file. This is quite useful when you editing a
beamer presentation and want to jump to a page in the middle.
Misc
In addition to previous random thoughts, Spacemacs supports keybinding in Vim
such as =
and gq
to format a paragraph. Moreover syntax checking is also a
optional layer in the dot file. For auctex
, there are still a lot of
features. For example, you
can preview equation and formula in side source code. You can also easily insert
\cite{}
and search keywords in bib file. It can generate WYSIWYG inline
previews of mathematics, figures, and other constructs which are hard to grasp
in plain source view by means of the preview-latex subsystem.
At last, this is just an introduction.
UPDATE on 2015/11/17
Previously, we said to add TeX-view-program-list
and
TeX-view-program-selection
. I forgot to mention one important thing. You need
also to make Okular recognize Emacs. You should go "Settings -> Configure Okular
-> Editor" and select the Emacs client configuration.
Since Okular changed the command line interface for users, the command for sync
from Spacemacs to Okular should be update. Now, you can use ("Okular" "okular
--unique %o#src:%n`pwd`/./%b")
for sync.
Besides Okular in Linux, I also recommend another light weight PDF viewer. It's
name is Zathura. Same as Okular, Zathura
also support vim
key binding. But its shortcuts is more than traditional
hjkl
, you will see more if you read the manual. However, the configuration of
tex sync for Zathura is a little complicated. First, you need to create a shell
script for Zathura which I call it zathura-sync.sh
.
#!/bin/sh
pos="$1"
pdffile="$2"
zathura --synctex-forward "$pos" "$pdffile" || \
(
zathura -x "emacsclient --eval '(progn (find-file \"%{input}\") (goto-line %{line}))'" "$pdffile" &
sleep 1; zathura --synctex-forward "$pos" "$pdffile" )
exit
Secondly, you need to put this script into a directory which in the PATH
environment. My preference is to put all my executables in /home/username/bin
directory. At last, add zathura-sync.sh
script to tex view program list:
(setq TeX-view-program-list
'(("Okular" "okular --unique %o#src:%n`pwd`/./%b")
("Skim" "displayline -b -g %n %o %b")
("Zathura" "zathura-sync.sh %n:1:%b %o")))
;; and change default program to Zathura
(cond
((spacemacs/system-is-mac) (setq TeX-view-program-selection '((output-pdf "Skim"))))
((spacemacs/system-is-linux) (setq TeX-view-program-selection '((output-pdf "Zathura")))))
Finally, you can use Zathura simply run SPC m v
to vew PDF and use CTL + left
mouse
to jump into corresponding tex sentence.
UPDATE on 2016/08/05
Recently, I found that the latest AucTeX began to support more view programs. This means that you no longer
need to bother define your own TeX-view-program-list
. The official list is in
http://git.savannah.gnu.org/cgit/auctex.git/tree/tex.el#n1345.
(defvar TeX-view-program-list-builtin
(cond
((eq system-type 'windows-nt)
'(("Yap" ("yap -1" (mode-io-correlate " -s %n%b") " %o") "yap")
("dviout" ("dviout -1 "
((paper-a4 paper-portrait) "-y=A4 ")
((paper-a4 paper-landscape) "-y=A4L ")
((paper-a5 paper-portrait) "-y=A5 ")
((paper-a5 paper-landscape) "-y=A5L ")
((paper-b5 paper-portrait) "-y=E5 ")
((paper-b5 paper-landscape) "-y=E5L ")
((paper-b4jis paper-portrait) "-y=B4 ")
((paper-b4jis paper-landscape) "-y=B4L ")
((paper-b5jis paper-portrait) "-y=B5 ")
((paper-b5jis paper-landscape) "-y=B5L ")
(paper-legal "-y=Legal ")
(paper-letter "-y=Letter ")
(paper-executive "-y=Executive ")
"%d" (mode-io-correlate " \"# %n '%b'\"")) "dviout")
("SumatraPDF"
("SumatraPDF -reuse-instance"
(mode-io-correlate " -forward-search \"%b\" %n") " %o")
"SumatraPDF")
("dvips and start" "dvips %d -o && start \"\" %f" "dvips")
("start" "start \"\" %o")))
((eq system-type 'darwin)
'(("Preview.app" "open -a Preview.app %o" "open")
("Skim" "open -a Skim.app %o" "open")
("displayline" "displayline %n %o %b" "displayline")
("open" "open %o" "open")))
(t
`(("dvi2tty" ("dvi2tty -q -w 132 %o"))
("xdvi" ("%(o?)xdvi"
(mode-io-correlate " -sourceposition \"%n %b\" -editor \"%cS\"")
((paper-a4 paper-portrait) " -paper a4")
((paper-a4 paper-landscape) " -paper a4r")
((paper-a5 paper-portrait) " -paper a5")
((paper-a5 paper-landscape) " -paper a5r")
(paper-b5 " -paper b5")
(paper-letter " -paper us")
(paper-legal " -paper legal")
(paper-executive " -paper 7.25x10.5in")
" %d") "%(o?)xdvi")
("dvips and gv" "%(o?)dvips %d -o && gv %f" ,(list "%(o?)dvips" "gv"))
("gv" "gv %o" "gv")
("xpdf" ("xpdf -remote %s -raise %o" (mode-io-correlate " %(outpage)")) "xpdf")
("Evince" ,(TeX-view-program-select-evince "gnome" "evince") "evince")
("Atril" ,(TeX-view-program-select-evince "mate" "atril") "atril")
("Okular" ("okular --unique %o" (mode-io-correlate "#src:%n%a")) "okular")
("xdg-open" "xdg-open %o" "xdg-open")
("PDF Tools" TeX-pdf-tools-sync-view)
("Zathura"
("zathura %o"
(mode-io-correlate
" --synctex-forward %n:0:%b -x \"emacsclient +%{line} %{input}\""))
"zathura"))))
"Alist of built-in viewer specifications.
This variable should not be changed by the user who can use
`TeX-view-program-list' to add new viewers or overwrite the
definition of built-in ones. The latter variable also contains a
description of the data format.")