Showing posts with label latex. Show all posts
Showing posts with label latex. Show all posts

Monday, September 14, 2009

Centering a figure on the page in LaTeX

With those wide margins of LaTeX, it sometimes happens that you have a figure or table that is too wide, and just sticks out to the right. Chances are that you want it centered, sticking out equally to the right and to the left, but tough luck: the standard center environment still aligns it with the left margin, and so does the \centering command.

I was about to write a package to provide an environment that centers its content on the page instead of inside the text. This is not too difficult, but preventing the overfull \hbox errors is tricky (they are so bad that even \hbadness=10000 has no effect). But then I stumbled into a standard LaTeX command that does exactly what I want.

So here's how to center your figure whilst ignoring the text margins:
\centerline{\includegraphics{...}}
It also works inside float environments such as figure. That was easy!

Wednesday, February 25, 2009

Sumatra PDF Viewer for LaTeX users on Windows

If you use LaTeX on Windows, or in particular pdflatex, you must have noticed that viewing the resulting PDF file is not as easy as you would think.

Sure, Adobe Reader displays it fine. But apart from being bloatware and slow as hell, it also locks the PDF file it is displaying. Thus you cannot rerun pdflatex until you close the file in Reader, because the output file cannot be overwritten. This is a pain in the arse.

Foxit Reader seems like a decent alternative; it is much faster and less bloated than Adobe Reader (even though the installer tries to get you hooked on several other pieces of software that you probably don't want). Foxit does not lock the currently viewed file; however, it offers no reload option, and if you overwrite the output file, it will only display blank pages for what's not currently cached.

Enter Sumatra PDF. A very lightweight and very simple program, contained in a single executable file, written by a single developer, but it has the one killer feature that LaTeX authors need. It automatically reloads the PDF file whenever it changes. And it stays on the same place in the document while it does this.

There is currently a bug that causes the window to demaximize when it reloads, but you can work around that simply by not maximizing the window in the first place. This little viewer will definitely make my life easier.

Thursday, January 22, 2009

Sequential page numbering in LaTeX

Did you ever notice that the page number that your PDF reader gave you was not the same as the page number printed at the bottom of the page? By default, some LaTeX commands, such as \tableofcontents, reset the page number to 1.

I don't like this behaviour. I just want my cover page to be numbered 1, the next to be numbered 2, etcetera. This way, the ‘physical’ and ‘virtual’ page numbers line up nicely. It also prevents some problems with hyperref, which will otherwise create duplicate page labels.

You can get sequential page numbering by putting the following snippet in your document preamble:

\let\oldsetcounter=\setcounter
\renewcommand\setcounter[2]{%
    \def\arg{#1}\def\pg{page}%
    \ifx\arg\pg\else\oldsetcounter{#1}{#2}\fi}

This simply overrides \setcounter, such that it ignores any attempt to set the page counter, which holds the current page number. Ugly, but it works.

Tuesday, August 5, 2008

A picture on the title page in LaTeX

In LaTeX, there is by default no way to put a picture on the title page or cover page that is produced by \maketitle. Surprisingly, no package seemed to exist for this either. Until now, because I wrote it.

The package titlepic.sty, which can be downloaded from CTAN, is very simple and easy to use. Install it by putting it in your texmf tree and rehashing, or simply drop titlepic.sty in the same directory as your .tex source document. It works with the default document classes article, report and book.

Include it as normal, with \usepackage{titlepic}. Then, along with the usual \title, \author and \date, put a command like the following:
\titlepic{\includegraphics[width=\textwidth]{cover.jpg}}
The argument to \titlepic will usually be an \includegraphics command, but it can actually be pretty much anything. The output produced by this argument will be typeset centered on the title page when you invoke \maketitle. (When you use the article document class, be sure to pass it the titlepage option, because articles do not have a title page by default.)

There are three optional arguments that control the vertical layout of the title page:

tt
Put both the title (and author, and date) and the picture at the top of the page, separated by a fixed amount of space.
tc
Put the title at the top of the page as with tt, but center the picture vertically on the page.
cc
Separate the title and the picture by a fixed amount of space, and center both together vertically on the page.

A full manual is also available. I hope this is of some use to someone. Enjoy!

Note: titlepic only works with the “standard” document classes article, report and book!. You may have some luck with other classes such as AMS, but no promises.

Sunday, June 29, 2008

LaTeX clever references

When you're referencing a section in LaTeX, you'd usually write something like
… as we saw in section \ref{sec:cake}.
But this is somewhat inconvenient. LaTeX knows that it's a section, right? So why the need to specify this? Worse, if you ever change it into a subsection, your reference will be wrong.

Luckily there's the command \autoref from the hyperref package. However, this too has some drawbacks, mainly that it does not provide a capitalised version. A better alternative is to use the package cleveref. It is not in the Ubuntu repositories, but you can simply download and extract the archive, then run latex cleveref.ins to obtain cleveref.sty and dump it in the directory along with your document.

Load the package with
\usepackage{cleveref}
and make sure it's the last package to load; that is, even laster that hyperref.

Using \cref the previous example becomes:
… as we saw in \cref{sec:cake}.
This will produce the text “… as we saw in section 3.”. At the start of a sentence you'd use the capitalised version \Cref:
\Cref{sec:cake} gives the recipe …
You can even write:
See also \cref{sec:cake,sec:lie,eq:recipe,thm:delicious}.
This produces “See also sections 2 and 3, eq. 5 and theorem 1.” Although I doubt that anyone would use this very often, it's still pretty cool.

You can customize the word that is printed before the number. For example, some people like them to be always capitalised. (I don't, but my supervisor does, and who am I to argue?)
\crefname{chapter}{Chapter}{Chapters}
\crefname{section}{Section}{Sections}
\crefname{subsection}{Section}{Sections}
\crefname{subsubsection}{Section}{Sections}
\crefname{figure}{Figure}{Figures}
\crefname{table}{Table}{Tables}

Etcetera. Note that subsections and subsubsections are usually all referenced to as sections.

Finally, as a bonus, here's how you make it work with references to \subfloats from the subfig package:
\crefname{subfigure}{Figure}{Figures}
(Capitalise according to taste, or even write “subfigure” if you like.)

For more options, see the cleveref documentation.

Wednesday, June 11, 2008

Integrating Inkscape graphics in LaTeX

Getting good-looking diagrams and figures into a LaTeX document can be tricky. My favourite software (and I think it ought to be anyone's favourite) for drawing such figures is Inkscape. This post explains how to get text in the proper font into Inkscape, how to put equations into Inkscape drawings, and how to get those drawings out of Inkscape and into your LaTeX document.

It is a good idea to use the latest version of Inkscape, because the program is rapidly being improved all the time.

LaTeX font in Inkscape

To make your figure look good in its environment, you can use the same font family that the surrounding body text uses. In LaTeX's case: Computer Modern.

Installing fonts

As Computer Modern is written in the METAFONT format, it cannot be directly used in Inkscape. For that, we need the font in OpenType (OTF) format, preferred for Linux (and MacOS?) systems, or in TrueType (TTF) on Windows. (Side note: here's an interesting article on the differences between all the font formats.)

The BaKoMa font bundle provides the Computer Modern font in these and some more formats. Download it here, then extract to a temporary directory. Installation is as follows:

Windows
Open up Fonts in the Control Panel and drag-and-drop all files from the ttf directory into here.
Ubuntu Linux
Open a file browser (Nautilus) and navigate to fonts://, then drop the fonts from the otf directory here. You may need to run Nautilus as root using the command gksudo nautilus.
Non-Ubuntu Linux (and Ubuntu Hardy, because they broke it)
Copy the files (as root) from the otf directory to anywhere you like inside /usr/share/fonts, then run sudo fc-cache -fv. (For a single-user installation, ~/.fonts might work, but no guarantees!)
Creating the figure

If you start Inkscape, new fonts with names like BKM-cmr10 should be available. Here, cm stands for Computer Modern, r means roman (normal body-text font) and 10 is the point size. Simply use this font for all the text in your illustrations to make them integrate seamlessly with the text in LaTeX.

Or, almost seamlessly. It seems that Inkscape (version 0.46) does something strange with the font size, or the BaKoMa fonts are too small to begin with. In any case, I find that using BKM-cmr10 at 12 points in Inkscape provides the best match to the default 10-point LaTeX body font. I personally prefer BKM-cmss10, the sans-serif version of Computer Modern, because it integrates nicely with abstract line drawings and with the surrounding serif body text, but if you use mathematics in you figures this is probably not an option.

LaTeX equations into Inkscape

Yes, it is possible to add mathematical symbols and equations to your Inkscape drawing! You can also use this for normal text, but it is more cumbersome than the font approach detailed above.

Installing textext

First, you need textext. Simply extract the two files from the archive into /usr/share/inkscape/extensions for a systemwide installation, and fix the permissions:
sudo chmod 644 textext.inx
sudo chmod 755 textext.py

(For a single-user installation, ~/.inkscape/extensions should work.)

You'll need some extra packages for the script to work. On Ubuntu:
sudo apt-get install python-lxml
sudo apt-get install pstoedit

On non-Debian based Linux distributions, install pstoedit and the Python lxml package in some other way. On Windows, see the textext web page for details.

Using textext

Now (re)start Inkscape, click Effects, Tex Text and type your LaTeX code! You can even load a preamble from a file to include additional packages such as amsmath. (Unfortunately the file must contain only the preamble, not an entire document.) The same problem occurs as with the previous approach: you need to set a scale factor of 1.25 to (approximately) match the font size of the LaTeX document. Close the dialog with the OK button or with Ctrl+Enter.

The equation (or other LaTeX text) is then placed as a group of shapes into Inkscape. To edit it (yes, that is possible!), select it and click Effects, Tex Text again. This feature is a little feeble, however: do not ungroup the text object, or else it will become uneditable.

From Inkscape to LaTeX

Exporting from Inkscape

When the figure is done, deselect all objects, then go to Document Properties and click Fit page to selection. This will adjust the page boundaries to fit exactly around all objects. Then save the figure to Inkscape SVG format (for later editing), but also save a copy as “PDF via Cairo”. Check the box to Convert text to paths, because otherwise the kerning seems to be messed up in the export.

Including in LaTeX

This is a simple matter of
\usepackage{graphicx}
in the preamble, and then placing the figure using
\includegraphics{diagram.pdf}
as usual. Do not use any of the scaling options of \includegraphics, since they will cause the text in the figure to scale as well, and it will no longer match the size of the surrounding body text.

Compile your LaTeX document using pdflatex (not normal latex, since that only handles inclusion of EPS files), and there you go!