2009-10-21

editing files owned by root with tramp

One of the times when many emacs-users still reach for vi is when editing system configuration files – the stuff in /etc/, etc. I think that is because of the now mostly false impression that emacs startup is slow, and because people don't want to run an open-ended program as emacs as root.

There is truth in that last point – but it applies just as well to, say, vim. Anyway, running emacs as root is not a good idea.

So how what can we do it? Easy! Using the tramp package (included with GNU/Emacs since version 22), you can run emacs as a normal user, but edit root-and-other-owned files. It does its magic using sudo, but you won't normally notice. It does require you to have sudo-rights of course.

How does this work? Well, instead of

C-x C-f /etc/hosts

to open a file as a normal user, you use:

C-x C-f /sudo:root@localhost:/etc/hosts

or even shorter, as noted by Alexander Kojevnikov (because Tramp defaults to root@localhost):

C-x C-f /sudo::/etc/hosts

It asks for a password - and you should use your user password for that (not the root password!). This usually works fine, but due to way Tramp works, it can get confused if root has some very weird command prompt. If so, you of course configure tramp. Also note that sudo usually remembers that you logged in, an does not require you to re-enter you password when opening ('visiting') another file for some time period – but you can change this. See the sudo(8).

This automatically invokes tramp which does all the magic for you. If you don't like the somewhat longer (pseudo)paths for files, you can of course use the emacs bookmarks facilty. After you load the file, you can use it like any other file.

Note, this is only of the many useful things you can do with Tramp. Tramp was actually written for editing files on remote machines (using ssh or other protocol), and I very happily used it to edit files on some European machine whilst in Australia. It caches the file locally, and only sends it over when you save it, so it very fast – it simply makes you forget your file is so far away.

2009-10-18

writing presentations with org-mode and beamer

[Updated:fixed an error in the template] Things have been a bit quiet at Emacs-Fu lately; this is mostly because I have been very busy with work, traveling, traveling for work etc. Emacs-wise, this has been a very intensive time, and I have been able to move more and more task to emacs – both professionally and privately. This is mostly because thing become easier when I can do all my things in one place.

Anyhow, one of the tricks I picked up recently is to write presentations with the combination of org-mode and a LaTeX-package called beamer. The most common tool for doing presentations is Microsoft's Powerpoint program. It gets a lot of criticism, most famously from prof. Tufte in his Powerpoint is Evil essay. Of course, the problem is in misuse of the tool, not so much the tool itself.

Still, I didn't want to use Powerpoint (or Powerpoint-wannabees, like OpenOffice's Impress). For the technically-inclined, the knee-jerk reaction to this kind of problem is to shout 'LaTeX!'. Indeed - the LaTeX text-processing system offers a package called Beamer, which allows you to write presentations with LaTeX. It is quite powerful, and even allows for all kinds of fancy graphical bling (fade-in, fade out etc.); even better, it generates PDF-files, which can be viewed just about anywhere. The various themes and color settings it offers are quite nice, even though they tend to fill only a small corner of the design-universe…

beamer and org-mode

Now, while I am no stranger to LaTeX, especially for writing a quick presentation, it can be a painful to remember the various directives and options. I am not really a daily LaTeX-user, so I tend to forget these things. I am a daily org-mode user though, and org-mode can export to LaTeX (which then, in turn, are translated into PDFs). So why not use org-mode to generate presentations?

It turns out that that is quite easy.

First, we need to define some of the LaTeX-boilerplate, and tell org-mode about it, so we never need to think about it again. Put the following in your .emacs:

;; allow for export=>beamer by placing

