0

How to capitalize the first letter for EACH word in a glossary using the glossaries-extra package?

I came across this thread but it uses the glossaries package.

I also tried \glsentrytitlecase{<label>}{long} but it didn't

  1. make the glossary hyperlinked
  2. include the glossary in the parentheses at the end, e.g., (ESs) in the MWE below

Basically, what I want is similar to \Gls but make capitalize the first letter for EACH word instead of the first word only.

MWE:

\documentclass[a4paper,12pt]{article}

\usepackage[acronym]{glossaries-extra}
\setabbreviationstyle[acronym]{long}
\newacronym
[
  description={Evolutionary Strategies},
  longplural={evolutionary strategies}
]
{es}{ES}{evolutionary strategy}
\makeglossaries


\begin{document}
\printglossary[type=acronym,title={List of Acronyms}]

\glsentrytitlecase{es}{long}.

\glspl{es}.

\Glspl{es}.

\glsentrytitlecase{es}{longpl}.

\end{document}

Update:

\documentclass[a4paper,12pt]{article}

\usepackage[acronym,automake]{glossaries-extra}

\makeatletter
\newrobustcmd*{\myGls}{\@gls@hyp@opt\@myGls}
\newcommand*{\@myGls}[2][]{%
  \new@ifnextchar[{\@myGls@{#1}{#2}}{\@myGls@{#1}{#2}[]}%
}
\def\@myGls@#1#2[#3]{%
  \glsdoifexists{#2}%
  {%
    \let\do@gls@link@checkfirsthyper\@gls@link@checkfirsthyper
    \let\glsifplural\@secondoftwo
    \let\glscapscase\@secondofthree
    \def\glscustomtext{%
    {%
    \ecapitalisewords{\glsentrylong{#2}}#3\space%
    \firstacronymfont(\glsentryshort{#2})}%
    }
    \def\glsinsert{#3}%
    \def\@glo@text{\csname gls@\glstype @entryfmt\endcsname}%
    \@gls@link[#1]{#2}{\@glo@text}%
    \ifKV@glslink@local
      \glslocalunset{#2}%
    \else
      \glsunset{#2}%
    \fi
  }%
  \glspostlinkhook
}

\newrobustcmd*{\myGlspl}{\@gls@hyp@opt\@myGlspl}
\newcommand*{\@myGlspl}[2][]{%
  \new@ifnextchar[{\@myGlspl@{#1}{#2}}{\@myGlspl@{#1}{#2}[]}%
}
\def\@myGlspl@#1#2[#3]{%
  \glsdoifexists{#2}%
  {%
    \let\do@gls@link@checkfirsthyper\@gls@link@checkfirsthyper
    \let\glsifplural\@firstoftwo
    \let\glscapscase\@secondofthree
    \def\glscustomtext{%
    {%
    \ecapitalisewords{\glsentrylongpl{#2}}#3\space%
    \firstacronymfont(\glsentryshortpl{#2})}%
    }
    \def\glsinsert{#3}%
    \def\@glo@text{\csname gls@\glstype @entryfmt\endcsname}%
    \@gls@link[#1]{#2}{\@glo@text}%
    \ifKV@glslink@local
      \glslocalunset{#2}%
    \else
      \glsunset{#2}%
    \fi
  }%
  \glspostlinkhook
}
\makeatother

\setabbreviationstyle[acronym]{long}

\newacronym
[
    description=Evolutionary Strategy,
    longplural=evolutionary strategies
]
{es}{ES}{evolutionary strategy}
\makeglossaries


\begin{document}
\printglossary[type=acronym,title={List of Acronyms}]

textbf{Singular:}

blablbla \gls{es}.

blablbla \glsxtrfull{es}.

blablbla \Gls{es}.

blablbla \Glsxtrfull{es}.

blablbla \myGls{es}.

blablbla \glsentrytitlecase{es}{long}.


\vspace*{1cm}


\textbf{Plural:}

blablbla \glspl{es}.

blablbla \glsxtrfullpl{es}.

blablbla \Glspl{es}.

blablbla \Glsxtrfullpl{es}.

blablbla \myGlspl{es}.

blablbla \glsentrytitlecase{es}{longpl}.

\end{document}
2
  • The glossaries-extra package is just an extension to the glossaries package. The basic commands will be same for both packages. The thread you mentioned is working in your case.
    – Tom
    May 7, 2022 at 23:38
  • @Tom Are you sure? If I change the line \usepackage{glossaries} to \usepackage{glossaries-extra}, it doesn't produce the same result... May 8, 2022 at 7:23

1 Answer 1

1

This is the solution. Notice the comments in the code. If you wanna the second use to display the short form, uncomment those comments. Otherwise it will always display the full form. Add \myGlspl to deal with plural form.

\documentclass[a4paper,12pt]{article}
\usepackage[hidelinks]{hyperref}
\usepackage[acronym,automake]{glossaries-extra}

\makeatletter
\newrobustcmd*{\myGls}{\@gls@hyp@opt\@myGls}
\newcommand*{\@myGls}[2][]{%
  \new@ifnextchar[{\@myGls@{#1}{#2}}{\@myGls@{#1}{#2}[]}%
}
\def\@myGls@#1#2[#3]{%
  \glsdoifexists{#2}%
  {%
    \let\do@gls@link@checkfirsthyper\@gls@link@checkfirsthyper
    \let\glsifplural\@secondoftwo
    \let\glscapscase\@secondofthree
    \def\glscustomtext{%
    \ifglsused{#2}
    {\acronymfont\glsentryshort{#2}#3}
    {%
    \ecapitalisewords{\glsentrylong{#2}}#3\space%
    \firstacronymfont(\glsentryshort{#2})}%
    }%
    \def\glsinsert{#3}%
    \def\@glo@text{\csname gls@\glstype @entryfmt\endcsname}%
    \@gls@link[#1]{#2}{\@glo@text}%
    \ifKV@glslink@local
      \glslocalunset{#2}%
    \else
      \glsunset{#2}%
    \fi
  }%
  \glspostlinkhook
}

\newrobustcmd*{\myGlspl}{\@gls@hyp@opt\@myGlspl}
\newcommand*{\@myGlspl}[2][]{%
  \new@ifnextchar[{\@myGlspl@{#1}{#2}}{\@myGlspl@{#1}{#2}[]}%
}
\def\@myGlspl@#1#2[#3]{%
  \glsdoifexists{#2}%
  {%
    \let\do@gls@link@checkfirsthyper\@gls@link@checkfirsthyper
    \let\glsifplural\@firstoftwo
    \let\glscapscase\@secondofthree
    \def\glscustomtext{%
    \ifglsused{#2}%
    {\acronymfont\glsentryshortpl{#2}#3}
    {%
    \ecapitalisewords{\glsentrylongpl{#2}}#3\space%
    \firstacronymfont(\glsentryshortpl{#2})}%
    }%
    \def\glsinsert{#3}%
    \def\@glo@text{\csname gls@\glstype @entryfmt\endcsname}%
    \@gls@link[#1]{#2}{\@glo@text}%
    \ifKV@glslink@local
      \glslocalunset{#2}%
    \else
      \glsunset{#2}%
    \fi
  }%
  \glspostlinkhook
}
\makeatother

\setabbreviationstyle[acronym]{long-short}

\newacronym{tla}{TLA}{three lettered acronym}
\makeglossaries

\begin{document}

\printglossary[type=acronym,title={List of Acronyms}]

\myGls{tla}

\myGlspl{tla}

\Gls{tla}
\end{document} 

enter image description here

12
  • Is there any way to keep the result of \Gls as it is (only the first letter of the first word is capitalized)? May 8, 2022 at 21:58
  • 1
    Do you wanna a new command did similar thing as \Gls but capitalized each words?
    – Tom
    May 9, 2022 at 0:24
  • Yes. and I would like to keep the functionality of \Gls intact. With the code you provided, the result of \Gls also capitalizes the first letter of EACH word instead of only the FIRST word as it is. May 9, 2022 at 6:28
  • 1
    @hmhuang I found a way to do that. I have a question for you. Do you wanna the second use of the command to display the short form or it doesn't matter. See the new edits in my answer.
    – Tom
    May 9, 2022 at 21:16
  • 1
    If you complete copy my code it won’t have any space in front. Notice every %in the code is necessary to prevent extra space happen
    – Tom
    May 11, 2022 at 8:36

You must log in to answer this question.

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