# Use a quality glossary and acronym list in Lyx

The question How to combine Acronym and Glossary shows us how to make a great glossaries and acronym lists. I would like to use a quality glossary and acronym list in Lyx.

This may be too large a question to ask all at once. I have broken a possible first step, combining use of nomenclature and glossary. I am trying to use two packages together:

1. nomenclature to render the glossary and acronym list in Lyx
2. glossaries to use the full name the first time it is used in the text and the acronym after.

My attempt is shown below:

%% LyX 1.6.7 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{nomencl}
% the following is useful when we have the old nomencl.sty package
\providecommand{\printnomenclature}{\printglossary}
\providecommand{\makenomenclature}{\makeglossary}
\makenomenclature

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{hyperref}
\usepackage{glossaries}
\usepackage{nomencl}
\makeglossary
\makenomenclature
\RequirePackage{ifthen}
\renewcommand{\nomgroup}[1]{
\ifthenelse{\equal{#1}{A}}
{\item[\textbf{Acronym}]}
{ \ifthenelse{\equal{#1}{G}}
{\item[\textbf{Glossary}]}{}}
}

\makeatother

\usepackage{babel}

\begin{document}
\newglossaryentry{api}{%
type=\acronymtype,
name={API},
description={Application Programming Interface},
first={Application Programming Interface (API)},
see=[Glossary:]{apig}
}

%%% The glossary entry the acronym links to

\newglossaryentry{apig}{%
name={API}, %
description={An Application Programming Interface (API) is a particular set of rules %
and specifications that a software program can follow to access and make %
use of the services and resources provided by another particular software %
program that implements that API%
}}

\nomenclature[a]{API}{Application Programming Interface}

\nomenclature[g]{API}{%
An Application Programming Interface (API) is a particular set of rules %
and specifications that a software program can follow to access and make %
use of the services and resources provided by another particular software %
program that implements that API%
}

\printnomenclature{}

main text body

first use \gls{api}

subsequent use of \gls{api}
\end{document}


Now how can I create a macro to automatically use all the glossary entries in nomenclature to render the printed glossary. Right now I am typing them twice, as you can see. Bonus points if you can get rid of the "Nomenclature" heading and make the Acronym and Glossary headings larger. Extra special bonus points if you can somehow get the links to work properly, for example, between an acronym and its corresponding glossary entry. I will check if the solutions provided render in Lyx.

-
This may be useful: wiki.lyx.org/Tips/Indexing#toc4 –  denilw Mar 26 '11 at 21:42
Did you find the solution? –  Lukasz R. Dec 18 '11 at 0:06
texblog.org/2012/05/14/… –  Nicholas Hamilton Dec 13 '12 at 10:20
I don't get it. The question you linked to shows how to make an acronym list and glossary with the glossaries package. What else do you want? Why do you want the nomencl package on top of that? And what has all this to do with Lyx? –  mafp Feb 22 '13 at 10:49
@mafp I agree with your first part that it is not clear why the OP wants to use package "nomenscl". But the OP has tried to integrate this kind of glossary into LyX. It seems that the OP does not know how to proceed in LyX. (Just as a side note: LyX is not a LaTeX editor but a document processor that can generate LaTeX code.) –  e-birk Feb 23 '13 at 11:49

I don't know what this has to do with Lyx, so I ignore that. Second, it might be helpful to know what larger problem you want(-ed) to solve, as the question you linked to seems to achieve the same thing, just without using nomencl. It might be possible that the still developed glossaries package is suitable to solve the larger problem at hand.

Having said that, let's start.

• To make the group headings larger, we insert a \Large into the place where you made them bold already:

\renewcommand{\nomgroup}[1]{%
\ifthenelse{\equal{#1}{A}}%
{\item[\textbf{\Large Acronym}]}%
{ \ifthenelse{\equal{#1}{G}}%
{\item[\textbf{\Large Glossary}]}%
{}%
}%
}

• To get rid of the "Nomenclature", we patch \thenomenclature using the etoolbox package:

\patchcmd{\thenomenclature}{%
\section*{\nomname}
}{}{}{\message{^^JPatching of thenomenclature failed^^J}}


Note that this will also remove the Nomenclature entry from the Table of Contents, as we don't have a sectioning command anymore. If you are using a chaptered document like book, the sections above need to be replaced by chapter.

• To save retyping \thenomenclature entries, we patch the \@gls@ macro that does all the work when \gls{api}, \gls*{api} is called for the first time for an entry. We add the two \nomenclature calls:

\patchcmd{\@gls@}{%
\glsunset{#2}%
}{%
\ifglsused{#2}{}{%
\nomenclature[a]{\glsentryname{#2}}{\glsentrydesc{#2}}%
\nomenclature[g]{\glsentryname{#2g}}{\glsentrydesc{#2g}}%
}%
\glsunset{#2}%
}{}{\message{^^JPatching of gls failed^^J}}

• To get the hyperlinking right, the glossary entry in the nomenclature must become a hypertarget, while the acronym entry in the nomenclature should get a link to this target. The target will be created by using

\nomenclature[g]{\hypertarget{glo:#2}{\glsentryname{#2g}}}{\glsentrydesc{#2g}}%


in the above code.

• Creating the link in the acronym entry is harder: one might be tempted to put that directly in the second argument of \nomenclature call, but this is eaten by makeindex. Instead, we save label and text of that link into two variables:

\nomenclature[a]{\hyperlink{glo:#2}{\glsentryname{#2}}}{


that can be used afterwards. To put the link between glossary description and page references, we use

\pretocmd{\nompageref}{%
\relax%
\else%
\fi%
}{}{\message{^^JAppending to nompageref failed^^J}}


To put the link after possible page references, we use

\renewcommand{\nomentryend}{%
\relax%
\else%
\fi%
}


Result:

Complete MWE:

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

%% the following is useful when we have the old nomencl.sty package
% \usepackage{nomencl}
% \providecommand{\printnomenclature}{\printglossary}
% \providecommand{\makenomenclature}{\makeglossary}
% \makenomenclature

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{kantlipsum}
\usepackage{babel}
\usepackage{ifthen}
\usepackage[refpage]{nomencl}
% \usepackage{nomencl}
\usepackage{glossaries}
\usepackage{etoolbox}

\makenomenclature

\makeatletter

%% Write name and description to the glossary of the nomenclature.
%% This assumes that only the acronyms are referenced in the text, i.e.,
%% you only use \gls{api}, not \gls{apig}.
%% The glossaries package makes hyperlinks from \gls{<label>} as glo:<label>, so we make
%% corresponding hypertargets at the nomencl--glossary entry.

%% Define the storage here, otherwise hyperref complains.

\patchcmd{\@gls@}{%
\glsunset{#2}%
}{%
ifglsused{#2}{}{%
%% acronym entry:
%       \nomenclature[a]{\glsentryname{#2}}{
%% or with another link at the acronym
%% does not work:
%% glossary entry:
\nomenclature[g]{\hypertarget{glo:#2}{\glsentryname{#2g}}}{\glsentrydesc{#2g}}%
}%
\glsunset{#2}%
}{}{\message{^^JPatching of gls failed^^J}}

%% At the end of acronym entries (those that define \glolinklabel and \glolinktext), put
%% a link to the corresponding glossary entry after possible page references.
% \renewcommand{\nomentryend}{%
%       \relax%
%     \else%
%     \fi%
%   }

%% At the end of acronym entries (those that define \glolinklabel and \glolinktext), put
%% a link to the corresponding glossary entry before possible page references.
\pretocmd{\nompageref}{%
\relax%
\else%
\fi%
}{}{\message{^^JAppending to nompageref failed^^J}}
\makeatother

%% To get rid of the Nomenclature section title, but this still adds a \section*{}
% \renewcommand{\nomname}{}
%% So remove the section, too. But then we should not write to the ToC, either.
\makeatletter
\patchcmd{\thenomenclature}{%
\section*{\nomname}
}{}{}{\message{^^JPatching of thenomenclature failed^^J}}
\makeatother

\renewcommand{\nomgroup}[1]{%
\ifthenelse{\equal{#1}{A}}%
{\item[\textbf{\Large Acronym}]}%
{ \ifthenelse{\equal{#1}{G}}%
{\item[\textbf{\Large Glossary}]}%
{}%
}%
}

%% glossary entries
\newglossaryentry{api}{%
type=\acronymtype,%
name={API},%
description={Application Programming Interface},%
first={Application Programming Interface (API)},%
% see=[Glossary:]{apig}%
}

%% The glossary entry the acronym links to
\newglossaryentry{apig}{%
name={API},%
description={An Application Programming Interface (API) is a particular set of rules %
and specifications that a software program can follow to access and make %
use of the services and resources provided by another particular software %
program that implements that API%
}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\printnomenclature

main text body
first use \gls{api}

\kant[1]

subsequent use of \gls{api}

\kant[2-3]

another use of \gls*{api}

\kant[4-5]

another use of \gls{api}
\end{document}


It may be cleaner to define only one glossaries entry to prevent accidental usage of the glossary entry apig. It also makes the code more robust, as we do not depend on the naming scheme api -> apig. This can be achieved by using the user1 field for the long description. The only things that need change are the glossary entry generation:

\nomenclature[g]{\hypertarget{glo:#2}{\glsentryname{#2g}}}{\glsentryuseri{#2}}%


(Spot the usage of \glsentryuseri.) The single entry would look like this:

\newglossaryentry{api}{%
type=\acronymtype,%
name={API},%
description={Application Programming Interface},%
first={Application Programming Interface (API)},%
user1={An Application Programming Interface (API) is a particular set of rules %
and specifications that a software program can follow to access and make %
use of the services and resources provided by another particular software %
program that implements that API},%
% see=[Glossary:]{apig}%
}

-
There is no statement how to create the auxiliary files .gls, .glg ... manually (knowing that makeglossaries creates them in LaTeX is not enough since LyX keeps the auxiliary files in a temporary directory - how to find this?) nor there is a description on how to configure LyX to automate the process. Please update. –  e-birk Mar 6 '13 at 0:31
Even in LaTeX, the MWE is not printing the glossary or acronym list since \makeglossaries is missing. –  e-birk Mar 6 '13 at 0:32
@e-birk The OP asked how to render the glossary and acronym list with nomenclature (see point 1). The OP also wants to use glossaries only to get the first use/subsequent use scheme working (see point 2). That's why my solution renders the nomenclature of the nomencl package, and not the glossary of the glossaries package. \makeglossaries is not needed. Did you even try it? –  mafp Mar 6 '13 at 8:21
Indeed I tried... –  e-birk Mar 6 '13 at 16:48
@e-birk So you were able to reproduce the output I gave in my answer? –  mafp Mar 6 '13 at 16:50

The following steps will lead to a glossary and an acronym list in LyX. When using this approach, "nomenclature" and "nomenclature entries" in LyX cannot be used.

## Setup

1. In the LaTeX Preamble ("Document > Setting...") we add:

\usepackage[acronym]{glossaries}
\makeglossaries

2. The generating the index files for the glossaries package uses different options than for the nomencl package. The following wrapper script needs to be used. saved in the home directory .

• for linux, save the file as mkgloss.sh in your home directory and make the script executable by chmod u+x mkgloss.sh

#!/bin/sh
makeglossaries basename "\$1" .glo

• for windows, (caution: not tested!) save the file as mkgloss.bat in your home directory

REM for loop used for formatting purpose only
FOR /F %i in ("%1") do makeglossaries %~nI

3. We tell LyX to use this script instead of the default makeindex -s nomencl.ist by changing the field "Nomenclature command" in "Tools > Preferences... > Output > LaTeX". We use the filename with absolute path. Depending on where you have saved the file, for example, for linux,

"/home/tux/mkgloss.sh"


or for windows,

"C:\Documents and Settings\Peter\mkgloss.bat"


## Usage

Now, we can write acroyms and a glossary entries in the LyX document by using TeX code/ERT (Ctrl-L). For example,

\newglossaryentry{api}{type=\acronymtype, name={API}, description={Application
Programming Interface}, first={Application Programming Interface (API)},
see=[Glossary:]{apig}}

\newglossaryentry{apig}{name={API},
description={An Application Programming Interface (API) is a particular set
of rules and specifications that a software program can follow to access and
make use of the services and resources provided by another particular software
program that implements that API}}


In the LyX document \gls{api} as TeX code/ERT will use the glossary entry api and \glsadd{apig} will add the entry apig to the glossary without generating text.

Finally, we write the two pieces of TeX code \printglossary[type=\acronymtype] and \printglossary[type=main] at the end of the document (or whereever the acronym list and glossary should appear). Now the PDF displays both.

A working example can be found here (on that site save the text as ".lyx" file). Removing all page breaks from the example we get:

Problem: A change of an acronym entry seems to be missed by LyX and the acronym list in the output (PDF) is not updated.

\newglossaryentry{dummy}{name={DUMMY},
description={Type or erase a letter here and LyX will update the glossary and
acronym list (do *not* use space as only working under certain circumstances)}}


By changing the description of this entry LyX will get reminded to update glossary and acronym list.

-

The trick is to use as much of the built in nomencl support from LyX as possible. I have done this by borrowing from the Biblatex in LyX wiki entry.

First we create a document with a nomenclature in LyX.

Insert > List / TOC > Nomenclature


Since we do not want LyX to print the nomenclature, 'capture' this element in a LyX comment (yellow box).

Next we edit the document preamble.

Document > Settings > LaTeX Preamble


% call the glossaries package
\usepackage{glossaries}

% activate the glossaries building
\makeglossaries

% redefine the command that LyX puts out, to the glossaries input
\let\nomenclature\gls

\newglossaryentry{linux}{name={linux},description={an open-source unix implementation}}


Save the document settings.

Now add nomenclature entries as you would normally do in LyX (except that you don't add the work you are defining, since glossaries will input it for you). When adding the (nomenclature) entries, please do not give a description (enter a space, so that you can close the window), or it will also appear in your document. If you view the code (View > Source), it should look something like this:

\nomenclature{linux}{ }


Now compile the document. The glossary will not appear and nor will the entries in your document. For this you first need to run makeglossaries filename in the LyX temp folder. If you are on linux, this folder is located in the

/tmp/SomeLyxTempFolder


If you are on windows it is something like

Users > username > Appdata > Local > Temp > someLyxTempFolder


Note that there must be a better way to reuse the LyX nomencl output as a glossaries input, e.g. using \renewcommand or something (especially using only the first argument of \nomenclature) but I cannot figure this out.

-