4

As things work by default in LaTeX, references to example numbers with \ref simply print the number of the example, with no additional formatting. However, almost all of the time in the papers I write, example numbers should be enclosed in parentheses, like '(2)' instead of just '2'. (These are not equations that I'm referencing, they're linguistic examples made with gb4e; I'm not sure the answers I've seen for equation references would work.)

Is there a way to introduce this functionality automatically, without breaking things like hyperref? Additionally, would there be any way to avoid parentheses for references to figures and tables, and instead output a bare '2' in these cases? I'm content with having figure references in parentheses, as it's a pain to manually enclose every other \ref command in parentheses.

Here's an MWE of how it works at the moment:

\documentclass{article}
\usepackage{gb4e}
\usepackage{hyperref}

\begin{document}

You can see the behaviour in example \ref{example}.

\begin{exe}
\ex\label{example} This is an example.
\end{exe}

\end{document}
  • 2
    It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – Peter Grill Jul 11 '16 at 20:27
4

One way to do this is to use cleveref:

\documentclass{article}
% Package loading order matters here.
% Load these packages in this order, after all other packages.
\usepackage{gb4e}
\usepackage{hyperref}
\usepackage[compress]{cleveref}
\crefname{xnumi}{}{}
\creflabelformat{xnumi}{(#2#1#3)}
\crefrangeformat{xnumi}{(#3#1#4)--(#5#2#6)}

\begin{document}
As we can see in \cref{lingex} the parentheses are correctly placed.
\begin{exe}
\ex\label{lingex}
\begin{xlist}
\ex An example.
\ex A second example.
\end{xlist}
\end{exe}
\begin{exe}
\ex\label{secondex}
\begin{xlist}
\ex Another example.
\ex And another.
\end{xlist}
\end{exe}
We can also refer to groups of examples like as shown in \cref{lingex,secondex} or we can refer to a range as in \crefrange{lingex}{secondex}.

This is a reference to \Cref{table}. Notice how it is formatted differently.

\begin{table}[hb]
\centering
\begin{tabular}{c}
Pretend this is a table.
\end{tabular}
\caption{A table caption\label{table}}
\end{table}

\end{document}

output of code

  • This is exactly what I needed, thank you! This package is amazing! – Sjiveru Jul 12 '16 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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