梅威瑟:
\documentclass[11pt,a4paper]{article}
\usepackage{xcolor}
\usepackage{soul}
\usepackage[acronym,toc]{glossaries}
\newacronym{a}{A}{The one and only A}
\newcommand{\myCmd}[3][]{%
\ifthenelse{\equal{#2}{}}%
{}%
{\texthl{#2}}%
\ifthenelse{\equal{#1}{}}{%
Test #3 %
}{%
Other Test #1 #3%
}%
}%
\begin{document}
\begin{itemize}
\item works: \gls{a}
\item works: \myCmd{1}{2}
\item works: \myCmd[3]{1}{2}
\item does not work: \myCmd[3]{\gls{a}}{2}
\end{itemize}
\end{document}
我知道这myCmd
看起来很奇怪,但这只是因为我为 MWE 简化了很多。
无论如何,MWE 会导致这个错误:
! Package glossaries Error: Glossary entry `{a}' has not been defined.
我唯一已经想到的是,该gls
命令没有找到参数,因为它寻找的是{a}
而不是a
——但是为什么呢?
答案1
这是一个解决方法,使用可扩展的命令\glsentryname
(\glsentrydesc
有关更多信息,请参阅glossaries-user.pdf
文档的第 9 节)。
为了平息soul
和“扩大”论点,一个\mbox{}
是适当的。然而,长描述不会超过行数。
\documentclass[11pt,a4paper]{article}
\usepackage{xcolor}
\usepackage{soul}
\usepackage{etoolbox}
\usepackage[acronym,toc,nomain]{glossaries}
\newcommand{\glsinfo}[1]{%
\glsentryname{#1} -- \glsentrydesc{#1}%
}
\makeglossaries
\newacronym{a}{A}{The one and only A}
\newcommand{\myCmd}[3][]{%
\ifblank{#2}{}{\texthl{\mbox{#2}}}%
\ifblank{#1}{Test #3}{Other Test #1 #3}%
}
\begin{document}
\begin{itemize}
\item works: \gls{a}
\item works: \myCmd{1}{2}
\item works: \myCmd[3]{1}{2}
\item works: \myCmd[3]{\glsinfo{a}}{2}
\item works: \myCmd[3]{\glsentrydesc{a}}{2}
\item works: \myCmd[3]{}{2}
\end{itemize}
%\printglossaries
\end{document}