Beginners Notes for Latex

This is just a random collection of notes about Latex covering hurdles I hit early on.

Including SVG images

Long story short, don’t bother, it’s not worth the hassle. In theory you can \usepackage{svg} but adding just that gave me a ton of errors mentioning xcolor names. Adding the line \PassOptionsToPackage{svgnames}{xcolor} to the preamble fixed that but when I tried to add an image I got an error starting Package svg Error: File `image-name_svg-tex.pdf' is missing.... I suspect under the hood the Latex SVG package converts the SVG image into a PDF+Latex and then includes that and something is going wrong.

The second answer to this Tex StackExchange question seems to be the simplest way to get the desired result. Essentially, you open the SVG in Inkscape and then export it as a PDF. When you select PDF as the export option have a look in the export options (bottom right cog button) and you’ll find an option “Omit text in PDF and create Latex file”. That’s a little misleading, you’ll get a Tex file even if there is no text in the image. The Tex file is what actually gets included in your document and it will import the image data (and draw any text).

If like me you have images in an images directory you’ll need to include the import library \usepackage{import} and then import the image created above like this:

\begin{figure}
    \centering
    \def\svgwidth{\columnwidth}
    \import{images}{image.pdf_tex}
\end{figure}

Notice that the file you are importing has the .pdf_tex extension. There is also excellent this guide which does a better job at explaining it than I do.

Images Always Appear at the Top of the Page

When I included an image with this code:

\begin{figure}
    \centering
    \def\svgwidth{5cm}
    \import{images}{image.pdf_tex}
\end{figure}

I found it always appeared at the top of the page, which wasn’t useful. The trick to get it to appear where it naturally should is to change the begin line to \begin{figure}[!htbp]. This is described here in more detail. Briefly, though, it tells Latex to just put the float where it appears in the source text and not to try and find the best position for it. See also this question.

Make Words Stick Together

In the dedication I was writing I had an awkward line break appearing in the middle of a name. This was causing just the surname to appear on a new line. What I wanted was the whole name to be on the next line or none of it. The solution is really simple, just include tildes ~ between words you want to stick together.

Use Greek Characters in Bibliography Entries

This one had me scratching my head for a while. I don’t know, for sure, that this is the whole solution but it’s a decent chunk of it. The problem I had was I had to write α-tin in one of my references. Latex, for all it’s wonderfulness, doesn’t seem to handle non-ascii characters very well. It feels like software from yesteryear. The solution is to \usepackage{textgreek} and then write the bibliography entry like this \textalpha-tin. Of course this makes perfect sense as soon as you realize that what’s in the Bib file just gets passed to Tex for processing once all the bits have been joined together to make a reference entry. See here for more background.

Inserting Chemical Drawings

This one took me a really long time to come up with a process I was even vaguely happy with. I started by wasting a load of time looking for a free chemistry drawing package. I seem to recall, back when I used to do chemistry, that we used ChemDraw as it was free at the time (maybe we had a site license), it certainly isn’t free any more. What I eventually settled on was ChemSketch which is adequate, I suppose. I tried every one I could reasonably get my hands on and they were all terrible. ChemDoodle looked fairly good but it was pay for and I’m allergic to paying for things. One downside of ChemSketch is that it’s Windows 64bit only, this is pointed out numerous times on the website. It seems to work just fine under Wine though. Another major downside is that it doesn’t have any really nice export options like SVG. The best seems to be PDF but then it leaves a lot of white space around the drawing so you can run the drawing though an online PDF cropping service like this. It’s certainly not a nice workflow but it works. The PDF can then be included like this:

\begin{figure}[!htbp]
	\centering
	\includegraphics[]{images/name_of_figure.pdf}
	\caption[short name for figure list]{full caption}\label{fig:name_of_figure}
\end{figure}

How to get a Table to Fit

The first document I had to format was my wife’s thesis which contained a number of tables with content that wasn’t allowed to break in a row (it was chemical formulae). Overfilled table cells would cause warnings in the log along the lines of:

Overfull \hbox (2.09827pt too wide) in paragraph

This is one of the more descriptive errors. It’s reporting that the content it was trying to squeeze into one of the cells was two and a bit points too wide. The best quick solution I’ve found to this is to add a fontsize command like this:

\fontsize{8pt}{8pt}\selectfont

It has to appear just just after \begin{table} and will then apply to the whole table. It doesn’t appear to apply to the caption but I saw a comment saying it did. Obviously you can change the point size to whatever makes sense for your table. Rather than fontsize you can also use \tiny or \small but I prefer to be able to specify the size myself.