In theprevious 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.
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.
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.
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"))))))
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%b")
("Skim" "displayline -b -g %n %o %b")))
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.
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.