
Emacs as the Ultimate LaTeX Editor - pmoriarty
http://piotrkazmierczak.com/2010/05/13/emacs-as-the-ultimate-latex-editor/
======
mih
Anyone seriously considering Emacs for LaTeX editing should also take a look
at the _yasnippet_ package. The customizable autocompletion shortcuts make it
really easy to insert commands using just keywords. In my case this has proved
to be very helpful especially when inserting figures or inline equations. I
can create a snippet so that when I type _fig_ +<tab> it inserts a
\begin{figure}.. \end{figure}float and positions the cursor at the proper
field saving me a lot of keystrokes.

~~~
josteink
I've stopped using yasnippet because I find it too resource-intensive. It's
especially noticeable on emacs-startup, even of good machines, and I've found
myself not using it very often. So I decided that it wasn't worth the time.

Just as a FYI to people on weaker hardware.

~~~
pmoriarty
My main gripe with yasnippet is that its feature list is pretty lackluster
compared to some advanced vim snippet plugins like xptemplate.[1]

xptemplate has a bazillion features, and yasnippet is primitive in comparison.
Just a simple example is that in xptemplate (and probably any respectable
snippet plugin in vim) you can type a part of a snippet name, hit TAB, and it
will expand the snippet whose partial name you typed (assuming there's no
exact match). In yasnippet, you have to type out the full snippet name before
it can be expanded. This is pretty annoying, especially for long snippet
names.

There are tons of other examples of far more advanced functionality, such as a
variety of snippet repetition and embedding features.

This comparative lack of features in yasnippet is pretty surprising, because
emacs extensions are usually very feature rich, and everyone seems to love
yasnippet. But I think that's because most emacs users don't know what they're
missing.

And before someone says it, yes, I know yasnippet gives you the ability to
embed eLisp code in it, so I could potentially write all the functionality I
need myself. But that's really beside the point. I'm talking about built-in
features, not the ability to code up the features myself.

[1]
[https://github.com/drmingdrmer/xptemplate](https://github.com/drmingdrmer/xptemplate)

~~~
tuhdo
> _Just a simple example is that in xptemplate (and probably any respectable
> snippet plugin in vim) you can type a part of a snippet name, hit TAB, and
> it will expand the snippet whose partial name you typed (assuming there 's
> no exact match). In yasnippet, you have to type out the full snippet name
> before it can be expanded. This is pretty annoying, especially for long
> snippet names._

Yes, you can do that as well. See the list of expansion methods with
screenshots inside: [https://capitaomorte.github.io/yasnippet/snippet-
expansion.h...](https://capitaomorte.github.io/yasnippet/snippet-
expansion.html#sec-2-3). You may interest in minibuffer prompting.

You already have a built-in template system in Emacs called Srecode that
allows you to program your templates:
[http://cedet.sourceforge.net/srecode.shtml](http://cedet.sourceforge.net/srecode.shtml)

According to the homepage, "the core template text is based loosely on Google
ctemplate".

~~~
pmoriarty
I was hoping someone would say that was possible. Thanks, I'll play around
with the available yas-prompt-functions.

As for SRecode, that looks interesting but its documentation is impenetrable.
Someone seriously needs to write a bunch of tutorials for that thing.
Otherwise, I can't really tell if it can do what xptemplate is capable of or
not.

I found some comments from SRecode's author suggesting that it's intended for
different uses from yasnippet:

    
    
      "Thus, the audience for yasnippet and SRecode is quite different.
      Of course you could do yasnippet like things with SRecode if someone
      took the time to improve the field-editing feature in SRecode, but
      that has not been my focus."[1]
    

[1] - [http://emacs-fu.blogspot.com/2010/09/interview-with-eric-
lud...](http://emacs-fu.blogspot.com/2010/09/interview-with-eric-ludlam-
cedet.html)

~~~
tuhdo
I agree Srecode needs to improve its documentation. It took me a while to
learn to use it by reading the actual templates written that come with CEDET.
I may write a tutorial for it at some point. But not now.

You can have a look at a sample template:
[https://github.com/emacsmirror/cedet/blob/f72ebbc52785013280...](https://github.com/emacsmirror/cedet/blob/f72ebbc52785013280fddea8b6b1626ddd7ee6b4/etc/srecode/c.srt)
. Srt file has syntax highlighting in Emacs.

------
vsbuffalo
I hate the best editor debate because I think it's distracting from what's
more important — what both editors can learn from each other and what both
need to do to improve. I used Emacs for years, switched to Vim because of RSI,
then recently switched back to emacs+evil. Frankly, for what I do most (R,
R+knitr, C++ with clang autocomplete), no single editor is great. First,
there's too little ability to switch between modes within a single buffer in
both Vim and Emacs. The feature's entirely lacking in Vim AFAIK, and poly-mode
in R uses a high level hack that (1) doesn't play well with other modes
(including evil) and (2) has so thoroughly destroyed my documents in the past
I refuse to use it now (mostly because it uses many buffers behind the scenes,
which _destroys_ undo history).

In general, if you want flawless R support in certain blocks of text (as in a
.Rnw file) in between LaTeX blocks that are fully connected to AucTeX, well...
you're out of luck. And Vim... Vim-R-Plugin is useful, but it's sort of a
painful hack to use tmux just to get R and Vim to talk (and I'm saying this
even though I love Tmux).

Vim has YouCompleteMe, which is smooth as silk compared to Emac's options
(which are painful and poorly integrated, especially with clang). But some
lower-level issue in Vim causes this constant error message in Vim whenever
YouCompleteMe uses clang — bloody annoying. So overall, both editors have huge
issues that would require serious overhauls or tedious bug fixing in various
modes. Sure, Emacs does AucTeX better, but until it does _everything_ better
(or Vim does _everything_ better) it's a flawed editor. Both are flawed
editors. But sadly everyone thinks the best course of action is to start fresh
— which usually creates a feature-poor flawed editor on a new shiny
foundation, that fails to attract developers because it's feature poor.
(apologies for ranting -- jetlag).

~~~
chongli
Neovim has a good chance to become the best editor. They are taking a really
hard stance against all the cruft in Vim and have some really great features
already implemented. I'm especially excited about their goal of reimplementing
all IO with libuv. The lack of proper asynchronous IO has been a sore point in
vim and emacs for a long, long time.

~~~
catern
Uh, Emacs has asynchronous IO, and it works fine.
[http://www.gnu.org/software/emacs/manual/html_node/elisp/Asy...](http://www.gnu.org/software/emacs/manual/html_node/elisp/Asynchronous-
Processes.html)

~~~
chongli
It has support for AIO, as had vim for quite a while, but it's not pervasive
in the standard library and the ecosystem. Both editors have way too many
plugins that block the UI needlessly while you're typing (autocompletion,
syntax checking etc).

~~~
tuhdo
It's quite more popular than you think. `M-x term` is an example. It's a full-
blown terminal emulator in Emacs that runs entirely in another process. Here
is another example of async grep/ack/ag: [http://tuhdo.github.io/helm-
projectile.html#sec-9](http://tuhdo.github.io/helm-projectile.html#sec-9) . As
you can see, Emacs gets line by line results from grep/ack/ag without blocking
and gives you new results and you type.

Or Emacs's fantastic support for communicating with REPLs in many interpreted
languages.

You can execute any shell command asynchronously while doing other things
inside Emacs. You can even compile with beautiful output, in terminal:
[http://tuhdo.github.io/static/c-ide/compilation-
compile.gif](http://tuhdo.github.io/static/c-ide/compilation-compile.gif)

Or built-in GDB integration, also asynchronous:
[http://tuhdo.github.io/static/c-ide/gdb-many-
windows.gif](http://tuhdo.github.io/static/c-ide/gdb-many-windows.gif).

Another GDB screenshot: [https://flic.kr/p/6FW7Q7](https://flic.kr/p/6FW7Q7).

And Emacs officially supports asynchronous IO, while in Vim it's a hack from
Vim Script.

------
dredmorbius
I'm seeing a lot of "X is best" or "y additional feature is powerful".

Neither adds much to the discussion.

Which isn't to say that posts such as this can't provide value. I've been
using vim and its antecedents for over a quarter century, as well as well over
another editors and authoring tools (closer to two dozen). Including a few
forays into emacs-land.

I find discussions of tools useful, and have added a few to my own suite in
the past year. Most recently vim-outliner, an outlining plug-in for vim, also
Pandoc, and in the past two years, for the first time after considering it for
a long time, LaTeX.

 _Describing WHY an editor or tool suits you, or SPECIFIC features it has or
offers, is far more useful._

It will also make the discussion here hugely more valuable than just pointing
people at vim or emacs (again). Both are pretty easy to find without that
help.

Providing reasons and specific benefits -- much more valuable.

------
JoshTriplett
I switched to vim years ago for everything else, but I still use emacs
specifically for auctex. I have yet to find a vim equivalent nearly as
convenient. In particular, C-c C-c as a do what I mean compiler (checking
timestamps to figure out whether to run LaTeX, BibTeX, etc, as well as C-c C-e
to insert an environment, and alt-enter to insert a newline and \item.

~~~
pmoriarty
I just use snippets (via xptemplate) to do LaTeX editing in vim. For running
LaTeX, BibTeX, pdftex, etc, I use LatexMk.[1]

I would be interested to hear what AucTEX provides that the above combination
can't.

[1] -
[http://www.emacswiki.org/emacs/LatexMk](http://www.emacswiki.org/emacs/LatexMk)

~~~
JoshTriplett
I don't find snippets nearly as convenient as the built-in auctex keybindings.
C-c C-e itemize <enter> item1 <alt-enter> item2 gives me (properly indented
for surrounding context):

    
    
        \begin{itemize}
        \item item1
        \item item2
        \end{itemize}
    

I could probably convince vim to do that, but auctex does it out of the box
with no additional configuration required.

Likewise, I could probably set makeprg to latexmk or rubber or some other DWIM
script, but that ought to Just Work as easily as auctex's C-c C-c, and
shouldn't require a pile of configuration.

I haven't even found a way to bind <A-Enter> in vim's insert mode, because vim
can't distinguish that from <Esc><Enter>.

~~~
pmoriarty
In vim, using xptemplate:

    
    
      item<TAB>item1<TAB><TAB>item2<C-d>
    

results in exactly the same thing. True, I had to spend 5 minutes writing a
snippet to do that,[1] but that was because the default snippet behaved
differently (though arguably just as intuitevely).

xptemplate comes with a bunch of LaTeX snippets (and ones for about 50 other
languages), but it's easy enough to add more yourself, especially when the
desired behavior is as simple as you describe. It gets trickier when you have
lots of nested snippets, menus, conditionals, etc. But the tools are there as
features, and you don't have to write the features themselves yourself, just
use them.

As for latexmk, depending on how complex your needs are, you may not have to
configure it, just run it. A "latexmk -pdf foo.tex" will probably do what you
want. Configuring it or using other/more options give you more control, which
is a good thing. AucTeX, by the way, has a latexmk plugin.

[1] - It would have gone a lot faster had it not been a couple of years since
I wrote an xptemplate snippet.

~~~
JoshTriplett
> While on the subject of configuration, if you are not a fan of configuring
> your editor, I'm not sure I understand what you're doing using Emacs.

I don't mind configuring something when I actually want to change its
behavior, but I'm a big fan of sensible and helpful defaults that Just Work.
I'm not complaining that there isn't a vim mode that works _exactly_ like
auctex out-of-the-box; I'm annoyed that there doesn't seem to be one that has
equivalent functionality out-of-the-box. I could reinvent it using
templates/snippets, but I didn't have to do so with auctex.

> As for latexmk, depending on how complex your needs are, you may not have to
> configure it, just run it. A "latexmk -pdf foo.tex" will probably do what
> you want.

I understand that I wouldn't have to configure latexmk (or rubber, or any
number of other DWIM scripts for LaTeX), but I'd still need to configure vim
to use one of them and have a convenient keybinding for them. I'd also prefer
not to rely on an external script; I'd rather have something built into vim
and configurable within vim.

------
grayclhn
AUCTeX mode is great; less well known is CDLaTeX mode, which has a lot of
quick abbreviations for mathematical symbols, etc. I'm a huge fan:
[https://staff.fnwi.uva.nl/c.dominik/Tools/cdlatex/cdlatex.el](https://staff.fnwi.uva.nl/c.dominik/Tools/cdlatex/cdlatex.el)

------
hchenji
Try out TexStudio
([http://texstudio.sourceforge.net/](http://texstudio.sourceforge.net/)).
Free, open source, integrates with MikTex, ctrl-space autocompletion for
bibtex entries, can easily switch build chain from latex-dvi-ps-pdf to
xelatex-pdf or pdflatex or latex-dvipdf.

------
scorpion032
The best LaTeX editor I have found is
[Latexian]([http://tacosw.com/latexian/](http://tacosw.com/latexian/))

In a discussion on Latex Editors, I find it surprising how it was not
mentioned even, so far.

~~~
pmoriarty
Could you elaborate on that? What makes it the best for you? More to the
point, in what ways is it better than AucTEX?

------
kgabis
The best latex editor I used so far is TexPad
([https://www.texpadapp.com](https://www.texpadapp.com)), but it's only
available on OSX and iOS...

~~~
pmoriarty
Could you elaborate on that? What makes it the best for you? More to the
point, in what ways is it better than AucTEX?

~~~
kgabis
* It's a standalone and native OS X application

* It has code completion

* File and document navigation is easy thanks to tree view

* It works out of the box, no dependencies other than TeX required

------
diarg
Indeed, Emacs rocks for LaTeX.

It would rock harder is someone(tm) implemented a visual version of LaTeX, for
small quick documents and for newbie accessibility.

~~~
dredmorbius
There's Lyx, though I've long found it harder to deal with than straight-up
LaTeX.

If you're doing quick small documents, Markdown and Pandoc may well be a
better fix for you.

A useful mode for working with LaTeX (or other tagged documents) is to have a
build system which watches your source, re-builds it as you edit it, and a
viewer which updates as the generated PDF is refreshed.

You can do this with LaTeX, Markdown + Pandoc, or numerous other systems. It's
quite powerful.

------
kyrre
I feel like the secret to being productive with LaTeX is not the editor, but
the build script (latexmk).

~~~
dredmorbius
More details?

------
alceta
I'm tired of these superlatives like 'THE best editor', it adds nothing to the
quality of a blog entry.

