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.

This article is part of my series LaTeX packages:Read the whole series at LaTeX packages.