3

I want to redefine the glossaries package’s \gls commands to include an index entry and an optional argument for formatting the index entry. However, there are so many variants of this command (\gls, \glspl, \glstext, etc. as well as many of my own defined keys), that I want to loop this command through a comma separated list. I am trying to use etoolbox and its \docsvlist command to accomplish this. My MWE is below:

\documentclass{report}

\usepackage{glossaries}
\usepackage{imakeidx}
\usepackage{etoolbox}
\usepackage{xargs}

\renewcommand{\do}[1]{%
    \expandafter\newcommand%
        \csname oldgls#1\endcsname%
        \csname gls#1\endcsname%
    \expandafter\renewcommandx%
        \csname gls#1\endcsname%
        [2][2]%
        \csname oldgls#1\endcsname{##1}\index{##1##2}%
}
\docsvlist{pl,text}

\makeglossaries
\makeindex

\input{glossaryDefs} % A file defining myterm & anotherterm




\begin{document}

Some text \glspl{myterm}[|textbf] and some more text  \glstext{anotherterm}.

\printglossary
\printindex

\end{document}

However, I get the error Missing \begin{document}. I am pretty new to LaTeX and I suspect that there are probably a number of issues in this example. Any help would be appreciated.

2
  • So you want to store the old macros and redefine them to also add \index. What's with the optional parameter given by 2?
    – Werner
    Jan 28, 2015 at 4:31
  • I hinted at the answer to this question in my MWE. The optional argument is supposed to customize the index entry. For example, \glspl{myterm}[|textbf] should be equivalent to \oldglspl{myterm}\index{myterm|textbf}.
    – cbward90
    Jan 28, 2015 at 15:16

2 Answers 2

5

When you call \do{pl}, here's what happens:

\expandafter\newcommand\csname oldglspl\endcsname\csname glspl\endcsname
\expandafter\renewcommandx\csname glspl\endcsname[2][2]%
  \csname oldglspl\endcsname{#1}\index{#1#2}

The first \expandafter expands \csname, so we get

\newcommand\oldglspl\csname glspl\endcsname
\expandafter\renewcommandx\csname glspl\endcsname[2][2]%
  \csname oldglspl\endcsname{#1}\index{#1#2}

and the consequence is a long string of errors, because this is just like doing

\newcommand\oldglspl{\csname}

which is certainly not what you want. For this application you need \let, not \newcommand.

Try with

\renewcommand{\do}[1]{%
  \csletcs{oldgls#1}{gls#1}%
  \expandafter\renewcommandx\csname gls#1\endcsname[2][2]{%
    \csname oldgls#1\endcsname{##1}\index{##1##2}%
  }%
}

The first instruction will do \let\oldglspl\glspl, so saving the meaning for usage in the redefinition of \oldgspl.

Note that \csletcs is provided by etoolbox; without it you should do

\expandafter\let\csname oldgls#1\expandafter\endcsname\csname gls#1\endcsname
0
0

You need to move \begin{document} to immediately after \makeindex. However I could not do a complete compile as you did not provide the glossaryDefs.tex file. MWE issue.

2
  • \makeglossaries should definitely be before \begin{document}.
    – egreg
    Jan 28, 2015 at 9:59
  • Fixed that, I should not answer questions after midnight as I confuse before and after:( Jan 28, 2015 at 19:10

You must log in to answer this question.

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