Useful LaTeX packages: linguistic examples

This is the conclusion of a short series of entries on LaTeX packages I found useful while preparing the examination copy of my PhD thesis.

Today’s entry is a package for displaying linguistic examples (ie, samples of text which you then want to discuss and analyse).  The LaTeX for Linguists Home Page is a good general resource for linguists and computational linguists using LaTeX. I discuss gb4e here because I had to do some messing around to get it to display example numbers the way I want (and the way my supervisor wanted: he likes in-text references to look like “example (4.1)” rather than “example 4.1”), and to get it to work with cleveref, and no one seems to have written that up to my knowledge.

gb4e

gb4e is a linguistic examples package.

usepackage{gb4e}

Input looks like:

begin{exe}
ex This is an example sentencelabel{example}
ex This is another example sentence.
end{exe}

This is a cleveref reference to cref{example}.
This is a normal reference to example (ref{example}).

You can mark sentences with * and ? and so on:

begin{exe}
ex[*] {This is an sentence ungrammatical.}
ex[?] {This is an questionably grammatical sentence.}
end{exe}

You can do sub-examples:

begin{exe}
ex This is an example.
ex
begin{xlist}
ex This is a sub-example.
ex This is another sub-example.
end{xlist}
end{exe}

A few things to do to make gb4e play really nicely. First, some cleveref config. gb4e doesn’t yet automatically tell cleveref how to refer to examples, so you need to tell it that the term is “example”, and second, if you want braces around the number (“example (1.1)” rather than “example 1.1” you need to tell it to use brackets:

% tell cleveref to use the word "example" to refer to examples,
% and to put example numbers in brackets
crefname{xnumi}{example}{examples}
creflabelformat{xnumi}{(#2#1#3)}
crefname{xnumii}{example}{examples}
creflabelformat{xnumii}{(#2#1#3)}
crefname{xnumiii}{example}{examples}
creflabelformat{xnumiii}{(#2#1#3)}
crefname{xnumiv}{example}{examples}
creflabelformat{xnumiv}{(#2#1#3)}

Also, by default, the gb4e numbering does not reset in chapters. That is, your examples will be numbered (1), (2), (3) etc right through a thesis. You probably want more like (1.1), (1.2), (2.1), (2.2), ie chapter.number. Change to this with the following in your preamble:

% Store the old chapter command so that
% our redefinition can still refer to it
letoldchapterchapter
% Redefine the chapter command so that it resets the
% 'exx' counter that gb4e uses on every new chapter.
renewcommand{chapter}{setcounter{exx}{0}oldchapter}

% Redefine how example numbers are shown so that they are
% chapter number dot example number
renewcommand{thexnumi}{thechapter.arabic{xnumi}}
You could also get it to reset in sections by replacing chapter and thechapter with section and thesection in the above.

Thanks to the TeX Stack Exchange community for their help with this. See Section based linguistic example numbering with brackets for more information.

Useful LaTeX packages: within document references

This is part of a short series of entries on LaTeX packages I found useful while preparing the examination copy of my PhD thesis.

Today’s entry is packages relevant to preparing within document references. These are both fairly new to me, although not absolutely now.

hyperref

This package turns cross-references and bibliography references into clickable links in your output PDF (at least if you generate it with xelatex or pdflatex), without you having to do anything other than the ref (or cleveref’s cref) and cite and so on commands.

usepackage{hyperref}

You will probably want to modify its choice of colours to something more subtle:

usepackage[citecolor=blue,%
    filecolor=black,%
    linkcolor=blue,%
    % Generates page numbers in your bibliography, ie will
    % list all the pages where you referred to that entry.
    pagebackref=true,%
    colorlinks=true,%
    urlcolor=blue]{hyperref}

Use black if you want the links the same colour as your text.

One note with hyperref: generally it should be the last package you load. There are occasional exceptions, see Which packages should be loaded after hyperref instead of before?

cleveref

cleveref is a LaTeX package that automatically remembers how you refer to things. So instead of:

see chapter ref{chapref}

you use the cref command:

see cref{chapref}

It handles multiple references nicely too:

see cref{chapref,anotherchapref}

will generate output along the lines of “see chapters 1 and 2”.

Use

Cref{refname}

to generate capitalised text, eg “Chapter 1” rather than “chapter 1”

To use it:

usepackage{cleveref}

It shortens the word “equation” to “eq.” by default, if you don’t like that, then:

usepackage[noabbrev]{cleveref}

For some packages that don’t yet tell cleveref how to refer to their counters, you will get output like “see ?? 1” rather than “see example 1”. You use the crefname command in the preamble to tell it what word to use for each unknown counter, examples of crefname will be shown tomorrow for gb4e.

Useful LaTeX packages: tables and figures

This is part of a short series of entries on LaTeX packages I found useful while preparing the examination copy of my PhD thesis.

Today’s entry is packages relevant to preparing tables or figures. Again, some are pretty widely known and some aren’t.

rotating

If you have a big table or figure that should be rotated sideways onto its own page:

usepackage{rotating}

And then you can replace the table and figure commands with:

begin{sidewaystable}
%Giant table goes here
end{sidewaystable}
begin{sidewaysfigure}
%Giant figure goes here
end{sidewaysfigure}

dcolumn

The dcolumn package produces tabular columns that are perfectly aligned on a decimal point (ie all the decimal points in that column are exactly underneath each other), which is usually how you want to display decimal numbers.

usepackage{dcolumn}

% create a new column type, d, which takes the . out of numbers, replacing the .
% with a cdot and aligning on it.
newcolumntype{d}[1]{D{.}{cdot}{#1}}

Now that you have defined the column type, you can use d in the tabular environment, where the numeric argument is the number of figures to expect after the decimal point. You don’t have to use exactly that number of figures in every entry, just that that’s how much room it will leave.

% a tabular enviroment with a 1 and 3 figures after the decimal point column
begin{tabular}{d{1}d{3}}
1.6 & 1.657
\
2.0 & 6.563
\
7 & 6.26
\
end{tabular}

One annoying aspect of this package is that for the headers of that column, which probably aren’t numbers, you will need to use multicolumn to get them to display nicely.

% a tabular enviroment with a 1 and 3 figures after the decimal point column
begin{tabular}{d{1}d{3}}
multicolumn{1}{c}{Heading 1} & multicolumn{1}{c}{Heading 2}\
1.6 & 1.657
\
2.0 & 6.563
\
7 & 6.26
\
end{tabular}

You can mix the d column type with the usual l, r and p column types.

threeparttable

You can’t use footnote in a floating table. This is one of several packages that allow table footnotes in various ways.

usepackage{threeparttable}

threeparttable doesn’t cause tables to float on its own, so you usually want to wrap in a table command:

begin{table}

begin{threeparttable}

% Normal bits of your table go here, and use tnote{a} and
% tnote{b} and so to generate a note mark

begin{tablenotes}
tnote General note
tnote General note 2
tnote[a] Note for mark a
tnote[b] Note for mark b
end{tablenotes}

end{threeparttable}

caption{Caption goes here}
end{table}

Unfortunately you need to generate the a, b, c (or whatever) numbering manually.

The general tnote entries are useful for things like “Bold entries are highest in the column”, so that they don’t need to go in the caption.

Useful LaTeX packages: bibliography

I’m going to post a short series of entries on LaTeX packages I found useful while preparing the examination copy of my PhD thesis. Largely this is just so that there’s a reference if my wiki page goes away, but also because I think many people use LaTeX the way I use it, that is, I got wedded to a bunch of packages 10 years ago and never really looked around for more recent stuff.

Today’s entry is a pretty slow start: the bibliography packages I used are pretty standard.

natbib

This is one of the most sophisticated and widely used packages for Harvard-style references (ie, “(Surname, Year)” rather than “[1]” style references).

usepackage[round]{natbib}
bibliographystyle{plainnat}

Inside your text use citep for a reference in parentheses “(Surname, Year)”, and citet for a in-text reference “Surname (Year)”. Its important to note that the plain cite command is equivalent to citet, which you may not expect.

You can use citeauthor to get just “Surname” and citeyear to get just “Year”.

bibentry

This is a useful add-on to natbib, which allows you to insert full bibliography entries into the body of your text. This is useful in the declaration portion of a thesis (where you say something like “this thesis incorporates revised versions of the following published articles”).

usepackage{bibentry}
nobibliography*

Then later on when you want to insert a full bibliography entry into the middle of your text:

bibentry{citationkey}