5

How do I get hyperref to allow me to link to individual examples created using expex (https://www.ctan.org/pkg/expex).

I am aware of a \label based workaround, however my full document has too many example sentences to allow retrofitting (a few thousand)

The behaviour I desire is that hyperref will allow me to click on the example reference and it will link it to the example in question.

Here is a minimal example of my usage, any pointers or suggestions appreciated!

\documentclass[11pt,twoside,openany,notitlepage]{book}
\usepackage[]{geometry}
\usepackage{graphicx}
\usepackage{expex}
\usepackage[table,xcdraw]{xcolor}

\gathertags

\usepackage{hyperref}


\makeatother

\begin{document}

Here is a bunch of text! and now there's an example (\getref{adj-predicates}).

\ex \deftagex{adj-predicates}
\begingl
\gla This way please. //
\glft `Bitte hier entlang' \trailingcitation source //
\endgl
\xe

Here is another bit of text and one more example (\getref{ex2}). (\getfullref{ex2.a})  is much better than (\getref{adj-predicates}).

\pex[nopreamble=true] \deftagex{ex2}
\a
\begingl
\gla Oh no! \deftaglabel{b} //
\glft `Oh nein!' \trailingcitation source1 //
\endgl
\a
\begingl
\gla I'm sorry! \deftaglabel{a} //
\glft `Es tut mir leid.' \trailingcitation source2 //
\endgl
\xe


\end{document}
1

1 Answer 1

4

I tried the patching method in the link that you provided, but it did not work for me either. Also, I had a similar issue because I wanted to use \pageref, but the solution provided only worked for \getref and not \getfullref also.

Fortunately, I was able to get both working by patching two commands: \deftag and \ep@putlabel@b.

The first is the one that fixes \getref. The second is required for \getfullref because it deals with each expex part in \pex...\xe defined by \a.

The key benefit here is what you were requesting: no need for \deftagex or \deftaglabel

The fix I am providing gives you two options. First, you can create new commands to keep the old \getref and \getfullref functionality (This method is safer, but requires a find/replace wherever you want they hyperlinks). Second, you can globally patch the commands so that they will all behave with the hyperlinks. I present the first option in comments above the second option in this MWE:

\documentclass[11pt,twoside,openany,notitlepage]{book}

\usepackage{geometry}
\usepackage{graphicx}
\usepackage{expex}
\usepackage[table,xcdraw]{xcolor}

\gathertags

\usepackage{hyperref}

\usepackage{etoolbox}
\makeatletter
\pretocmd\deftag{\hypertarget{#2}{}\label{#2}}{}{\fail}%
\patchcmd{\ep@putlabel@b}
    {\deftaglabel{\@optionaltag}}
    {\hypertarget{\@localextag.\@optionaltag}{}\label{\@localextag.\@optionaltag}\deftaglabel{\@optionaltag}}
    {}
    {\fail}%

%% Version 1: SAFE method - new methods for hyperlinking
%\def\gethyperref#1{\hyperlink{#1}{\getref{#1}}}%
%\def\getfullhyperref#1{\hyperlink{#1}{\getfullref{#1}}}%

%% Version 2: UNSAFE - overwrite \getref and \getfull ref globally
\let\old@getref\getref
\def\getref#1{\hyperlink{#1}{\old@getref{#1}}}%
\let\old@getfullref\getfullref
\def\getfullref#1{\hyperlink{#1}{\old@getfullref{#1}}}%
\makeatother

\begin{document}

Here is a bunch of text! and now there's an example (getref=\getref{adj-predicates}) (getfullref=\getfullref{adj-predicates}).  Next page has (getref=\getref{ex2}) and (getfullref=\getfullref{ex2.A})--(getfullref=\getfullref{ex2.B}).

\ex<adj-predicates>
    \begingl
        \gla This way please. //
        \glft `Bitte hier entlang' \trailingcitation source //
    \endgl
\xe

\clearpage

Here is another bit of text and one more example (getref=\getref{ex2}). (getfullref=\getfullref{ex2.A})--(getfullref=\getfullref{ex2.B})  is much better than (getref=\getref{adj-predicates}) on previous page.

\pex<ex2>
    \a<A>
        \begingl
            \gla Oh no! //
            \glft `Oh nein!' \trailingcitation source1 //
        \endgl
    \a<B>
        \begingl
            \gla I'm sorry! //
            \glft `Es tut mir leid.' \trailingcitation source2 //
        \endgl
\xe

\end{document}

Page one: expex pg1 with hyperlinking

Page two: expex pg2 with hyperlinking

If you don't want to use etoolbox for whatever reason, you'll need to copy and paste the entire definitions for \deftag and \ep@putlabel@b from expex and redefine them with the patches.

Lastly, I tested with XeLaTeX. The links worked for me in the default TeXworks (for MiKTeX), Adobe Acrobat, and SumatraPDF. I assume this should work in general, but I have not tested it exhaustively.

1
  • Note that with version 2, \nextx and friends will still not be links.
    – Keelan
    Commented Sep 21, 2023 at 9:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .