Figuring out the Label and Caption in LaTeX

By Andriy Drozdyuk Posted on 12 April 2012.

Sometimes, when you place the label and caption inside the figure out of order, you get weird numbering in your document when you go to reference that figure. What many people don’t realize is that there is a reason for this madness and it is much easier to understand why this happens, then to try and remmember the order in which these things should go.

Why this happens

Perhaps if people know why this happens, it will be much easier to remember what goes where.

Short version

Caption command within a figure or table acts much like a sectioning command within the document. So if you don’t put a caption, the label has nothing to “label”, and the closest thing it can find is a section in which you are in.

Longer version

To understand what is going on we must establish a few things. First, a label command when placed anywhere in the document will label the section of the document in which it is places. So something like this:

Section \ref{sec-one} is great!  

\subsection{First section}
\label{sec-one} 

This is the first section

Will produce:

Section 2.11 is great!

2.11 First section
This is the first section

Second, label command appearing in a numbered environment acts differently. Numbered environments are things like equation, eqnarray, enumerate, figure, table… In each of these environments the label command acts differently. For example, in eqnarray environment the label can go anywhere before the \\ that ends the current equation - effectively giving each equation it’s own label!

The figure environment has different rules. In particular, the thing that creates numbering inside the figure environment is the caption command. A figure may have multiple captions, creating multiple “sections” within the figure. On other hand, if the caption command is never used, the label still uses the text numbering (which is the number of the section of the document it is in).

Remember: A figure may have multiple captions

So, if we have something like:

\begin{figure}
  BLAH
  \label{sec:one}
  \caption{Blah figure!}
  \label{fig:blah}
\end{figure}

we now know that Section \ref{sec:one} will refer to the section of the document in the text and will produce something like Section 2.11.

While Figure \ref{fig:blah} will actually refer to the thing we want and will produce something like Figure 1.

Conclusion

Since it is difficult to remember which goes first, caption or label, inside your figure, understanding why the order matters completely removes the need for memorization. Since the caption command is responsible for sectioning the figure environment, we know that we must call it at least once before using the label command. By knowing the why we can immediately deduce the how. And that, makes all the difference!

Reference

I would be lying if I said that I figured this out (no pun intended) on my own. The book “LaTeX: A Document Preparation System (2nd Edition)” by Leslie Lamport—as old as it is—was the primary source for my understanding of this topic.

Better Vim Configuration

By Andriy Drozdyuk Posted on 12 February 2012.

Having gotten tired of constantly re-configuring my Vim on every computer, I’ve decided to create a Git repository to store all the stuff. I’ve made it public, available for anyone to use: https://bitbucket.org/drozdyuk/vimfiles

Vimfiles Git project

What are the advantages of using Git to manage your Vim configuration?

  1. Good base to start from. You don’t need to fight with the ugly default configuration of Vim.

  2. Better plugin management. By using Git, you can take advantage of Git’s submodules feature, which allows you to easily fetch up-to-date Vim plugins straight from their repositories.

  3. Useful Plugins. My Git repository comes with a minimal number of useful plugins, which are tested and working. If you want you can easily add more!

On Programming

By Andriy Drozdyuk Posted on 15 January 2012.

Writers write. They say that if you want to be better at writing the only way to do it is to sit down and write. There is no other way. Reading books on “becoming a better writer” and going to “inspiration” workshops won’t help; there is no manual on how to become a famous writer. Because if there was, then anyone could become a great writer, publish a great book, and make a lot of money. And most of the writers I know, are far from millionaires.

The only way to get better is to write. A lot.

If you are a programmer - you write. You write a lot. You write every day. You write at work, at home, in the morning, in the evening, and sometimes even when you sleep. Whether you write for work or for fun, is irrelevant. Most writers don’t write at “work” and then at home. Their writing consists of about three hours every day (unless they are journalists of course). Compare that to the fifteen hours for most programmers.

Isn’t it curious then, how being a writer is regarded so highly by many people, as being a hard occupation? Nevertheless, writer is a much older occupation than a programmer, so we could probably learn a thing or two from them.

Workday

The very same work day of three hours per day, mentioned earlier, is not just a random number, made up randomly by writers because they are lazy. It is there exactly because that is the extent of how much creative work a human can do to their full potential during the day. So, when you say that you work ten hours a day, how much work are you really doing?

