Write Paper With Spacemacs and Org Mode
1 Rationale
- Spacemacs makes Emacs easy to set up.
- Latex is not really readable.
- Learning org-mode once, and you do not need to worry about Markdown, HTML, Latex set up in lots of cases. You also get todo lists, calendars if you want.
2 Prerequisites
3 Preparations
3.1 Conventions
- I will use
C-x
for "Ctrl + x",C-X
for "Ctrl + Shift + x",M-x
for "Alt + x", etc. - I will use
SPC
for the space key. - Notice in VIM mode,
SPC SPC
is equivalent toM-x
. Both allow you to execute an Emacs command.
3.2 Install the required layers
If you already know Spacemacs setup, you can skip this section.
Layers in Spacemacs are a bunch of packages put together for a specific purpose.
We will be using latex
, bibtex
, and org
layers.
In Spacemacs, SPC f e d
to open the .spacemacs
file. Spacemacs key sequences
usually follow a logical order. In this case f
represents "files", e
represents "emacs", d
represents "dot-file". the "dot-file" is the Spacemacs
configuration file, normally located at ~/.spacemacs
.
In dot-file, search for the variable dotspacemacs-configuration-layers
,
ignoring the comments, it should look somewhat like the following:
dotspacemacs-configuration-layers
'()
This is where we add layers. We need to add latex
, bibtex
, and org
and the
variable now looks like the following. auto-completion
and shell
are also
absolutely recommended for obvious reasons. helm
should already be installed
when you install Spacemacs.
dotspacemacs-configuration-layers '( latex bibtex org helm auto-completion shell ;; other layers go here )
Save this file by SPC f s
Now SPC q r
to restart Spacemacs. Spacemacs will
install and configure these layers automatically.
3.3 Create your paper
Find your favorite place in the file system and create a new folder. The files
we will be editing has a .org
suffix. I will use paper.org
in the rest of
this post.
4 Hello Org-mode
Org-mode is a powerful plain-text system. You can learn more from its website http://orgmode.org. This post will focus on how to efficiently write papers with it.
4.1 Latex headers
In Latex, we usually start with the following lines
\title{Awesome Title} \author{ My Awesome Name } \date{\today} \documentclass[12pt]{article} \begin{document} \maketitle \end{document}
Then we copy these lines around all the time.
With org-mode, we do not need to deal with these nonsense anymore. Most of these lines will be generated by default. Instead we need the following
#+TITLE: Awesome Title #+AUTHOR: My Awesome Name
These lines are self-explanatory.
Most of the common Latex packages are included by default, such as amsmath
and
graphicx
. If you need additional package, for example geometry
to control
the margins, you can add the following line.
#+LATEX_HEADER: \usepackage[top=0.5in]{geometry}
Any options that normally appears at the top of the Latex file can be added this way, for example, we can remove indentation by adding
#+LATEX_HEADER: {\setlength{\parindent}{0cm}
4.2 Sections, subsections, items, enumerate, etc.
Remember typing \Section
and \item
all the time? It is simpler with org. I
highly recommend check out the org manual at http://orgmode.org/manual/. I will
cover some basic usage here. Here is a basic example
* This is a section ** This is a subsection 1. enumerate item 1 2. enumerate item 2 3. enumerate item 3 - itemize item 1 - itemize item 2 - itemize item 3 *bold*, /italic/, =code=
This piece of org text will be formatted into the following Latex code
\section*{This is a section} \subsection*{This is a subsection} \begin{enumerate} \item enumerate item 1 \item enumerate item 2 \item enumerate item 3 \begin{itemize} \item itemize item 1 \item itemize item 2 \item itemize item 3 \end{itemize} \end{enumerate} \textbf{bold}, \emph{italic}, \texttt{code}
Looks great?
4.3 Export to PDF
You can Export to PDF by the following keystrokes. First C-c C-e
will open a
export dispatch. You can follow the instructions in the window and press l p
to export to a pdf file, or press l o
to generate the file and open it.
4.4 Bibliography made easy
(Don't tell your rhetoric teacher)
Important! We need to tell Latex to use bibtex. Otherwise you will be running
into weird issues like I did. Add the following lines to the user-config
section of the .spacemacs
file. So that the user config looks like the
following.
(defun dotspacemacs/user-config () (setq org-latex-pdf-process '("latexmk -pdflatex='pdflatex -interaction nonstopmode' -pdf -bibtex -f %f")) ;; Other configurations )
Restart Spacemacas or simply reload the .spacemacs
file by SPC f e R
for
this change to take effect.
Now we need to create a .bib
file to hold the references. You can find out
more about bibtex here. Lets call the .bib
file ref.bib
. Now at the end of
paper.org
, add the following lines
bibliographystyle:ieeetr bibliography:ref.bib
The first line tells the bibtex to use IEEE style. You can also change it to other styles. The bibliography will not work until you provide a correct style. The second line is the place to search for references.
Now press C-c ]
to open helm-bibtex buffer. You can type in the title of any
paper, then C-n
to your preferred reference site (e.g. Arxiv) and press
Enter
. You will get a list of search results. Put the cursor on the paper you
want and press c
to copy or C
to copy and close the search window. Now go to
ref.bib
, paste the entry there and save.
Now go back to paper.org
, press C-c ]
again. This time you will see the
entry you just saved is displayed. Press enter to insert it at any place. Export
the file to pdf and all references are magically generated.
5 Code
Code for paper.org
#+TITLE: Demo Paper #+AUTHOR: Demo author #+OPTIONS: toc:nil num:nil #+LATEX_HEADER: \usepackage[top=0.5in]{geometry} #+LATEX_HEADER: {\setlength{\parindent}{0cm} * This is a section ** This is a subsection 1. enumerate item 1 2. enumerate item 2 3. enumerate item 3 - itemize item 1 - itemize item 2 - itemize item 3 *bold*, /italic/, =code= ** This is a reference cite:Nobody bibliographystyle:ieeetr bibliography:ref.bib
Code for ref.bib
@misc{ Nobody, author = "Nobody", title = "Article", year = "2017" }
Here is an image of the generated pdf document.
