Myself, Coding, Ranting, and Madness

The Consciousness Stream Continues…

The LaTeX Atrocities: Dimensions, Glues, and Mus

28 Sep 2012 8:00 Tags: None

The last post looked at the internal handling of numbers in LaTeX1. Today, three more LaTeX types will be discussed. Note that this post is building on the concepts covered in the number handling post (as these are all data types which extends numbers), so it may be worthwhile briefly familiarising yourself with the usage of \numexpr2.

This introduction heavily implies that these constructs come directly from LaTeX; In fact, most of this post is an empirical exploration of chapters 10 and 12, and a small part of chapter 183 of the monolith that is the TeXBook4. All of the code samples compile without imports using TeXLive-LaTeX 2009 onwards.

Dimensions

Dimensions are essentially a special case of the number type, where the number is understood to represent a distance measured in the base unit of a point.

Dimension Units

Dimensions in LaTeX are defined using \dimexpr in terms of a number of different units of measure: mm, cm, in, pt5, em6, ex7. The example uses a 10pt font.

The addition of a unit into the type adds a new layer on complexity. The internal storage is done purely in the underlying terms of points, which, by ancient tradition, is slightly less than one seventy-secondth of an inch (approximately a third of a millimetre)8. Also, by the definitions used, The size of a capital M, which is 1em, is equivalent to the font size in points; in these examples, a 10pt font is used.

Units for \dimexpr

Not specifying a unit, or specifying an invalid one will cause LaTeX to throw the warning "Illegal unit of measure, pt inserted", and prompt for continuation.

Dimension Algebra

There are a number of operations we can do with these measurements, and a variety of ways in which they can be used. Addition and multiplication works roughly as one would expect, but does suffer from some unit conversion errors (all of these equations should be equal to 15mm or 1.5cm shown above).

Arithmetic with \dimexpr

It is important to note that the ordering of the operands when dealing with a mixture of numbers and dimensions is important — if the number is placed first, \dimexpr will throw a missing unit error as described above.

Page Layout

The different measurements that control page layouts are already well documented by Wikibooks9, so I will not dwell on it here. It is important to note, however, that these measurements are only for use when some other layout package — such as the widely used Geometry package — are not in use.

Glues

Glues are an extended version of dimensions which, rather than having one fixed value, represent a range of values which can be used to allow the TeX engine to perform some layout automation. It's worth noting at this point that the LaTeX concept of lengths maps to TeX glues, not to TeX dimensions, so the stuff explained here can be used with the \setlength and related commands.

Defining Glues

A Glue is made up of a dimension, with optional plus and minus error bars, which are also dimensions. This creates a range of dimensions, with a default value. The first thing to note is that all valid dimension expressions are also valid glue expressions.

\gluexpr as \dimexpr

To this, we can append the ‘error bars’10 using the plus and minus directives. For ease of reading, the next example first defines11 three of these expressions, and then performs some basic operations with them.

\gluexpr extensions and arithmetic

Glues in LaTeX

All of the LaTeX defined lengths are actually glues under the hood, and \setlength will take glue values. This is very useful for settings such as paragraph skips: you may wish to give the engine some leeway so it can make the margins look nice, and not split up a two-line paragraph across pages.

MuGlues

The third extra type for today is the mu-glue, defined using the \muexpr command, which appears to be a contraction of 'maths unit glue'. It is functionally equivalent to a glue, expect it is intended for use in Maths environments, and can only be be defined in terms of a new unit, mu. According to the TeXBook12, one mu is one 18th of an em. By the definition of a font size, these means that 1 mu = (font size / 18)pt.

\muexpr and mus

Using these expressions

For almost all LaTeX tasks, you're unlikely to need to actually use these values directly — understanding them, and being able to set values for LaTeX to use is generally enough. However, sometimes you need to so some custom spacing. Here is a quick spacing example to demonstrate:

\mskip and \hskip

The commands used here, \mskip and \hskip, will take full on glue-like expressions just as well. It is also worth noting that \mskip will not work outside of a Math environment of some description.

In LaTeX, is it often more command to use the \hspace{} and \vspace{} commands for performing manual spacing.

All of these code examples will be available as source files in the coming days, for people to play around with and improve.

  1. 1 Data which is of type 12. I don't know how I found this out, but I had a really good resource for this, which I've lost...
  2. 2 http://harcourtprogramming.co.uk/blogs/benh/2012/08/the-latex-atrocities-dealing-with-numbers/
  3. 3 In that order
  4. 4 http://www.amazon.com/The-TeXbook-Donald-E-Knuth/dp/0201134489
  5. 5 http://en.wikipedia.org/wiki/Point_(typography)
  6. 6 http://en.wikipedia.org/wiki/Em_(typography)
  7. 7 http://en.wikipedia.org/wiki/X-height
  8. 8 http://en.wikibooks.org/wiki/LaTeX/Useful_Measurement_Macros
  9. 9 http://en.wikibooks.org/wiki/LaTeX/Page_Layout
  10. 10 It took me rather a long time to get this example to work, because I attempted to do it without using a proper LaTeX installation — the range of two error messages that it gives you isnt' really enough to debug your latex with...
  11. 11 The way the \newcommand is used here, you can just substitute \expr with the appropriate line
  12. 12 Chapter 18, between 18.10 and 18.11 http://code.google.com/p/pdfsizeopt/downloads/detail?name=texbook.pdf