10/16/2012

latex-beamer + emacs-org-mode

The what? 
"You set the learning curve depending on your needs" -- Bastien Guerry, org-mode maintainer since 2011
     In this post, I am going to describe how to use org-mode in the awesome emacs to write reports/papers and create presentations as swiftly as writing plain text. Naaah, you will be writing plain text only! Rest of the shebang -- generating the tex file, producing the pdf, and even opening it for preview! --  is taken care by a shortcut in Emacs: C-c C-e d. Peace in life. Especially useful when you start writing the report at 2345 and the submission deadline is 2359 :D.
    We will be using the following tools/technologies: Beamer (it's just a Latex class), and Emacs* (org-mode in specific). I have written this post in haste so a few things may have slipped my mind. Put off in comments if you have any problems and/or tips.

The why?

    Most of us have suffered the horrendous pain, not to mention utter frustration, of "using" MS Office (or LibreOffice) while creating reports, ppts, and more reports. Aligning the pics, formatting the text, the font, the size, the bullets -- you add some more text, and everything needs to be redone!
    Some of us are clever, only slightly, so they use Latex to create pdfs, etc. I won't blabber on the advantages of Latex over WYSIWYG document creators. But even those who have used Latex have experienced inserting the irritatingly large number of commands, braces, etc. manually!
    But why, I ask? Latex is a structured language. What's the point of it having a machine-readable structure if tex files are to be generated manually?? On the other hand, having such a complicated structure, with all the commands and stuff, makes the users even more averse to use this wonderful brainchild of Knuth. I have heard so many non-CS (and even CS) students, who have heard of Latex, saying, "But Latex is so complicated. I better use MS Word; it takes less time." Hence. This. Post.

Starters:

    The following packages need to be installed (via Synaptic Package Manager or its counterpart in your system):
  1. texlive-latex-base
  2. texlive-latex-recommended
  3. texlive-latex-extra
  4. latex-beamer
    Caution: I guess one of the dependencies of texlive-latex-recommended is texlive-latex-doc which is huge in size. You better have a fast internet connection or just don't install it.

    If you don't have admin privileges, do not fear. Just install the latest org-mode and it should have in-built support for beamer.
Main course:

    Open a file in org-mode in Emacs (this can be done by opening a file with .org extension) and start editing it. Here are a few tips:
  1. You can use the rich text-editing facilities provided by org-mode. For details on these facilities, go here
    • Especially, check out the way you can create headings and sub-headings. For ex. if you start the sentence by *, that sentence becomes one of the main headings in the exported pdf. Thus, prefixing a sentence by ** (2 asterisks) makes it a sub-heading. And so on.
    • For creating a new heading and sub-heading, you can use shortcuts like M-<return> and  C-<return>. There is slight difference in their functionalities. Try 'em out!
    • For indenting a level deeper, you can use <Tab> just after M-<return> or C-<return>. Or you can use M-<right-arrow> and M-<left-arrow> anytime from anywhere in the heading/sub-heading to indent it.
    • You can fold-in and fold-out the headings/sub-headings by repeatedly pressing <Tab> while on a particular heading/sub-heading. Use Shift-<Tab> repeatedly use fold-in and fold-out the entire document.
  2. Whatever text you put in it will show up as it is in the pdf that you export.
  3. You can use almost all the Latex commands, since org will anyway export your file into tex format before creating a pdf.
  4. By pressing C-e C-t, you can enter create a header (which means it should come at the top) for your document which has several options (It is actually a template for exporting the document to Latex). I use one of the options for disabling the Table of Contents -- "toc:nil" :-)
  5. You can add the following lines to the header to make the report more compact and to change the font size:
    #+LATEX_HEADER: \usepackage{fullpage}
    #+LaTeX_CLASS_OPTIONS: [12pt]
    
  6. Adding images is very easy! Just insert the following piece of code where you want to include the image:
    [[file:/img/org-mode-unicorn.png]]  
    Adding the file attribute is optional as long as you are adding well-known formats such as jpg, jpeg, png, and gif. More details on formatting/scaling/adding-attributes-to the image here.
    Now, for creating a presentation, we just need to tell Emacs that while exporting the document, use a certain latex class: Beamer. Just execute the following somewhere near the header (or copy-paste the code given below):

M-x org-insert-beamer-options-template <ret> g

    This will insert some code which is similar to the following except for the 4th line. I have just used Copenhagen theme instead of the default theme. More on different themes in a later post. :-)

#+LaTeX_CLASS: beamer
#+LaTeX_CLASS_OPTIONS: [presentation]
#+BEAMER_FRAME_LEVEL: 1
#+BEAMER_HEADER_EXTRA: \usetheme{Copenhagen}
#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra)
#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC


Desserts!

    Some beamer-related key-bindings I use routinely:
  1. C-c C-e -- Opens a menu containing a plethora of options of which the ones I use frequently are listed below
  2. C-c C-e t -- Inserts the export class template
  3. C-c C-e d -- Exports the current buffer as pdf and opens the pdf for preview. If you are using Ubuntu or other Debian-based system, this option should work just fine. Otherwise you will have to tell Emacs to use your system's pdf viewer. These links might help: [link1][link2][link3] Tell me in comments if you have any problems/better solutions
  4. C-c C-e p -- Exports the current buffer as pdf but doesn't open the pdf
  5. C-c C-e b -- Exports the current buffer as HTML (very beautifully!) and opens in default browser
    Here is an example each of report [org] and ppt [org]. Most of this stuff was taught to me by Pradeep (thanks!) long back. But as and when I forget bits and pieces, I refer to this tutorial. Apart from that, of course, there is extensive documentation on how to use org-mode and Emacs itself. By the way, I haven't even touched on org-mode. It is HUGE!
* I am sure there exists some Vim plugin to do the same. Some Vimmer somewhere might help you. :-)

No comments:

Post a Comment