Elementary principles

Here I will blatantly rip off the writers’ bible The Elements of Style, which details some of the core principles that most writers must follow. While these rules are not agreed upon by all, it is the consensus that if you want to break the rules, you must, at least, know what they are.

Choose a suitable design and hold to it

Writing, to be effective, must follow closely the thoughts of the writer, but not necessarily in the order in which those thoughts occur.

Here, programmers, perhaps, have an upper hand, as setting down their thoughts in code is arguable easier (at least easier to check if what they wrote meets their expectations) than it is for writers to set down their thoughts on paper in a clumsy and imprecise language, such as English. In the next part, we are probably as much to blame as anyone else:

The first principle of composition, therefore, is to foresee or determine the shape of what is to come and pursue that shape.

How often have you plunged into coding (“writing”), without first thinking of what it is you are trying to achieve, only to find yourself, two days later, lost in the heaps of methods and classes that have become so intertwined that you can no longer understand how your masterpiece works?

This next piece of advice, took me about ten years to understand.

The position of the words in a sentence is the principal manes of showing their relationship. Confusion and ambiguity result when words are badly placed. The writer must, therefore, bring together the words and groups of words that are related in thought and keep apart those that are not so related.

I was always taught to break up my code into functions; sometimes even put a bunch of functions into a separate file. The focus was always on breaking code into smaller pieces, emphasizing reuse and avoid copying. But nobody ever told me that I should group related functions, variables and other things close together - so that they would be easy to read, without jumping from one file to another. After all, if you are reading a novel, you can’t imagine jumping to footnote and then back over to main text - this would distract from the main flow of the story! So why, if I am reading you code, should I jump from one file to another and another to figure out exactly what story you are telling me?

Conclusion

While I could go on and try to stretch this even further, I won’t. What I am trying to say is that if writers, who were around for thousands of years, still can’t get their stuff right - need to practice their craft every day and read rule books to not forget how to do what it is they do - how can such a young profession as a programmer claim to be perfect in it’s practice? You cannot afford to stop learning and refining the way you learn. And maybe, take a few tips from other older professions. Unless, of course, you want to stay a journalist all your life.

Tintin for the win

By Andriy Drozdyuk Posted on 13 January 2012.

I am so used to going to the movies alone that I no longer think of movie going as a social activity. My main purpose in going to the cinema has become to spend time by watching a good movie.

Finding a good movie is hard, without looking at the trailers, reading reviews and peeking at the storyline. This often results in spoiling it. And, given that about 99% of the movies are crap I really don’t want to risk accidentally spoiling that 1% that are good. So, yes, I tend to see more crap movies that I would have if I did the proper research (cough ninja assassin cough).

Fortunately there is a flip side. If you don’t think much of the movie when you go to see it, and it turns out great - the enjoyment is tenfold that of seeing it with high expectations. Such was the case for me with The Adventures of Tintin.

In my life I only saw a few cartoons that really surprised me. After all, the technology does not change that often, and most of them seem to look the same. It is when the technological development does not simply increase, but evolves, that I get surprised. This was when I saw Ratatouille. And now, when I saw Tintin.

What is remarkable about the technology used in Tintin is not that it is realistic, because it’s clearly distinguishable from a movie, but that it is, for the lack of a better word, perfect. Perfect, in the sense that it seems to capture the exact vision that the creators wanted to express. There were no technical barriers, no limitations to stop them from doing so. The fact that the picture retained its cartoonish look only enhanced it. It gave it an element of magic.

When watching Tintin you feel like adding any more realism would be bad. It would feel too much like the real life. Real life is boring. Tintin has an aesthetic similar to many of today’s video games. It has similar action sequences. It almost makes you feel in control (if you ever played of the Battlefield or Call of Duty games you will related). However, unlike most video games, the movie manages to inject some human emotion into the characters. While the captain can be seen by many as a crazy person, some of us may recognize in him an everyday person, lost in his ways, clinging onto the memories of old. Or on the verge of a breakdown from having spent most of his life in a meaningless occupation, only to wake up at the end of his life and realize what he missed.

All in all, I recommend this movie.