Presentation, Article and Notes in one run using Beamer

Still using Powerpoint for your presentation? Consider using LaTeX with the Beamer package.
 
Tom Rutherford taught me how to build an efficient stream for a presentation or lecture:
  1. generate the presentation with beamer,
  2. generate the presentation as an article and
  3. clean up all the stuff
All this in one batch file. The presentation is projected on the big screen and the article is used for preparation and for notes, I use during my prepartation (I have a printout of the article next to my laptop during the presentation)
 
The batch (build.bat) file looks like this:
 
@echo off
pdflatex beamer
pdflatex beamer
pdflatex print
pdflatex print
del *.toc
del *.log
del *.out
del *.nav
del *.snm
del *.vrb
del *.synctex
del *.aux
cd auto
del *.*
cd..
rd auto /Q
 
and after finishing my slides, I just run it from my shell or my explorer.
 
The latex commands are run twice, so if there are citations, bibtex is run. I give a serie of lectures and for every lecture I have three files:
  1. lecture.tex:    all the slides to be presented
  2. beamer.tex:   the preamble for generating the lecture as beamer presentation
  3. print.tex:        the preamlbe for generating the lecture as article.
 
 
Beamer tex looks like this:
It gets the lecture.tex file as input and grabs the style.tex file for “styling” my presentation.
 
I added the following features.
 
Setting a directory for all my figures and for my style file:
I use import, so I can set a directory for all my figures (instead of having them in the same direcotry as my tex files). The directory is set in the lecture.tex file:
 
      \graphicspath{./figures/}


The style file resides in its own directory, so I do not have to replicate it and changes are easy done in just one file.
 
Comments not showing in my presentation, but in my article:
I use the package comment to add notes to my presentation. Beamer allows you to make notes, that will be shown as a parallel slide, so you can have your presentation projected and the other half on your notebook monitor (split screen). This seems to work fine on a Mac or under Linux, but is almost impossible on a Windows machine (there is ony very basis and not very satisfactorysolution called presenter-pdf). I just use the comment package that allows you to add notes that are printed in my article version of my presentation but do not show up in my beamer presentation. In beamer.tex I give the commend to not show the comments:
 
 
In print.tex I add the lines to include the comments:
 
 
A comment in the lecture.tex follows an \end{frame} command and could look like this:
 
 
In the article version, the comments are red and start with a bold “NOTES” above them, so I can concentrate on them, when I am presenting.
 
Size of graphics
I use the width option for included graphics, so figures also fit if the format of my paper in beamer (landscape) is changed to portrait (article). Example:  [width=0.8\textwidth].
 
Here you see the presentation:
 
 
And here the article format (in the right margin, you see the number of the lecture as well as the slide number).
 

Auto-Completion in Emacs, LaTeX and Gams

The fun with Emacs is that you can discover new (or old) stuff that makes your life much easier on an almost weekly base. I was reading the manual on Emacs Speaks Statistics and discovered that there is a mode called auto-completion. I googled it, installed it and after some experimenting, I got it running. You can find it here. It comes with autocompletion for some programming languages (Python, Ruby, C+, etc.) but not for LaTeX, so I googled further and discovered an extension called auto-complete-latex (you can download it here.
Here are some screenshots: You just start typing and it suggests a keyword or if you wait a little bit longer it gives you a dropdown llist. You can jump to the next suggestions using the tab-key.
It doesn’t matter if you use capitals or not:
and it also keeps track of words in the actual document. Here an example of my paper on parking. As you see the word “Parking appears, when I typ “par”.
For Gams there is no dictionary, so I wrote one myself and added it to the dictionaries. I only did not manage to start auto-complete for Gams automatically. But this can be done with M-x auto-complete-mode.
Update:
;; ac-modes – major modes ac can run on
(add-to-list ‘ac-modes ‘gams-mode)

;; toggle auto-complete in all buffers

(global-auto-complete-mode t)

(with thanks to a reader)
These extensions make Emacs now to a modern text editor.

Bibliography with abstracts in LaTeX

If you ever have written a scientific paper, you probably know how hard it is to remember what was in the papers you cited. Usually you cite them once and forget about them, but if you want to discuss your paper with other scientist, it would be nice to have at least an idea of what you exactly cited.
In LaTeX you have a nice way of producing a bibliography with abstracts. It is quite easy. You only have to use biblatex, an improved bibtex. Information on biblatex can be found at the CTAN repository (just google it). There are also two good articles in German from http://biblatex.dominik-wassenhoven.de/dtk.shtml.
 
Here is an example. Your LaTeX file looks like this, where the file jabref is my bib-file with all my citations (for which I use jabref):
 
 
The results looks like:
 
 
If you have some annotations, they also will be reproduced.

Using Gams, R and LaTeX I

Wow, I just discovered how easy it is to produce nice LaTeX tables from your Gams results. Usually I capture my model results in some parameters that are exported (using gdx or gdxxrw) to excel. Then I make my tables in Excel and use an excel add-in to export the table to LaTeX (I have written about this add-in in this blog). This works fine, but is tedious because as soon as my results are in excel, I can’t do things automatically and have to click my way to get my LaTeX tables.
Today, I was working on the parking model and wanted to automate my LaTeX tables. I already wrote on how to produce nice plots using R Statistics, but that might have been too geeky for a lot of you (“I don’t do network modelling and Excel or Gnuplot produces plots that will do…”). However, what I show you today is worth trying to start working with R if you are doing your modeling with Gams.
My workflow now looks like this:
  1. Run my model and produce some parameter with results and export the parameters to a gdx file
  2. Read the parameters from the gdx file with R using the R package (gdxrrw).
  3. Recast the data using “cast” and “mold” from the package reshape (see for an excellent introduction (http://www.jstatsoft.org/v21/i12/paper)
  4. Use the R package “xtable” (see http://cran.r-project.org/web/packages/xtable/xtable.pdf) to produce for each table a separate tex file.
  5. Write my paper and include the tex files with tables.
Let us have a look at the code for every step.
1. Run my model and produce some parameter with results  and export the parameters to a gdx file
Most of this is straight forward. Note that I save my gdx files in my R directory. In this directory I have my R-scripts.
2. Read the parameters from the gdx file with R using the R package (gdxrrw).
Gams Corporation is working on a R-package that allows you to transfer data from R to Gams and the other way around. The package is called gdxrrw (the “rrw” stands for R read and write)  is still work-in-progress, but you can already use it (just download it from (http://support.gams.com/doku.php?id=gdxrrw:interfacing_gams_and_r).
When you have it installed, you can use it as follows:
The gams parameter is now a R data frame with the following structure:
3. Recast the data using “cast” and “mold” from the package reshape
I want to produce two different tables: one for the model statistisc and one for the road results. I saved all this results in one parameter, so now I have to take a subset of the results for every table.
I defined a subset “modelstatistics” and use them to subset my dataframe using %in%. This gives us the following dataframe
I want to have a table with the parameter as columns and the scenarios as rows. For this I first rename the columns and then I use the cast command:
If I combine this with the xtable command, I already have my LaTeX table:
4. Use the R package “xtable” to produce for each table a separate tex file.
Now we are almost ready: I set some options for saving the LaTeX table (table title, label and filename) and save the file using the print command on the table:
5. Write my paper and include the tex files with tables.
I jump in my paper tex file and add the following line for including the table:
and produce my LaTeX file:
In your xtable-code in the R-script you can a lot of additonal options to format your table.
The great thing is that I can do it all now without having to adjust my tables every time I run a different or addtional scenario. I just add in gams the following line to run the R-script:
And for the Emacs fans: I didn’t have to leave Emacs at all: I run my Gams model, my R code and my LaTeX from within Emacs (and for those with the good eyes: I can even clock this task…)
In a next post, I will write more on the use of cast: you can use this R command for lots of interesting stuff (building tables with the sum, mean, min, max, etc.).

Biblatex and folder contents

I was looking for a way to make nice index for my folders using LaTeX. I usually have lots of papers saved in a folder and I was looking for a way to use my paper references stored in JabRef (an open source bibliography reference manager which integrates neatly with AucTexi in Emacs, see http://jabref.sourceforge.net/). The problem is that I want to have the title of the author, year and the title of the paper (who remembers something like Arrow 1955?), so I looked for a way to print the paper titles. BibTex and all the citation packages don’t seem to manage, but then I stumbled on bibllatex (see http://ctan.org/pkg/biblatex).
On the homepage you find the following description of biblatex:
“Biblatex is a complete reimplementation of the bibliographic facilities provided by LaTeX in conjunction with BibTeX. It redesigns the way in which LaTeX interacts with BibTeX at a fairly fundamental level. With biblatex, BibTeX is only used (if it is used at all) to sort the bibliography and to generate labels. Formatting of the bibliography is entirely controlled by TeX macros (the BibTeX-based mechanism embeds some parts of formatting in the BibTeX style file. Good working knowledge in LaTeX should be sufficient to design new bibliography and citation styles; nothing related to BibTeX’s language is needed.”
 
Biblatex allows you to cite the title using \citetitle{bibtexkey}. Using this and, after some surfing, a little bit more LaTeX code, I managed to use LaTeX to produce something like:
 
Looks nice!

 

Compiling LaTeX files to pdf: a second try

In a previous topic, I showed how to use a macro to kill your pdf-reader when you compile LaTeX files. Most pdf-readerss do not allow to refresh a pdf-file after compilation, so you have to close the file in the pdf-reader before compiling. My solution was to kill the pdf-reader completely. A reader of my blog wrote that you can use Sumatra, a free pdf-reader, which allows to refresh your pdf-file.
I decided to give it a try, but I wanted to keep my normal pdf-reader for all other pdf-files, because it has lots of options like commenting etc. I regularly use.
Another thing that bothered me with my pdf-reader is, that I could not jump back to Emacs (inverse-searching). Sumatra has this option.
Here are my settings in Emacs:
Sumatra will not reopen if you add the option -reuse-instance:
 Do not forget to set the inverse search command in the options of Sumatra to:
     c:\program files\emacs\bin\emacsclientw.exe +%l “%f”

Template Mode for Emacs

If you write a lot of R-, Stata-, Gams and/or LaTeX-files templates can make your life more relaxed. Emacs doesn’t have a template system (well there is auto-insert, but I did not find enough information on how to use if for templates). I used to have some templates in a directory and usually I would start with them (and forget that I should save it under another name…). The disadvantage is that you have to jump around in the file to fill out the information you want to have at the top of the file (like project name, topic, file name etc.). Today I discovered templatemode (see http://emacs-template.sourceforge.net/details.html). It works like a breeze and makes my writing life much easier.
 
I have templates for the above mentioned file types. The Gams template looks like this:
 
Now if I start a new file, TemplateMode asks me if I want to use the Gams-template, it inserts all the information between >>> and <<< or asks for information to insert. For example DIR, DATE will grab the directory and the date. At the end of the template file you see pairs of words. These words define the interactive part. Templatemode asks me for the project name with Project:, the job with Job: and the Gams title with Title:.
 
The line with the Version Control Information grabs information from my verison control system as soon as I commit the file (and is not part of templatemode).
 
This is how a new Gams file would look like:
 
 
Installation is easy and you can find lots of examples in the directory.
 
 

Killing an open instance of pdf viewer when compiling latex

If there is one thing that is annoying when you us pdflatex in Emacs, it is the behavior of Adobe or another commercial product. If you haven’t closed the pdf-file, Emacs or better LaTeX will complain that the pdf-file is still open. I found some tricks in the internet, but I did not manage to implement them properly (you can for example use sumatrapdf, but I wanted to keep my original pdfreader on my computer).
Finally, I decided to use my macro/script program winautomation (www.winautomation.com). I bought this program a few months ago after getting tired of the open source program Autohotkey (which is quite good, but you have to program a lot). Winautomation is just point and click. I wrote a lot of macros that make my life much simpler. For example, I have some scripts that start my subversion backup on my servers, some scripts that open the explorer in a certain directory, a script for logging in at the university, etc.
 
 
For the problem above, I did two things: I wrote a script that checks if the pdf-viewer is open, and closes it and integrated it in an Emacs macro.
Here is the script in winautomation:
 
 
In Emacs I recorded a macro with C-x ( and C-x ) in which I run a shell command:
 
      Alt-x shell-command Enter
      d:\inbox\closepdf.exe             (this is the script I wrote with winautomation)
      C-c C-c                                 (starting latexing)
       …
 
After some trial-and-error, I now have a Emacs-keyboard macro that exactly does what I want: it closes the pdfviewer program using the winautomation-script and compiles my tex file to pdf.
I changed my settings for latex to always produce a pdf-file by putting the following line in my .emacs
 
      (setq TeX-PDF-mode t)
 
so now I don’t have to bother about the proper file-format for figures (no converting to ps, etc.).
 
The last thing I had to do was assign a key to my macro in Emacs. This is done by first naming the keyboard macro I recorded using “Alt-x name-last-kbd-macro” and after that save the macro in my .emacs file with Alt-x insert-kbd-macro and attach a key to it:
 
Here is the code snippet for the .emacs file
 
 
Of course, all this is also possible with autohothey or AutoIt,

Update of Gams-Tex

Edson Valle sent me an update for Gams-Tex (see my blog from 28. Januari) where he showed how to add new keywords to the Gams-Tex file (here done for the MPSGE keywords):
 
Just add the following lines to the gams theme file (theme_gams.tex) after the moreKeywords={
$PROD, $DEMAND, $AUXILIARY, $COMMODITIES, $SECTORS, $CONSUMERS, $MODEL, $REPORT,
It’ll become automatically case insensitive due to the options selected.

Gams code in LaTeX documents

Edson Valle recently wrote a style for the package listings from LaTeX. It allows you to format Gams code nicely in your LaTeX documents.
Here is a small example taken from his post to the Gams mailing list:
Scalar X /1/;
Scalar Y;
Y = 2*X;
Display “This is the famous Y”, Y;
In your LaTeX document this would look like:
The only thing you have to do is write a file gams.tex with the style and include this file in your LaTeX document before \begin{document}.
Information on the package Listings can be found here: listings
The gams.tex file should look like this:
\usepackage{listings}
%author Edson Cordeiro do Valle edsoncv at gmail dot com

\lstdefinelanguage{GAMS}{
morekeywords={
ABORT , ACRONYM , ACRONYMS , ALIAS , ALL , AND , ASSIGN , BINARY , CARD , DISPLAY , EPS , EQ , EQUATION , EQUATIONS , GE , GT , INF , INTEGER , LE , LOOP , LT , MAXIMIZING , MINIMIZING , MODEL , MODELS , NA , NE , NEGATIVE , NOT , OPTION , OPTIONS , OR , ORD , PARAMETER , PARAMETERS , POSITIVE , PROD , SCALAR , SCALARS , SET , SETS , SMAX , SMIN , SOS1 , SOS2 , SUM , SYSTEM , TABLE , USING , VARIABLE , VARIABLES , XOR , YES , REPEAT , UNTIL , WHILE , IF , THEN , ELSE , SEMICONT , SEMIINT , FILE , FILES , PUT , PUTPAGE , PUTTL , PUTCLOSE , FREE , NO , SOLVE , FOR , ELSEIF , ABS , ARCTAN , CEIL , COS , ERROR , EXP , FLOOR , LOG , LOG10 , MAP , MAPVAL , MAX , MIN , MOD , NORMAL , POWER , ROUND , SIGN , SIN , SQR , SQRT , TRUNC , UNIFORM , LO , UP , FX , SCALE , PRIOR , PC , PS , PW , TM , BM , CASE , DATE , IFILE , OFILE , PAGE , RDATE , RFILE , RTIME , SFILE , TIME , TITLE , TS , TL , TE , TF , LJ , NJ , SJ , TJ , LW , NW , SW , TW , ND , NR , NZ , CC , HDCC , TLCC , LL , HDLL , TLLL , LP , WS , /,PROD: },
sensitive = false,
morecomment=[f]*,%
morecomment=[s]{$ontext}{$offtext},
morecomment=[s][\color{green}]{/}{/},
morestring=[b]”,
morestring=[b]’
}
\lstset{
basicstyle=\fontfamily{pcr}\fontseries{m}\selectfont\footnotesize,
commentstyle=\color{gray}\itshape,
keywordstyle=\color{blue}\bfseries,
stringstyle=\color[rgb]{0.5,0,0.5}\itshape,
showstringspaces=false,
numbers=left,
numberstyle=\color[rgb]{0,0.5,0.5}\fontfamily{pcr}\fontseries{m}\selectfont\tiny,
numberblanklines=false,
showlines=false,
belowskip=\bigskipamount{},
breaklines=true,
%stepnumber=2,
tabsize=6,
%extendedchars=true,
%float=h,
frame=tb
}