# Numbered examples in linguistics that I can refer back to

I do linguistics and I'm looking for a way to enumerate language examples that lets me reference to them in the text.

      (1) This is an example.

Sentence \ref{...} provides a good example of basic English.


Bonus points if it's usable with \item, which makes it easy to use with org-mode.

-
Related (or maybe duplicate): tex.stackexchange.com/questions/48353/… See also tex.stackexchange.com/questions/57276/… –  egreg Oct 16 '12 at 22:57

There are good packages for numbering examples in linguistics which have been specifically designed by linguists to meet our needs. Two of the most commonly used ones are gb4e and linguex. The questions egreg linked to in the comments show some examples. A third package, ExPex is still in development, and is extremely powerful, although somewhat non-LaTeX like in its syntax. Here are examples of all three packages which show off some of their abilities. In the first example, three levels of example embedding are shown. In the second example, the automatic lining up of glosses for use with examples not in the language of the article you are writing. I've coded the same examples in all three packages to highlight differences in their syntax.

## Overall evaluation

All three packages can very quickly produce standard examples and glosses quite quickly. For most users, and especially new users, either one of gb4e or linguex is a good choice. Personally I prefer gb4e because it implements true semantic markup of examples using LaTeX environments. Many linguex users prefer it, however, because the source input is simpler (and therefore not semantic at all). Since any good editor can insert chunks of code automatically, this has always struck me as being a weak argument.

I would really only recommend ExPex if you really require the degree of customization that it implements, especially in its handling of complex glossing systems. For some examples of its capabilities, see Type a interlinear text, bi or tri-lingual and Doing an interlinear text.

## gb4e

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{gb4e}
\begin{document}
\begin{exe}
\ex Wh/Quantifier Interactions\label{WHQ}
\begin{xlist}
\ex[]{What did everyone bring to the party?}
\begin{xlist}
\ex {Bill brought beer, Sue brought wine, Sam brought vodka \hfill \emph{Pair List answer}}
\ex {His favourite drink. \hfill \emph{Functional answer}}
\end{xlist}
\ex[]{Who brought every dish to the party?}
\begin{xlist}
\ex[*]{Bill brought the salsa, Sue brought the hummus, \\ Sam brought the tabouli \hfill \emph{Pair list answer}}
\end{xlist}
\end{xlist}
\end{exe}

As we can see in (\ref{WHQ}) there is a subject/object asymmetry in the availability of the Pair list and functional answers.

In example (\ref{citko21}) we can see some data from Polish.
\begin{exe}
\ex\label{citko21}
\begin{xlist}
\ex[]{\gll Kogo     Jan lubi e      a Maria podziwia e?\\
who.ACC Jan likes e.ACC and Maria admires e.ACC\\
\trans Who does Jan like and Maria admire?'}
\ex[*]{\gll Kogo/Komu Jan lubi e        a Maria ufa e?\\
who.ACC/DAT Jan likes e.ACC and Maria trusts e.DAT\\
\trans Who does Jan like and Maria trust?'}
\end{xlist}
\end{exe}
\end{document}


• Real semantic markup for examples
• Good support for grammaticality judgements
• Automatic lining up of glosses (same as linguex)
• Uses the standard \label and \ref reference system
• counters are easy to manipulate
• allows for use of _ and ^ outside of mathmode

• Hard to customize the basic list margins
• allows for use of _ and ^ outside of mathmode

## linguex

The other commonly used package for linguistic examples is linguex. Here's the same example coded using that package.

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{linguex}
\begin{document}
\ex. Wh/Quantifier Interactions\label{WHQ}
\a. What did everyone bring to the party?
\b. Bill brought beer, Sue brought wine, Sam brought vodka \hfill \emph{Pair List answer}
\b. His favourite drink. \hfill \emph{Functional answer}
\z.
\b. Who brought every dish to the party?
\b. *Bill brought the salsa, Sue brought the hummus, \\ Sam brought the tabouli \hfill \emph{Pair list answer}
\b. *Its maker \hfill \emph{Functional answer}

As we can see in \ref{WHQ} there is a subject/object asymmetry in the availability of the Pair list and functional answers.

In example \ref{citko21} we can see some data from Polish.
\ex.\label{citko21}
\ag. Kogo     Jan lubi e      a Maria podziwia e?\\
who.ACC Jan likes e.ACC and Maria admires e.ACC\\
\trans Who does Jan like and Maria admire?'
\bg. *Kogo/Komu Jan lubi e        a Maria ufa e?\\
who.ACC/DAT Jan likes e.ACC and Maria trusts e.DAT\\
\trans Who does Jan like and Maria trust?'

\end{document}


• Automatic lining up of glosses (same code as gb4e)
• Uses the standard \label and \ref reference system
• shorthands for next and previous examples without using labels
• simple input syntax with contextual determination of levels (at the expense of semantics)
• easy to customize the basic list margins

• markup is not semantic; contextual determination of levels
• some counters are hard coded and hard to modify
• handling of grammaticality judgements needs extra code to extend
• command for glossed examples is different from non-glossed examples
• example groups must be separated by a blank line
• cannot be used in conjunction with packages like xcomment which allow environments to be selectively excluded/included

## ExPex

The ExPex package is an extremely powerful package for formatting examples and glosses. It is useable by both Plain TeX and LaTeX. It is highly customizable and supports multiple levels of glossing. It uses a syntax that is more TeX-like than LaTeX like, (for example the basic example environment uses \ex ... \xe rather than \begin{ex} ... \end{ex} and implements customizations using a syntax based on the PSTricks key-value system.

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{expex}

% ExPex doesn't implement 3 levels of embedding, so this code is
% required for the first example
\def\beginsubsub{%
\par
\begingroup
\def\b##1{\par\leavevmode\llap{\hbox to 2em{##1\hfil}}\ignorespaces}}
\def\endsubsub{\par\endgroup}

\gathertags % write tags to external tag file
\Lingset{everygla=\upshape} % make first gloss line upright
\begin{document}
\pex~ Wh/Quantifier Interactions\deftagex{WHQ}
\a {What did everyone bring to the party?}
\beginsubsub
\b{ii.} {Bill brought beer, Sue brought wine, Sam brought vodka \hfill \emph{Pair List answer}}
\b{iii.} {His favourite drink. \hfill \emph{Functional answer}}
\endsubsub
\a {Who brought every dish to the party?}
\beginsubsub
\b{ii.} {\ljudge{*}Bill brought the salsa, Sue brought the hummus, \\ Sam brought the tabouli \hfill \emph{Pair list answer}}
\b{iii.} \ljudge{*}Its maker \hfill \emph{Functional answer}
\endsubsub
\xe
As we can see in (\getref{WHQ}) there is a subject/object asymmetry in the availability of the Pair list and functional answers.

In example (\getref{citko21}) we can see some data from Polish.
\pex<citko21> % an alternative syntax for tagging
\a
\begingl
\gla Kogo     Jan lubi e      a Maria podziwia e?//
\glb who.ACC Jan likes e.ACC and Maria admires e.ACC//
\glft Who does Jan like and Maria admire?'//
\endgl
\a
\begingl
\gla Kogo/Komu Jan lubi e        a Maria ufa e?//
\glb who.ACC/DAT Jan likes e.ACC and Maria trusts e.DAT//
\glft Who does Jan like and Maria trust?'//
\endgl
\xe
\end{document}


• extremely customizable using a key/value interface
• support for multiple levels of glossing with very rich formatting
• usable with both Plain TeX and LaTeX
• powerful reference and labeling system but using methods distinct from the standard LaTeX \label and \ref system
• independent macros for grammaticality judgements

• very idiosyncratic syntax for LaTeX users (although not for Plain TeX users)
• fairly steep learning curve (71 pages of documentation)
• only two levels of automatic embedding
-
@doncherry I'll add some more to the answer in a bit, but I don't have time at the moment. I prefer gb4e because its markup is semantic and linguex's isn't. With a good editor there's no real reason that the extra code in gb4e should pose a problem. For example, I use TeXShop as an editor and can insert the wrapping code for either a single or multiple example environment into my source with a single keystroke. –  Alan Munn Oct 17 '12 at 10:46
Really nice answer, Alan! –  Gonzalo Medina Oct 20 '12 at 3:47
Great, thank you so much! I think this could turn out to be an extremely helpful resource for every TeXing linguist. –  doncherry Oct 20 '12 at 17:31
Does anyone have a good setup for inserting environments for gb4e in AUCTeX/emacs? –  emacsomancer Jan 18 at 20:25

You may use the enumerate environment with regular label and \ref combination:

\documentclass{article}
\usepackage{enumitem}
\usepackage{lipsum}
\setlist{label*=(\arabic*)}
\begin{document}
\lipsum[1]
\begin{enumerate}%%[label*=(\arabic*)]
\item{This is an example \label{ex:basic}}
\end{enumerate}

\noindent Sentence~\ref{ex:basic} provides a good example of basic English. If you want to continue the numbering, you may use \verb|resume| feature provided by the \verb|enumitem| package.
%
\begin{enumerate}[resume]
\end{enumerate}

\noindent Sentence~\ref{ex:advanced} provides a good example of simple English. Hope this is useful.

\end{document}


This won't work if you have some other enumerate in the middle. resume will try to continue form that point. To have control, you may use series option provided by enumitem:

\documentclass{article}
\usepackage{lipsum}
%
\usepackage{enumitem}
%
\begin{document}
\lipsum[1]
\begin{enumerate}[label*=(\arabic*),leftmargin=1.5cm,series=example]
\item{This is an example \label{ex:basic}}
\end{enumerate}

\noindent Sentence~\ref{ex:basic} provides a good example of basic English. If you want to continue the numbering, you may use \verb|resume| feature provided by the \verb|enumitem| package. Some other \verb|enumerate| in the middle:
%
\begin{enumerate}
\item{This is an some other example not related to language \label{ex:other}}
\end{enumerate}
%
And again, one more language example.
\begin{enumerate}[label*=(\arabic*),leftmargin=1.5cm,resume*=example]
\item{This is an example running very very very very very very very very very very very very very very very very very very very long into two lines  \label{ex:advanced}}
\end{enumerate}

\noindent Sentence~\ref{ex:advanced} provides a good example of simple English. Hope this is useful.

\end{document}


Or define a new list:

\documentclass{article}
\usepackage{lipsum}
%
\usepackage{enumitem}
\newlist{example}{enumerate}{1}
\setlist[example]{label=(\arabic*),leftmargin=1.5cm}

\begin{document}
\lipsum[1]
\begin{example}
\item{This is an example \label{ex:basic}}
\end{example}

\noindent Sentence~\ref{ex:basic} provides a good example of basic English. If you want to continue the numbering, you may use \verb|resume| feature provided by the \verb|enumitem| package. Some other \verb|enumerate| in the middle:
%
\begin{enumerate}
\item{This is an some other example not related to language \label{ex:other}}
\end{enumerate}
%
And again, one more language example.
\begin{example}[resume*]
\item{This is an example running very very very very very very very very very very very very very very very very very very very long into two lines  \label{ex:advanced}}
\end{example}

\noindent Sentence~\ref{ex:advanced} provides a good example of simple English. Hope this is useful.

\end{document}

-
Next step is providing a proper example environment. :-) –  egreg Oct 16 '12 at 23:09
You could also use a \newlist to define your own list environment so you don't need to remember to specify the resume option. –  Peter Grill Oct 16 '12 at 23:10

You can create your own environment sentence based on the primitve list environment (the environments itemize and enumerate are based on list, amongst others).

I defined

1. an environment sentence that can hold exactly one (numbered) sentence. The macro \item is used but you don't need to provide it,
2. a macro \oneSentence that works just as the environment, and
3. an environment nsentence that can hold more than one sentence (you can use it for only one sentence, too, but must insert \item explicitly).

Number 1. and 2. has an optional argument that is used as a \label, to what you may refer to with \ref. In the multiple-sentences environment, you must use \label (as you would in an enumerate environment).

## Code

\documentclass{article}
\newcounter{mySentence}                               % provides the number
\newcounter{myTempSentence}                           % is used for a resume-like style for nsentence
\renewcommand*{\themySentence}{(\arabic{mySentence})}               % (1), (2), (3), …
\newenvironment{sentence}[1][]{
\ifnum\pdfstrcmp{#1}{}=0\else\label{#1}\fi                     % if opt label is provided, use it, otherwise do nothing (needs pdfLaTeX, solutions for other engines are available)
}{\end{list}}

\newenvironment{nsentence}{%
\setcounter{myTempSentence}{\value{mySentence}} % save old value
\begin{list}{\themySentence}{
\usecounter{mySentence}
\setcounter{mySentence}{\value{myTempSentence}}
\refstepcounter{mySentence}
}
}{\end{list}}

\newcommand*{\oneSentence}[2][]{\begin{sentence}[#1]#2\end{sentence}}

\begin{document}
Let's take a look at a sentence:
\begin{sentence}[myFirstSentence]
This is an example.
\end{sentence}
Sentence~\ref{myFirstSentence} provides a good example of basic English.
\oneSentence[thisIsALabel]{This works, too.}
After sentence~\ref{thisIsALabel} we look at a few more sentences.
\begin{nsentence}
\item This is another example. \label{mySecondSentence}
\item And this is a great sentence. \label{exampleForAdvancedEnglish}
\end{nsentence}