5

I use the glossary package with xindy and compile my document with lualatex.

Is it possible to display specific locations in the glossary?

My favoured way would be:

  1. The standard \gls{} command should display the linked glossary item and no back reference in the glossary.

  2. With an additional option, like \gls[showloc]{}, the linked glossary item is displayed in the text and a back reference to the page in the glossary.

Additional explanation: I would like to use acronyms and glossary entries very often during a long document. This will result in a long location list in the glossary. Unfortunately, a user will have no glue which back reference has the most interesting information.

MWE (works with pdflatex, too):

\documentclass{scrartcl}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage[toc, xindy]{glossaries}
\makeglossaries

\newglossaryentry{ex}{name={sample},description={an example}}
\newglossaryentry{oe}{name={{\"o}l},description={{\"O}l}}

\newacronym{svm}{SVM}{support vector machine}

\begin{document}

\gls{ex} liegt im \gls{oe}.

First use: \gls{svm}. Second use: \gls{svm}.

\Blindtext[2][3]

\gls{svm}

% Task: do not display the backlink to page 2 on svm
\printglossaries

\end{document}

Edit: This question is similar to this thread, but not the same. I would like to use the functionality of \gls{} and omit only the page location.

Dirk
  • 2,728
  • May I ask you to provide a minimal working example? – Peter Ebelsberger May 25 '15 at 10:08
  • Sure! Sorry for missing it. – Dirk May 25 '15 at 10:18
  • seems to me, that I have some problem calling lualatex on my miktex system. I have to fix this first. Maybe someone else will try to help you much faster. – Peter Ebelsberger May 25 '15 at 10:42
  • It works with pdflatex, too. – Dirk May 25 '15 at 11:02
  • I found \glshyperlink{}. Unfortunately, it does not work like \gls{} e.g. on the first use of an acronym. – Dirk May 26 '15 at 11:38
  • If I understand Is it possible to display specific locations in the glossary? correctly... Any chance your instances of a redefined command such as \gls[yes]{term} that would require a hyper-linked page reference vs \gls[no]{term} could correspond with two distinct types of glossary tables? i.e. definitions that have linked page usages, vs abbreviations, nomenclature, or symbols that don't because there would be too many? If this is the case you can easily modify custom display styles to include or exclude the page numbers... Happy to help, so if I misunderstand, please expand. – EngBIRD Jun 10 '15 at 03:09
  • As far as I know, it is not possible to display specific locations. The option nonumberlist hides all page numbers. Otherwise, it would be possible to define your solution. – Dirk Jun 11 '15 at 05:52
  • A real good solution would require to define a new glossary style together with some deep insights into Xindy stuff -- I find xindy rules quite complicated. –  Jun 16 '15 at 20:54
  • What a pitty that it is so hard to suppress page numbers, or add them at specific positions when using nonumberlist. – Dirk Jun 17 '15 at 08:17

2 Answers2

5

This is only a partial solution, by redefining the \gls command, transforming it to be \glslink effectively and changing the display format to \glsignore, if the (new!) option showloc is false (or unset).

However, this will not work with \Gls etc. so far and it displays an undesireful - due to the location classes from xindy.

I've found no way to get around this. As said, only a partial solution.

\documentclass{scrartcl}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage{etoolbox}
\usepackage{letltxmacro}
\usepackage{xparse}

\usepackage[toc,xindy]{glossaries}


\makeatletter
\LetLtxMacro\defaultgls\gls

\newif\ifglsaddshowloc
\glsaddshowlocfalse

\define@boolkey{gls}{showloc}[true]{%
  \ifKV@gls@showloc
  \glsaddshowloctrue%
  \else
  \glsaddshowlocfalse%
  \fi
}%

\define@boolkey{glslink}{showloc}[true]{%
  \ifKV@gls@showloc
  \glsaddshowloctrue%
  \else
  \glsaddshowlocfalse%
  \fi
}%


\RenewDocumentCommand{\gls}{somO{}}{%
  \IfBooleanTF{#1}{%
    \IfValueTF{#2}{%
      \defaultgls*[#2]{#3}%
    }{%
      \defaultgls*{#3}%
    }%
  }{%
    \IfValueTF{#2}{%
      \setkeys{gls,glslink}{#2}
      \ifglsaddshowloc
      \glslink[#2]{#3}{\glsentryname{#3}}%
      \glsaddshowlocfalse
      \else
      \glslink[format=glsignore,#2]{#3}{\glsentryname{#3}}%
      \fi
    }{%
      \glslink[format=glsignore]{#3}{\glsentryname{#3}}%
    }%  
  }%
}
\makeatother

\makeglossaries

\newglossaryentry{ex}{name={sample},description={an example}}
\newglossaryentry{oe}{name={{\"o}l},description={{\"O}l}}
\newacronym{svm}{SVM}{support vector machine}

\begin{document}

\gls[showloc]{ex} liegt im \gls[showloc]{oe}.

First use: \gls[showloc]{svm}. 

\Blindtext Second use: \gls{svm}.

\Blindtext[2][3]

\gls{svm} 

\blindtext[10]
\gls[showloc=false]{oe}



% Task: do not display the backlink to page 2 on svm
\printglossaries

\end{document}
  • In my opinion, a separate command \mygls would be better than using \gls for this –  Jun 16 '15 at 09:37
  • Unfortunately, the separate command will have all constraints of the redefined \gls, haven't it? – Dirk Jun 16 '15 at 12:27
  • @Dirk Yes, that is right. My redefined command breaks other features, considering this, it would be better to use a wrapper and leave \gls unchanged –  Jun 16 '15 at 16:04
  • I agree with you. My request should extend the feature of \gls not break it. – Dirk Jun 17 '15 at 08:15
  • @Dirk: I think you should write a mail to Nicola Talbot -- She's the author of glossaries, perhaps she's got a clue ;-) –  Jun 17 '15 at 20:54
4

Version 4.16 of glossaries now has a hook \glswriteentry that's used to determine whether or not to performing the indexing for commands like \gls. The default definition is:

\newcommand*{\glswriteentry}[2]{%
  \ifglsindexonlyfirst
    \ifglsused{#1}{}{#2}%
  \else
    #2%
  \fi
}

The first argument is the entry's label and the second argument is the code that actually performs the indexing, so the default definition checks the indexonlyfirst package option and only does #2 on first use if indexonlyfirst=true.

If you simply want to switch off the indexing at the start of the glossaries, you can redefine \glswriteentry to do nothing just before \printglossaries:

\renewcommand*{\glswriteentry}[2]{}

You can also use this mechanisms for more complex situations, such as first use only indexing for a particular glossary type:

\renewcommand*{\glswriteentry}[2]{%
 \ifthenelse{\equal{\glsentrytype{#1}}{acronym}}
 {\ifglsused{#1}{}{#2}}%
 {#2}%
}

Alternatively, you could adapt the sample-chap-hyperfirst.tex example so that it also only performs the indexing once per chapter.

If you want to add a new key, as in the other answer, you can test for the key's setting within \glswriteentry. For example:

\define@boolkey{glslink}{index}[true]{}
\KV@glslink@indexfalse

\renewcommand*{\glswriteentry}[2]{\ifKV@glslink@index #2\fi}

(This would need to go in a package or be placed between \makeatletter and \makeatother.) Since the default value is set to false, no indexing will occur unless you explicitly switch it on using \gls[index]{label} (or similar).

Note that \glswriteentry isn't used by \glsadd (or \glsaddall) since the whole purpose of \glsadd is to add indexing information.

Update:

The glossaries-extra package, which extends the glossaries package provides the noindex key (the negation of the above index key). It also provides a method of applying indexonlyfirst to particular entry categories.

Note that glossaries-extra provides a new (more flexible) mechanism for abbreviations. If you want to continue using \newacronym, you'll need to set the style using:

\setabbreviationstyle[acronym]{long-short}

Alternatively, replace \newacronym with \newabbreviation.

For example, apply indexonlyfirst to just acronyms:

\documentclass{scrartcl}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage[xindy]{glossaries-extra}
\makeglossaries

\glssetcategoryattribute{acronym}{indexonlyfirst}{true}

\newglossaryentry{ex}{name={sample},description={an example}}
\newglossaryentry{oe}{name={{\"o}l},description={{\"O}l}}

\setabbreviationstyle[acronym]{long-short}
\newacronym{svm}{SVM}{support vector machine}

\begin{document}

\gls{ex} liegt im \gls{oe}.

First use: \gls{svm}. Second use: \gls{svm}.

\Blindtext[2][3]

\gls{svm}

\printglossaries

\end{document}

Alternatively, switching from \newacronym to \newabbreviation:

\documentclass{scrartcl}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage[xindy]{glossaries-extra}
\makeglossaries

\glssetcategoryattribute{abbreviation}{indexonlyfirst}{true}

\newglossaryentry{ex}{name={sample},description={an example}}
\newglossaryentry{oe}{name={{\"o}l},description={{\"O}l}}

\newabbreviation{svm}{SVM}{support vector machine}

\begin{document}

\gls{ex} liegt im \gls{oe}.

First use: \gls{svm}. Second use: \gls{svm}.

\Blindtext[2][3]

\gls{svm}

\printglossaries

\end{document}

The indexing can be suppressed for a particular entry using \gls[noindex]{svm}. Instead of using indexonlyfirst, you can set noindex=true as the default:

\GlsXtrSetDefaultGlsOpts{noindex}

This means that now \gls etc won't automatically index the entry unless this new default is explicitly overridden in the optional argument, for example \gls[noindex=false]{svm}. This is useful if the first use isn't the most important use. Since it's a bit cumbersome to write [noindex=false], you can set up a shortcut using \GlsXtrSetAltModifier. This allows you to set up a third modifier (in addition to * and +) for commands like \gls. For example:

\GlsXtrSetAltModifier{>}{noindex=false}

This means that \gls>{svm} is now equivalent to \gls[noindex=false]{svm}. You can use another character if you prefer, but it may only be a single character (and you need to be careful of changing category codes).

The following indexes the first use of oe and svm but the second use of ex:

\documentclass{scrartcl}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage[xindy]{glossaries-extra}
\makeglossaries

\GlsXtrSetDefaultGlsOpts{noindex}
\GlsXtrSetAltModifier{>}{noindex=false}

\newglossaryentry{ex}{name={sample},description={an example}}
\newglossaryentry{oe}{name={{\"o}l},description={{\"O}l}}

\newabbreviation{svm}{SVM}{support vector machine}

\begin{document}

\gls{ex} liegt im \gls>{oe}.

First use: \gls>{svm}. Second use: \gls{svm}.

\Blindtext[2][3]

\gls{svm}, \gls>{ex}.

\printglossaries

\end{document}
Nicola Talbot
  • 41,153
  • I tried to implement your solution with the additional boolean index. After I used the option: \gls[index]{label} all following \gls{label} are shown in the glossary, too. I assumed they second call is not shown in the index. Can you reproduce this behaviour? Should I open a new question with a MWE? – Dirk Sep 29 '15 at 12:44
  • It works with this line: \renewcommand*{\glswriteentry}[2]{\ifKV@glslink@index #2\KV@glslink@indexfalse\fi} – Dirk Sep 29 '15 at 12:46