Drawing nice side-by-side trees in LaTeX

In my work I often have to describe the model I use. For this it is almost inevitable to draw some hierarchical production functions. The following shows you how to draw two production functions side by side using Tik/. TikZ is a German acronym and stands for “TikZ ist kein Zeichenprogramm” (TikZ is not a drawing program) and produces high-quality drawings in LaTeX.

This is how the two production function look in the paper:

2017-12-06 12_04_12-Report top-down modeling.pdf - SumatraPDF

I use the Latex package subcaption (as the package subfigure is deprecated) to get the two production functions side by side. You further need to load Tikz and Tikz-qtree.

Some remarks:

  • The following line starts the left panel of the figure. I set the width to 0.45 of the text width, so both pictures can fit easily. Furthermore, I use the option “t”, so that both figures align at the bottom.
  • The following code starts the TikZ picture with some style settings. The size of the picture is slightly reduced
  • The direction of growth for the production function is down. I change this direction to “up” to add the top of the picture.
  • Each nest starts with a top node by adding a dot right after the square parenthesis, like: [.{your text}. Every child is added by some text between two curly brackets like

Tables with footnotes in LaTeX

Once in a while, I need a table in LaTeX with some footnotes below the table. Doing an internet search I found a nice solution using the package threeparttable. Here is a small example with the nuclear power plants in Switzerland:

image

The LaTeX code for this table is self-explaining (note the special structure: table – threeparttable – tabular):

Using Powershell to clean up the animations your LaTeX file

In a previous post, I talked how to animate your Beamer presentations and how to print them as slides and article for the audience. One problem with a presentation with animations is that every element of an animation means one more slide. If you want to give the presentation to the audience so they can write on their slides using their ipad or touch screen notebook, they will complain that only one of the animated slides is complete.

The easiest way to remedy this, replace all overlays, \pause, etc. from your LaTeX file and print it like this. One could do this easily in Emacs and write a macro for this. This time I chose another way to do this using Powershell, the awesome windows scripting language.

First, I define a function that will show me a file picker. Then I will use this file picker to get the LaTeX-file that should be cleaned up. From the file chosen, I save the path in a variable $FolderPath and get the name of the file. This information is used to save the file as the original name with “_NA” (for no animation).

Then I read the content and replace all occurences of \pause, <+-.> <digit-> and \only by a space using regular expressions. After this I save the file in the original place using the _NA-addition. The only thing I had problems with was the encoding (Powershell saved it in the wrong format), so I had to add “–Encoding Default” as option.

That’s all. Of course, this could be done with any scripting language.

Tricks with Beamer: Animations and Print-Outs

If you use Beamer for presentations, you might want to use “animations”: either show list elements successively or build up a diagram. Beamer has some nice possibilities for doing this:

  • In lists (enumerate, itemize) you can use the overlays. Just add <1-> after the first \item and this item will show up first. For the second item you just add <2->, etc. You can also let them disappear again by giving a range (e.g. <2-3>, which means that this item will appear at the second and third slide, but will disappear again after that. If you want all the list elements to appear one after another and you don’t want do write down the elements one by one, you can use [<+->] just after your \begin{itemize}.
  • For other parts you can use \pause.
  • For diagrams made with tikz, you can enclose the parts of the diagram using \only<1->{ draw commands } with the same syntax as the overlays mentioned above. 

  If I give a lecture, I use a trick by Tom Rutherford, to print out my presentation in slides- and article form. My lecture is always in the file lecture.tex. I than have two additional tex files: beamer.tex and print.tex for producing pdf-files in slide- and article format. This way, students can either use the slides and write on them using their ipad, or use the article and print it out for in class.

The files look like this for the slides:

and for the article:

And here are the first pages:

beamer

article

Note taking with LaTeX for learning

Summarizing a text book is easy in LaTeX, but sometimes I want to learn the summarized text. Reading the summary over and over again is not very efficient, so I came up with the typical solution, where you can hide the main part of the summary and can check your knowledge by asking yourself questions based on keywords in the margin. Here is an example, where I started summarizing the R package data.table:

image

Now I use hide the main text and can ask myself the questions:

  • Express the data.table in SQL-form?
  • How to create a data.table?

Add the following to your latex file (note that I load also the packages lstlisting and some other packages not mentioned here):

Some remarks:

  • I changed the vertical distance of the paragraphs, so it automatically skips a line (if you summarize with lots of text this is probably not a good thing, but if you summarize R commands like in the example, you don’t want to have to add linebreaks after every paragraph.
  • I removed the indent for new paragraphs for the same reason.
  • I added the package color so I can show the margin notes in the color red (using the renewcommand for the marginfont.

In the text you can now add a margin note by adding:

Using file templates in Emacs

A good practice in modeling is to place information related to the project your are working on, the subject, the data and your personal information like E-mail address at the top of your file. In Emacs you can define skeleton functions that will prompt for the information and put it in the file. Below is an example for a file with as comment symbol “**: (the $Id: $ is for my version control system, that automatically will add the information on the last commit in this line).

image

Below you see how to make a function using skeleton. As I don’t want to repeat myself, the function asks as an input the comment symbol(s) that should be used. I now can use the function for different programming languages (R uses “#” , C++ uses two forward slashes, GAMS “*”, etc.). You can add the time and date by using the command “current-time-string”. All other inputs that have skeleton-read in front of it, are also prompted for. If you need the template for your whole project team, you could just make the author and contact field also input-dependent.

image 

More information can be found at: SkeletonMode. (note that this is part of Emacs since version 22).

PS. The function puts an extra comment symbol at the end (not shown). This is a bug in my function. Any ideas how to get rid of this are welcome.

Using Preview for LaTeX documents in Emacs

Editing LaTeX documents with lots of equations can be sometimes hard if you want to refer to the equations in the text. You can use, if you have a big monitor, put the compiled file to the right of your editor, but on a notebook this is not a good option. If you use AucTex in Emacs, you can use the preview mode.

image

Here is the text without preview in Emacs And here it is with preview.

image

You can set the display options in the Option Group “AucTex” under “Preview Appearance”. One more thing: I always compile to pdf (not dvi). In order to generate the preview, you have to set this option back (C-c C-t C-p).

Automating writing LaTeX papers even further

In an earlier post I wrote on how to use R to produce all kind of figures and tables for a LaTeX paper. This time, I will show, how to automate this process even more.

My work flow consists of a batch file, that runs my model, sends the results to R, produces my tables and figures and sends these to my paper: If I change my model, I just have to run this batch file, and the results in my paper are automatically adjusted. One smaller problem is that I often write in my text on a specific value from a table (for example, the minimum time used). Every time I change my model, I have to check these entries by hand (of course, I could use knitr, but that is on my list for the future and at the moment, I want to keep my R-files separate from my LaTeX files).

Here is a neat trick, to automate this process. Suppose I have calculated the reduction in time in my model and exported the results to R. In my paper I have the following  sentence:

“As you can see in Table 1,in the scenario “Cruising” the reduction in driving time is 12.4%.”

In my R-script, I have the R-Code that produces this value or takes the value from my model results directly. Now I use some simple R-commands to produce a LaTeX file that can be included in my paper:

In my LaTeX-paper, I now just change the sentence to:

This produces the same sentence as before. However, if I change my model, the minimal reduction is changed and automatically adjusted.

Managing papers with Org-Mode and Reftex

Writing papers means reading lots of papers, and I often lose track of all the papers I am reading: Where did I file a certain paper? Which paper did I start reading? Which paper might be interesting?

A reading list would be nice, but such a list should be easy to handle. I have all my papers catalogued in JabRef and use AucTex and RefTex for writing and Org-Mode for my tasks.The solution should therefore combine the strength of all three. I found a nice post on how to this (http://tincman.wordpress.com/2011/01/04/research-paper-management-with-emacs-org-mode-and-reftex/) and adopted it to my needs.

Here is how my solution looks like (for managing tasks with Org-Mode see one of my previous posts)

image

  1. For every paper I am writing I have an org-file with my tasks, reading list, notes, and calls.
  2. I have four lists for my reading:
    1. Actual reading
    2. Should read: papers I glanced through and that look promising
    3. Not yet read: lower priority papers to be read
    4. Already read
  3. I insert new papers by using C-c ) (as in LaTeX/AucTex/RefTex). More on that later. Automatically, a link to this paper and the title are inserted (I save all papers as pdf-file and named with the first author name and the year of publication. They are all saved in the same dropbox directory). With “C-c C-c” I can tag the entries for easy searching later on.
  4. If I want to work on my paper, I open the org-file and, if I want to read a paper, I just click on its link and start reading.
  5. If necessary I can make short notes directly under the link.

Org-Mode makes it possible to use RefTex. For this it is necessary to put the information on where the bib-file is (third line in the next figure)

image 

The .emacs file has to be adjusted as follows:

Note: as I can make the reading to a TODO or a MIT (most important task), it will also show up in my daily agenda and I can also start reading from here by clicking on the link:

image

How to generate nice nomenclatures in LaTeX

If you have to write up your model results, often you have to write down the model documentation. My colleague at work pointed me to a great package in LaTeX that can make your life easier with respect to the nomenclature used. It allows you to produce a glossary with all your variables, parameters and acronyms without much extra work. The package is called “nomencl” and here is an example of a pdf generated with LaTeX:

image

As you can see, I have acronyms, parameters and variables as well as the reference to where they are used for the first time (if you only use variables and parameters you can even refer to the equation they are used for the first time).  

Using this package is quite easy:
Add:

and

to print the nomenclature.

Symbols can be added in the text like follows (after the equation they appear the first time):

\nomenclature{$vw$}{ Time cost multiple for walking time}

To generate the nomenclature you have to give a specific command. In Emacs you can add the following code to your .emacs-file

When you now hit C-c C-c you can choose the option “Nomenclature” and the necessary files are generated.

If you want to have the distinction between several groups, you just add the following code in your preamble and change the second part according your preferences:

To produce an entry for paramters, you just add [P] in front of the normal entry

One last remark: The entries might not be aligned properly. For this you can add in your preamble:

and choose the width accordingly. The nomenclature entries have rather much vertical space between them. This can be changed by adding one of the following lines to your preamble: