Sunday, May 12, 2013

Conlanging with LaTeX, Part Two

In the previous post I suggested a basic LaTeX tutorial you might use to get a basic command of LaTeX. I'm going to assume everyone reading this has played around a little with LaTeX.

Before you can produce any document in LaTeX, you need to tell it a little about what you intend. The very simplest trussing for this will look a lot like this:

\documentclass{article}

\begin{document}
Saluton!
\end{document}

The space between the \documentclass and \begin{document} lines is called the preamble, and this is were you can put all sorts of other declarations to change how LaTeX works, either by changing its default behavior or by adding new functionality. For this post, I'm going to mention a few things that are useful for conlangers to have in their preambles. Specifically, I'm going to focus on what LaTeX calls packages. Fortunately, if you do a web search on most LaTeX packages you can get good documentation on how to use them effectively.

The first thing you should know, is that the font size can be changed in the \documentclass line. I usually like a 12pt font, but you can also ask for 10 or 11 points. As always, you need to use other packages to get more font size options.

\documentclass[12pt]{article}

By default, LaTeX has rather large margins. I have no need for so much whitespace, so I use the fullpage package to pull out the margins to something less wasteful of paper:

\documentclass{article}
\usepackage{fullpage}

\begin{document}
Saluton!
\end{document}

And that's all you need to say. Simply by using the package, the changes you want take effect.

The next big thing is a package to manage fonts. In the old days, dealing with fonts in LaTeX was truly a nightmare — strange font names, freaky encodings, fonts themselves in a special LaTeX format, fights between different packages and font expectations, etc. These days, the XeTeX version of LaTeX has much simpler font management capabilities, though you still have to do a little work.

For XeTeX to find a TrueType or OpenType font, it needs to be installed in the usual places your OS would put the font, since it relies on local mechanisms to find them.

There is a utility package that helps manage all this, fontspec:

\documentclass{article}
\usepackage{fullpage}

\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\setromanfont{Gentium Basic}
\newfontfamily\gplus{Gentium Plus}

\begin{document}
Saluton!
\end{document}

So, what I'm doing here is loading up the package, then immediately running some commands provided by that package to set some font defaults. The \defaultfontfeatures line tells XeTeX I want to use the normal, old-fashioned LaTeX digraphs and trigraphs for certain kinds of characters. For example, it will convert three consecutive minus signs into an em-dash (—), in the usual LaTeX way. If you omit this line, many examples of LaTeX you might find on the web may break in subtle ways for you.

The next line, \setromanfont picks the default font for the document. I like the Gentium family, since it has lots of accenting support, as well as Ancient Greek, which I often find myself using.

The next line lets me create a font command. It turns out, the Gentium Plus font has much better support for IPA characters, so when I want to type IPA, I can use the \gplus command to get the IPA. Note that you have to enclose the commands created that way in curly-braces to limit their effect. An example from my Kahtsaai grammar:

 \item Double \LL{ł}, \LL{łł}, is
pronounced [{\gplus ɮ}:]. 

So, the \newfontfamily command needs a command name, which you choose, and then a font name. Here, I picked the name \gplus (the leading backslash is required for all LaTeX command names).

The fontspec package is vast and powerful, allowing many interesting effects. You can look at the documentation to learn about more of its capabilities. I will just add that it is common for LaTeX documentation to have a large section at the end with the actual package code, with explanations. Most of the time, that is safely skipped.

I like to use different sorts of underlining in examples, for which the package ulem is very useful. Just use \usepackage{ulem}, and then you get some new LaTeX commands:

\uline{Just a normal underline.}
\uuline{A double underline}.
\uwave{A wavy underline}.

Some people will want to use the tipa package, which provides a funky encoding for IPA. I don't use it these days, since I don't always like the look of the output.

These are the most basic packages I use. There are a few more, but they are complex enough, or add such large new functionality, that I will save them for future posts.

