Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to have examples in my document look like this:

Section 2

(2.1) This is an example.

This is a reference to example (2.1).

That is:

  • the section number appears both next to the example and in the reference to it
  • both the labels and references to examples have brackets around them: I shouldn't have to do a whole lot of manual bracketing of \ref, i.e., not example (\ref{myexample}) for every single reference to every single example.
  • have the example numbers reset to 1 at the beginning of each section
  • ideally I want to use cleveref in order to minimise typing too :)

So far the closest I've got is with the linguex package:

\documentclass{article}
\usepackage{linguex}
\renewcommand{\theExNo}{(\thesection.\arabic{ExNo})}
\usepackage{cleveref}
\crefname{ExNo}{example}{examples}
\let\oldsection\section
\renewcommand{\section}{\setcounter{ExNo}{0}\oldsection}

\begin{document}

\section{First section}

\ex. Example the first.\label{ex1}

I'd like to refer to \cref{ex1}.

\section{Second section}

\ex. Example the second.\label{ex2}

I'd like to refer to \cref{ex2}.

\end{document}

However the output of this looks like:

1 First section

(1) Example the first.

I’d like to refer to example (1.1).

2 Second section

(1) Example the second.

I’d like to refer to example (2.1).

That is, I've got the automatic bracketing in the references, the counter resetting, but the examples themselves are marked (1) rather than (1.1) and (2.1).

I am presently agnostic regarding the various linguistic example packages, so if you know how to make this work with a different one, I would be grateful for that too.

share|improve this question
add comment

2 Answers 2

up vote 8 down vote accepted

The numbering of examples is hardwired in linguex; you can modify it by changing the corresponding command. Here's a commented minimal document.

\documentclass{article}

\usepackage{linguex}

% We want that the label to an example has also the section number
\renewcommand{\Exarabic}{\thesection.\arabic} 

% We want to reset the ExNo counter at each section
\usepackage{chngcntr}
\counterwithin{ExNo}{section}


\usepackage{cleveref}
\crefname{ExNo}{example}{examples}

% We want cross references of the form "example (1.1)"
\creflabelformat{ExNo}{(#2#1#3)}

\begin{document}

\section{First section}

\ex. Example the first.\label{ex1}

I'd like to refer to \cref{ex1}.

\section{Second section}

\ex. Example the second.\label{ex2}

I'd like to refer to \cref{ex2}.

\end{document}
share|improve this answer
1  
Thanks for the example! I've managed to achieve it with gb4e as well, and will supply that as another answer. –  puzzlement May 25 '12 at 8:28
add comment

My solution using gb4e:

\documentclass{article}

\usepackage{gb4e}
\renewcommand{\thexnumi}{\thesection.\arabic{xnumi}}

\usepackage{cleveref}
\crefname{xnumi}{example}{examples}
\creflabelformat{xnumi}{(#2#1#3)}
 \let\oldsection\section
 \renewcommand{\section}{\setcounter{exx}{0}\oldsection}

\begin{document}

\section{First section}

\begin{exe}
\ex Example the first.\label{ex1}
\end{exe}

I'd like to refer to \cref{ex1}.

\section{Second section}

\begin{exe}
\ex Example the second.\label{ex2}
\end{exe}

I'd like to refer to \cref{ex2}.

\end{document}

Presumably if using subexamples all the config for xnumi would need to be repeated for xnumii, xnumiii and xnumiv.

share|improve this answer
    
Instead of your redefinition of \section you can just use the chngcntr package and add \counterwithin{exx}{section}. –  Alan Munn May 25 '12 at 12:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.