Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Similiar to acronym package: don't write acronym if single occurence i'd like to avoid listing acronyms that i only used once with the glossaries package. The answer to this question Ignoring an acronym that is only used once contains code which detects one time use. Can i alter this code snippet to only print the acronym description without putting an entry+pagenumber into the glossary?

Acronyms

Sample:

\documentclass[8pt]{scrartcl}

\usepackage[xindy,style=long,numberline,savewrites=true,acronym,nomain]{glossaries}
\makeglossaries

\newacronym{seqse}{SE}{spin echo}
\newacronym{seqffe}{FFE}{fast field echo}
\newacronym{mri}{MRI}{magnetic resonance imaging}


\begin{document}


\printglossaries

\begin{itemize}
  \item Multiple use: \gls{seqse}, \gls{seqse}, \gls{seqse}. 
  \item Multiple use: \gls{seqffe}, \gls{seqffe}.
  \item One-time use: \gls{mri}.
\end{itemize}

\end{document}
share|improve this question
    
I just found within the documentary that using e.g. \glsentrydesc{#2} (\glsentryname{#2}) will not add a reference to the glossary. Yet, when using this in the hack of tex.stackexchange.com/questions/26238/… to overwrite \gls{} a note in the aux file will precede thereby still putting a link to the glossary :/. –  bioslime Feb 17 '13 at 9:53

2 Answers 2

up vote 4 down vote accepted

Yes, this is possible. My first attempt was to define a new style that suppresses the output of items that are used only once (see the definition of mylong). But this fails when the style is "grouped", i.e., when there are group headings, or skips between the groups, as then a suppressed item that is the only item in a group leads to a superfluous group head.

So we have to completely suppress that items that are used only once are written to the acn file. To achieve this, we hack into the output routine \@do@wrglossary, and let it write only items that are used at least twice. After than that, we use the code from this answer.

\documentclass[8pt]{scrartcl}

\usepackage{etoolbox}
\usepackage[style=long,numberline,savewrites=true,acronym,nomain]{glossaries}


\makeatletter
%% code for \glo@LABEL@usedonlyonce from http://tex.stackexchange.com/a/26263/21591
\appto\newacronymhook{%
  \newbool{glo@\the\glslabeltok @usedonlyonce}% define an additional switch per acronym
}

\patchcmd{\@gls@}{%
    \glsunset{#2}%
  }{% write appropriate information to the main auxiliary file
    \ifglsused{#2}{%
      \write\@auxout{\global\setbool{glo@#2@usedonlyonce}{false}}%
    }{%
      \write\@auxout{\global\setbool{glo@#2@usedonlyonce}{true}}%
    }%
    \glsunset{#2}%
  }{}{\message{^^JPatching failed (1)^^J}}

\patchcmd{\@gls@}{%
    \glsentryfirst{#2}%
  }{% print the long form of the acronym if the acronym is used only once
    \ifbool{glo@#2@usedonlyonce}{\glsentrylong{#2}}{\glsentryfirst{#2}}%
  }{}{\message{^^JPatching failed (2)^^J}}

\let\old@do@wrglossary\@do@wrglossary
\renewcommand{\@do@wrglossary}[1]{\ifbool{glo@#1@usedonlyonce}{}{\old@do@wrglossary{#1}}}

% \newglossarystyle{mylong}{%
%   \glossarystyle{long}%
%   \renewcommand*{\glossaryentryfield}[5]{%
%     \ifbool{glo@##1@usedonlyonce}{}{%
%       \glsentryitem{##1}\glstarget{##1}{##2} & ##3\glspostdescription\space ##5\\%
%     }}%
%   \renewcommand*{\glsgroupskip}{}%
% }
\makeatother

\makeglossaries

\newacronym{seqse}{SE}{spin echo}
\newacronym{seqffe}{FFE}{fast field echo}
\newacronym{mri}{MRI}{magnetic resonance imaging}
\newacronym{goo}{GOO}{bar}


\begin{document}

% \printglossary[type=\acronymtype,style=mylong]
\printglossaries

\begin{itemize}
  \item Multiple use: \gls{seqse}, \gls{seqse}, \gls{seqse}.
  \item Multiple use: \gls{seqffe}, \gls{seqffe}.
  \item One-time use: \gls{mri}.
  \item Multiple use: \gls{goo}, \gls{goo}.
\end{itemize}

\end{document}

example output

Note that you have to run pdflatex twice before creating the index with xindy/makeindex. BTW, this would be a nice feature request for the glossaries package (@NicolaTalbot).

share|improve this answer

Upgrade to glossaries version 4.14 (I've only just uploaded it to CTAN, so you may need to wait a few days before it reaches the TeX distributions). This has a new command \glsenableentrycount which enables two extra fields for each entry: currcount and prevcount. The currcount field keeps track of how many times the entry has been used so far. (By "used", that means how many times \glsunset has been used for that entry, so that means used by the \gls-like commands, but not by other commands, such as \glsadd, \glstext or \glsentrytext. The value is reset to zero by \glsreset.) The prevcount field stores the final value of the currcount field as it was at the end of the previous LaTeX run.

In addition, \glsenableentrycount also enables the commands \cgls, \cglspl, \cGls and \cGlspl which are like \gls, \glspl, \Gls and \Glspl but they test if the value of prevcount is 1. If it is, then they just use commands like \glsentrylong or \glsentryfirst (followed by \glsunset). if it isn't, they behave like their \gls etc counterparts.

So your example can be modified as follows:

% arara: pdflatex
% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{scrartcl}

\usepackage[xindy,style=long,numberline,savewrites=true,acronym,nomain]{glossaries}
\makeglossaries

\glsenableentrycount % enable \cgls, \cglspl, \cGls, \cGlspl

\newacronym{seqse}{SE}{spin echo}
\newacronym{seqffe}{FFE}{fast field echo}
\newacronym{mri}{MRI}{magnetic resonance imaging}


\begin{document}


\printglossaries

\begin{itemize}
  \item Multiple use: \cgls{seqse}, \cgls{seqse}, \cgls{seqse}. 
  \item Multiple use: \cgls{seqffe}, \cgls{seqffe}.
  \item One-time use: \cgls{mri}.
\end{itemize}

\end{document}

(I've added arara directives as a reminder of the document build process, but you can ignore them if you don't use arara.) If \cgls seems like too much typing, remember you can define synonyms. For example:

\let\ac\cgls
\let\acpl\cglspl
\let\Ac\cGls
\let\Acpl\cGlspl

The result looks like:

Image of resulting document. MRI entry not included in list of acronyms.

Notes:

  1. This method will naturally take longer to build your document (since it requires an extra LaTeX run and has to do more stuff when using an entry).
  2. This method changes \newglossaryentry (and therefore \newacronym) into preamble-only commands to ensure the entries are defined before the .aux file is read.
  3. If you use other commands, such as \glsadd or \glstext the single-use entry will be added to the list of acronyms.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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