Sunday, August 31, 2014

Tips for LaTeX

So I used LaTeX using Texmaker in order to do my thesis. Here are some tips worth sharing.

Bibliography
For bibliography use BibTeX since you'll find BibTeX citations all over the Internet and use JabRef to edit the bibliographic details. Unfortunately the default for rendering citations is number in square brackets type like [1]. This might not be your preferred option as it doesn't show any information about what you're citing (although its great to save space in your text). If you want to use the "(author, year)" type of citation like "(Smith, 2014)" you'll need to use "\usepackage[round]{natbib}" in your preamble and then use "\pcite{}" instead of "\cite{}" to insert citations.

Unicode
Unless you're writing only in pure English, you'll often need to use non-ASCII characters. The LaTeX compiler doesn't let you compile non-ASCII text but allows you to make these characters using codes like \'{o}; however they make the text unreadable when used frequently. Instead you can type your text using unicode and then compile using XeLaTeX which allows you to compile unicode text.

Single line compilation
LaTeX requires multiple compilations to get a final product. So, if using command line commands, you'll need to enter something like this to have a bibliography in your document:
latex x.tex
bibtex x.tex
latex x.tex

In order to get around this, IDEs such as Texmaker allow you to customize a compilation button to make it run a command line instruction of your choice. In Linux you can use | to combine multiple instructions in one line (use && in Windows). Here is how I was doing my compilation in the Texmaker quick build function:

xelatex -synctex=1 -interaction=nonstopmode %.tex|makeindex %.idx|bibtex %.aux|makeindex %.nlo -s nomencl.ist -o %.nls|xelatex -synctex=1 -interaction=nonstopmode %.tex|xelatex -synctex=1 -interaction=nonstopmode %.tex

Notice that in Texmaker "%" means the file name of the main LaTeX source file.

Overfull/underfull hbox error handling
LaTeX has a rather poor handling of word wrap and often needs some help from you. When this happens, it gives you an overful hbox or underfull hbox error, depending on whether a line in the text is too long or too short. To fix it you need to either reword the words in the offending line (yes it's normal in the LaTeX community), or you add hyphenation marks using "\-" inside long words such as "ele\-phant". This tells LaTeX where to split a word if it occurs at the end of a line. LaTeX usually does this on its own but if it's an unknown word you'll need to help it.

Overfull/underfull hbox error handling on URLs
Long URLs are particularly problematic in LaTeX because you cannot hyphenate them as the hyphen might be thought to be part of the link. I found that the best way to handle it is to just line break the URL using "\\". Here's an example:

\href{http://www.url.com/very/very/very/very/very/long}{http://www.url.com/very/\\very/very/very/\\very/long}}

In this way you'll be telling LaTeX where to split the URL without adding hyphens.

Tables
Ouch. Tables are quite nightmare in LaTeX since you get no word wrapping in the table cells. You can fix this by putting your text in a par box which wraps words by itself and then putting this par box in the table cells here's the inefficient way to do this:

\begin{table}
 \begin{center}
  \begin{tabular}{ccc}
   \hline
   
    \parbox[t][][t]{2cm}{
     \begin{flushleft}\vspace{-0.3cm}Title 1\vspace{-0.3cm}\end{flushleft}
    }
    &
    \parbox[t][][t]{4cm}{
     \begin{center}\vspace{-0.3cm}Title 2\vspace{-0.3cm}\end{center}
    }
    &
    \parbox[t][][t]{6cm}{
     \begin{flushright}\vspace{-0.3cm}Title 3\vspace{-0.3cm}\end{flushright}
    }
    
   \\ \hline

    \parbox[t][][t]{2cm}{
     \begin{flushleft}\vspace{-0.3cm}Data 1\vspace{-0.3cm}\end{flushleft}
    }
    &
    \parbox[t][][t]{4cm}{
     \begin{center}\vspace{-0.3cm}Data 2\vspace{-0.3cm}\end{center}
    }
    &
    \parbox[t][][t]{6cm}{
     \begin{flushright}\vspace{-0.3cm}Data 3\vspace{-0.3cm}\end{flushright}
    }

   \\ \hline
  \end{tabular}
 \end{center}
 \caption{A caption about this table.}
 \label{tbl:some_identifier_to_refer_to_this_table}
\end{table}

This will make the table have the first column be left aligned 2cm wide, the second centred 4cm, and the third right aligned 6cm. The \vspace is there to keep the rows from being too big. Of course the problem with this approach is if you need to change the width or alignment of a column, in which case using custom commands would be the best way forward:

\newcommand{\col1}[1] {
 \parbox[t][][t]{2cm}{
  \begin{flushleft}\vspace{-0.3cm}#1\vspace{-0.3cm}\end{flushleft}
 }
}
\newcommand{\col2}[1] {
 \parbox[t][][t]{4cm}{
  \begin{center}\vspace{-0.3cm}#1\vspace{-0.3cm}\end{center}
 }
}
\newcommand{\col3}[1] {
 \parbox[t][][t]{6cm}{
  \begin{flushright}\vspace{-0.3cm}#1\vspace{-0.3cm}\end{flushright}
 }
}

\begin{table}
 \begin{center}
  \begin{tabular}{ccc}
   \hline
   
    \col1{Title 1}
    &
    \col2{Title 2}
    &
    \col3{Title 3}
    
   \\ \hline

    \col1{Data 1}
    &
    \col2{Data 2}
    &
    \col3{Data 3}

   \\ \hline
  \end{tabular}
 \end{center}
 \caption{A caption about this table.}
 \label{tbl:some_identifier_to_refer_to_this_table}
\end{table}

Commands are very useful for repetitive code in LaTeX but you need to use a different name for each different table; otherwise you'll need to use \renewcommand instead of \newcommand.

Positioning images and tables (floats)
I used to think that LaTeX intelligently places my floats at the best, most sensible place, but no it does not. It places them in the middle of bullet points without feeling guilty if you place your \begin{table} or \being{figure} anywhere near them. So what I did was that I placed the floats where ever it was sensible myself and then forced LaTeX to do it by surrounding the float with "\FloatBarrier" which requires "\usepackage{placeins}" in the preamble.