# Howto Write a Thesis using LaTeX: Custom commands

The tweaks presented in this post are in my opinion the most useful ones. When writing a thesis, there are typically a few expression and words that appear at many many places in across the document. I wrote my thesis about a slime mold called Physarum Polycephalum and I can’t count how often I needed to write this name in my thesis. Lazy as I am, I didn’t want to write this nasty word each time and also I wanted to avoid misspelling of it.

Fortunately, LaTeX gives the opportunity to define commands by yourself. This means, you can define a new command which is inserting a self defined string and use it across your document. To do so, simply write:

\newcommand{\phys}{\textit{Physarum Polycephalum}}

The expression in the first curly braces defines the command name, I called it \phys, and the expression in the second curly braces is defining the command to be executed. Here I want the string Physarum Polycephalum to be printed in italic style. So far so good, but where exactly is the benefit?

At any place in your document where you want to insert Physarum Polycephalum, no matter if it within a text section or a caption of an image, you simple write

 My topic is about \phys{}, which is a slime mold.

Please note the the curly braces after the command are very necessary in order to get LaTeX manage white spaces after the inserted text correctly!

Despite from saving time and reducing misspellings, there is another great benefit of using this technique. It allows you to easily change these word at only one position. Image I finished writing my whole thesis and after that, my Prof tells me that polycephalum should be written in lower case, I am unbelievably thankful that I do NOT need to go through my hundreds of pages and search for positions where I need to replace it. I only need to change it at one simple position!

By the way, one typical command I use in all my documents is

\newcommand{\etal}{\textit{et.~al.}}

Another trick that is somehow related to this topic, turned out to be very useful for me while writing the thesis. I defined three little commands and used them to give nodes to myself in the document:

