Numbered examples in linguistics that I can refer back to

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 {Beer. \hfill \emph{Individual answer}}
\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[]{Sue \hfill \emph{Individual answer}}
\ex[*]{Bill brought the salsa, Sue brought the hummus, \\ Sam brought the tabouli \hfill \emph{Pair list answer}}
\ex[*]{Its maker \hfill \emph{Functional 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}

output of gb4e code

Advantages and disadvantages

Advantages

  • 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

Disadvantages

  • 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?
        \a. Beer. \hfill \emph{Individual answer}
        \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?
        \a. Sue \hfill \emph{Individual answer}
        \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}

Advantages and disadvantages

Advantages

  • 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

Disadvantages

  • 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

output of linguex code

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
\advance\leftskip by 2em
\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{i.} {Beer. \hfill \emph{Individual answer}}
  \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{i.} Sue \hfill \emph{Individual answer}
  \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}

output of expex code

Advantages and Disadvantages

Advantages

  • 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

Disadvantages

  • 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

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]
  \item{This is an example \label{ex:advanced}}
\end{enumerate}

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


\end{document}

enter image description here

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}

enter image description here

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}

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][]{
     \begin{list}{\themySentence}{\refstepcounter{mySentence}}\item % \item already provided
     \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
     \addtocounter{myTempSentence}{-1}
     \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}
Sentences~\ref{mySecondSentence} and~\ref{exampleForAdvancedEnglish} are great examples for advanced English.
\end{document}

Output

Output