;; #+LaTeX_CLASS: beamer in org files
(unless (boundp 'org-export-latex-classes)
  (setq org-export-latex-classes nil))
(add-to-list 'org-export-latex-classes
  ;; beamer class, for presentations
  '("beamer"
     "\\documentclass[11pt]{beamer}\n
      \\mode<{{{beamermode}}}>\n
      \\usetheme{{{{beamertheme}}}}\n
      \\usecolortheme{{{{beamercolortheme}}}}\n
      \\beamertemplateballitem\n
      \\setbeameroption{show notes}
      \\usepackage[utf8]{inputenc}\n
      \\usepackage[T1]{fontenc}\n
      \\usepackage{hyperref}\n
      \\usepackage{color}
      \\usepackage{listings}
      \\lstset{numbers=none,language=[ISO]C++,tabsize=4,
  frame=single,
  basicstyle=\\small,
  showspaces=false,showstringspaces=false,
  showtabs=false,
  keywordstyle=\\color{blue}\\bfseries,
  commentstyle=\\color{red},
  }\n
      \\usepackage{verbatim}\n
      \\institute{{{{beamerinstitute}}}}\n          
       \\subject{{{{beamersubject}}}}\n"

     ("\\section{%s}" . "\\section*{%s}")
     
     ("\\begin{frame}[fragile]\\frametitle{%s}"
       "\\end{frame}"
       "\\begin{frame}[fragile]\\frametitle{%s}"
       "\\end{frame}")))

  ;; letter class, for formal letters

  (add-to-list 'org-export-latex-classes

  '("letter"
     "\\documentclass[11pt]{letter}\n
      \\usepackage[utf8]{inputenc}\n
      \\usepackage[T1]{fontenc}\n
      \\usepackage{color}"
     
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
     ("\\paragraph{%s}" . "\\paragraph*{%s}")
     ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

This is based on the template by Thomas S. Dye on the org-mode mailing list. You can of course add other packages to it with \usepackage. In my version, I have add the Listings-package for including syntax-highlighted snippets of source code in my presentations. Importantly, I added the [fragile] options to the frame-settings, otherwise you cannot include such source code fragments without LaTeX complaining in various unhelpful ways.

Note, you can customize the way the Listings package works by changing the template above; or by setting the options in the org-file; this involves some 'raw LaTeX' though. It might make sense to define some macros for that.

Now, we can easily make a presentation in org-mode; simply start the file with something like:

#+LaTeX_CLASS: beamer
#+MACRO: BEAMERMODE presentation
#+MACRO: BEAMERTHEME Antibes
#+MACRO: BEAMERCOLORTHEME lily
#+MACRO: BEAMERSUBJECT RMRF
#+MACRO: BEAMERINSTITUTE Miskatonic University, Astrology Dept.
#+TITLE: Presentation with Org-Mode and Beamer
#+AUTHOR: Someone

Of course, you can change these parameters; for example, you might want to change the BEAMERTHEME into Madrid or Warsaw, or … – see the Beamer User Guide (PDF).

After having set up these things, you can write presentations in the familiar org-mode mark-up.

including source code

As mentioned before, you can use the listings-package to include source code snippets in your presentation. You'd write this something like:

#+BEGIN_LaTeX
\begin{lstlisting}[language=c]
for (int i = 1; i != 10; ++i) 
    std::cout << i << ": hello, world!"
              << std::endl;
\end{lstlisting}

#+END_LaTeX

In other words, we include some 'raw' LaTeX to do this. Now, the org-mode-way of doing this, would be to use something like

#+BEGIN_SRC c
  /* code */
#+END_SRC

as discussed before. This works well when exporting to HTML, but at this moment this will simply translate into a verbatim environment in LaTeX - so we use lstlisting to get some syntax-highlighting.

including pictures

Of course, the full arsenal of org-mode tools is available as well, for example Ditaa, as discussed before. Ditaa is now shipped as part of org-mode, and you can use it to create picture which are then included in your presentation. Very nice.

For including existing images (PNGs, JPGs etc.), it's probably easiest to put use some raw LaTeX for that, e.g., something like

#+LaTeX:\includegraphics{/some/path/emacs.png}

putting it together

Now, let's put it all together. Below is an example presentation. Assuming you have everything installed (ie., LaTeX with the listings package, a fairly recent org-mode, ditaa), you create your presentation.org and then press C-c C-e d and your presentation (presentation.pdf) is generated and automatically shown in your PDF-viewer. Easy!

The intermediate files (such as presentation.tex) are there as well, so you can check them if something went wrong.

I have uploaded the resulting PDF to Slideshare, so you can see what it looks like. However, the Slideshare-converted version is extremely blurry, unlike the crisp PDF I actually created. I'd be happy to upload the file somewhere else if someone can point me to a good place, thanks!

So hopefully this shows that you can quite easily make presentations with org-mode, with some help from Beamer, LaTeX etc. Beamer actually provides a lot more, such as funky slide-transitions and other tricks – but the things here should give you a good starting point.

#+LaTeX_CLASS: beamer
#+MACRO: BEAMERMODE presentation
#+MACRO: BEAMERTHEME Antibes

#+MACRO: BEAMERCOLORTHEME lily
#+MACRO: BEAMERSUBJECT RMRF
#+MACRO: BEAMERINSTITUTE Miskatonic University, Astrology Dept.
#+TITLE: Presentation with Org-Mode and Beamer
#+AUTHOR: Someone

* My presentation

** Overview

   1. Introduction

   2. Main part

   3. Questions
    
    
** Some interesting stuff
    
*** an important point
    
    - subpoint a
      
    - subpoint b

** Graphics

*** a picture

#+begin_ditaa blue.png -r -S
+---------+
| cBLU    |
|    +----+
|    |cPNK|
+----+----+
#+end_ditaa

*** another picture
#+LaTeX:\includegraphics{emacs.png}

** More interesting stuff

*** some C++ code
#+begin_LaTeX
\begin{lstlisting}[language=c]
for (int i = 1; i != 10; ++i) 
    std::cout << i << ": hello, world!"
              << std::endl;
\end{lstlisting}
#+end_LaTeX

*** and some Python...

#+begin_LaTeX
\begin{lstlisting}[language=python]
for i in range(1,10):
        print i, "hello, world!"
\end{lstlisting}
#+end_LaTeX