Do experienced LaTeX-er conlangers have other basic packages to recommend, other than things like multicol, makeidx, multicol or hyperref, which I hope to talk about more in the future?

In the next post, I will talk a bit about defining your own simple macros to ease some formatting tasks, and tables tables tables...

1. Hi William, nice start! :)

You asked what packages we would recommend. Personally, if you're using XeTeX, I would advise people to always include the packages xunicode and xltxtra. They belong as much with XeTeX as fontspec. What they do is add additional controls to input Unicode characters that would otherwise be difficult to access.
In particular, they provide the \textipa{} command, which allows one to input IPA characters using the "funky" tipa encoding, but without tipa's font issues. Could be useful if you're limited to ASCII in your source for some reason. It'd be nicer if you could just input X-SAMPA, but that's a bit too much to ask I'm afraid :) .

Another package that's necessary as soon as you want to add figures in your text is graphicx, which allows you to add pictures in various formats.

Besides Mapping=tex-text, I usually add Scale=MatchLowercase to \defaultfontfeatures. Basically, this ensures that different fonts will work well together by scaling them so their lowercase characters have the same weight.

Finally, one thing you may want to mention is that you don't need to install fonts on your system to use them with XeTeX and fontspec. For some reason, you may have a font that is very specific to one project, and don't want to pollute your system with it, or you can't install custom fonts on it. That's not a problem with fontspec, which will happily use fonts that are simply put in the same folder as the source of the LaTeX document. The only difference here is that you will have to call those fonts using their finenames (including extension) rather than with their names. So for instance "CharisSILR.ttf" rather than "Charis SIL Roman". This method actually works even if you're using the online editor ShareLaTeX, so it's nice to keep it in mind :) .

That's about all I have to say, I'm looking forward to the next instalments of this series :) .

2. Aargh! I always forget about xunicode and xltxtra. In my usual new XeTeX document template I now use these, but my older docs are a bit retro, and unfortunately I consulted those while writing this. I'll include them in future examples. I'm especially pleased to know you can get a decent IPA macro that way.

Strangely, I almost never include pictures in my docs. Even for the Thesaurus, I just used a LaTeX package to draw the semantic maps. :)

I knew there had to be some way to include non-OS-installed fonts, but I assumed it would be seriously ugly so didn't research it. It's nice to know it's actually fairly easy.

Thanks for the suggestions.

1. Yeah, when I do some XeTeX I usually use TeXworks, and its XeTeX template contains xunicode and xltxtra already, so I never forget them :) .

I tested the \textipa{} macro. It works. unfortunately, since it uses the tipa encoding rather than something more common like X-SAMPA, it's a bit of a pain to use. I never use it, but that's only because I've got an input method on my computer that allows me to type X-SAMPA and get IPA in return :) .

For pictures, I was simply thinking of those nice dictionaries that include pictures, and thought it might be nice for people to know how to do that in LaTeX :) .

Yeah, it's very easy to include non-OS-installed fonts. You don't even have to put them in the same folder as your root document, but then you have to include the path along with font filename. It used to work only with absolute paths, which was a bit of a pain, but I believe nowadays it works with relative paths too. Still easier to simply put the fonts in the same folder as the root document.
There *is* a small issue if you use non-installed fonts: if you use the \setmainfont command, you have to separately specify the various forms of the fonts, while if you use it with an installed font you just give it the font name and it finds the bold, italic and bold italic variants for you. It's still not too ugly. For instance, with a document of mine that uses Charis SIL as its main font (and the various Charis SIL fonts in the same folder as the root document), I put in the preamble the following command:
\setmainfont[Path,
UprightFont=*R.ttf,
BoldFont=*B.ttf,
ItalicFont=*I.ttf,
BoldItalicFont=*BI.ttf]{CharisSIL}
And it works fine that way ("Path" is necessary here to tell XeTeX not to look for installed fonts. When you don't give it a value, it will just look in the root document's folder).

By the way, the fontspec documentation is very good, and very legible. I advise anyone who wants to us XeTeX to read it.