\newcommand{\note}[1]{\color{red}(#1!)\color{black}}
\newcommand{\missref}{\note{[REF]}}
\newcommand{\todo}[1]{\textcolor{blue}{[Todo: #1]}}

The first command inserts a note for me in red color, so that I cannot miss the note on reviewing the document. The second one is used for indicating that there is a reference missing and that I need to insert it later on. But please, I really recommend to insert references on the fly! Believe me, after weeks or month of writing your thesis, the very last thing you want to do is inserting 100+ references before submitting your thesis. I used the third command to remind myself that there is something left to do. Have a look at the screenshot of the document using these commands, isn’t that really helpful for reviewing?

The command of this text part is looking like

Sch\"on \etal{} \missref{} have been scientifically proven that the
psychological stress during writing your PhD thesis leaves a disorder
from that you never will retire completely \note{Better double check
that!}.
\todo{Search for and include some examples here, maybe a graphic!}

# Howto Write a Thesis using LaTeX: Algorithms

People writing their thesis, paper or any other report with LaTeX tend to have some connection to programming. Thus, it is not uncommon to include some algorithms in the document. I prefer using the algorithmic package for providing source code. To include the package, add the following lines to the preamble in file Thesis.tex.

\usepackage{algorithm}
\usepackage{algorithmic}

Inserting an algorithm is then pretty straight forward, you only need to know the algorithmic specific commands, for a description, see for example http://en.wikibooks.org/wiki/LaTeX/Algorithms.

\begin{algorithm}[ht]
\caption{See how easy it is to provide algorithms}
\label{myFirstAlgorithm}
\begin{algorithmic}
\REQUIRE $a$
\STATE $b = 0$
\STATE $x \leftarrow 1:10$
\FORALL{x}
\STATE $b = b+a$
\ENDFOR
\RETURN $b$
\end{algorithmic}
\end{algorithm}

Algorithms can be referenced like an image, by calling:

??

If you like to include a list of algorithms at the end of your thesis, simply write

\listofalgorithms

at the position you want the list to appear.

# Howto Write a Thesis using LaTeX: Export Scalable Graphs from Excel to Inkscape

The trick with creating nice looking and scalable graphs is the same as discussed in Howto Write a Thesis using LaTeX: Generate Resolution Independent Figures Using Inkscape. Instead of saving the graph as ‘common’ image, meaning as bitmap, jpeg or png, graphs should also be generated as vector graphics.

Unfortunately, Excel does not provide an obvious way to export graphs as vector graphics (at least, I do not know any). But, fortunately, there is a little workaround which I will describe in this little tutorial.

First, assume we have some data present in an Excel sheet and generated any plot from the data. In the example below, I put in some data from the top of my head (without any scientific verification!) on how probable it is to went totally crazy, formatting your hard drive, destroy all your lab equipment and move to any abbey in Tibet for the rest of your live dependent on the amount of time you spend already to prepare your thesis.

To export this nice and somehow scary graph from Excel and to import it in our LaTeX document, we first need to select the graph by clicking on it, you should see a border around the figure. Note that it is important to select the figure only as we just want to export the graph and not the whole document!

Next, we select File->Save As … from the context menu and save it as PDF! See the screenshot below (Sorry, my installation of Excel is German)

That’s all that needs to be done in Excel. Actually, we would be easily able to import the PDF just generated into our LaTeX document, but there is some beautification needed which is best to be done with Inkscape.

To do so, we first open Inkscape and select File->Open (do NOT use Import!) from the context menu. Browse to the recently generated PDF file and press the Open button.

It might take a few seconds until the next dialogue should pop up. Do not touch any configuration at the import dialogue, simply press OK. The graph may be placed like that:

Note that the imported graph behaves like it would have been created with Inkscape meaning that you can easily click elements, remove or edit them. I personally do not like the grey border around the graph, so I remove it by clicking the border and removing it. You might want to change the colour, axis titles, font size or whatever…

Next, we go to File->Document Properties… to set the page size to fit the graph with some spacing of 10px.

Last, we save the plot as PDF file by selecting File->Save as from the context menu.
Remind that you need to select PDF as file type. That’s it, we just created a resolution independent scalable graph with Excel and Inkscape. Of course, this PDF can be included in our LaTeX document in the same way as any other image, namely by writing:

A scary and maybe exaggerated Graph is shown in Figure ??.
\begin{figure}[ht]
\centering
\includegraphics[width=0.5\textwidth]{fig/FreakingOut.pdf}
\caption[Short caption]{Detailed caption}
\label{fig:imageGraph}
\end{figure}

Also, the graphicx package should be added to the preamble of the Thesis.tex file:

\usepackage{graphicx}

# Howto Write a Thesis using LaTeX: Excel to LaTeX Table

Generating nice tables in plain LaTeX can be really annoying as it is very hard to get an overview of columns and rows for raw text. One possibility would be to use a WYSIWYG editor that comes with many development environments.

You can find a nice tool for generating tables in TexMaker by selecting Quick Tabular from the Wizard menu item.

But, in most cases, we do not want to insert tables manually. Instead, most of the data already exists in any other program and we would like to generate a table from our existing data. Also, there are much more specialised application for editing and generating tables, one and probably the most common one is Microsoft Excel.

Fortunately, there is a great tool that let’s you export your existing Excel table to LaTeX code, called  Excel2LaTeX!

# 1. Install Software

Go to http://www.ctan.org/tex-archive/support/excel2latex/ and download the latest Excel2LaTeX.xla file. Next, open the file with Excel.
You might get asked if you want to activate Makros and this is a potential security issue. As we know what we are about to do, we accept Makros. And that’s it, the add on is already installed!

You will find two additional buttons in the Add-Ins tab of your Excel Toolbar.

Note that the Add-Ins tab needs to be activated for some Excel versions separately!

Now comes the easy part, select the area of your table you want to export to LaTeX and click the Convert table to LaTeX button. The following dialog pops up:

Click either the Copy to Clipboard button to copy the LaTeX text or save it to a file by choosing Save to File:.
For some reason, copying the text snippet did not work for me on Windows 8, so I had to copy it manually!

Next, as we have the table as LaTeX code in our Clipboard, we only need to paste it to our LaTeX file. Navigate to the position where you want to insert the table in your TexMaker file and paste the content. Note that you might need to load the following packages in the preamble depending on how fancy your table is styled:

\usepackage{booktabs}
\usepackage{color}

The generated code for the example table looks like:

% Table generated by Excel2LaTeX from sheet 'Tabelle1'
\begin{table}[htbp]
\centering
\caption{Add caption}
\begin{tabular}{rrr}
\toprule
\multicolumn{1}{c}{\textbf{Name}} & \multicolumn{1}{c}{\textbf{Age}} & \multicolumn{1}{c}{\textbf{Score}} \\
\midrule
Maria & 23    & 1 \\
Thomas & 21    & 0.78 \\
\textit{Alicia} & 19    & 0.27 \\
Mark  & 31    & 0.45 \\
&       &  \\
\bottomrule
\end{tabular}%
\label{tab:addlabel}%
\end{table}%

Note that you also might want to update the table caption:

Cool, we just inserted a table from Excel to our LaTeX document! Wasn’t that much easier than typing it yourself?

Please also note, that the newly inserted table is automatically listed in the list of tables we inserted at the end of the document.