
我想獲得以下行為:如果首字母縮略詞僅在節/章/部分中使用一次,則在這種情況下僅應使用長名稱。然而,如果首字母縮略詞被使用多個,我想要這種long-short
行為。
我有以下非工作 MWE:
\documentclass{book}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\fpcompare}{ m m m }
{
% #1 = test to perform
% #2 = text for the true case
% #3 = text for the false case
\fp_compare:nTF { #1 } { #2 } { #3 }
}
\ExplSyntaxOff
\usepackage[acronym,symbols,nomain,toc,sanitizesort,nogroupskip,xindy,numberedsection=autolabel]{glossaries-extra}
\setglossarystyle{super}
\setabbreviationstyle[acronym]{long-short}
\GlsXtrEnableEntryUnitCounting{acronym}{1}{section}
%\renewcommand*{\gls}{\cgls}%
%\renewcommand*{\Gls}{\cGls}%
%\renewcommand*{\glspl}{\cglspl}%
%\renewcommand*{\Glspl}{\cGlspl}%
%\renewcommand*{\GLS}{\cGLS}%
%\renewcommand*{\GLSpl}{\cGLSpl}%
\makenoidxglossaries
\newacronym{wn}{WN}{wireless network}
\glsxtrnewsymbol[description={pi},type=symbols]{symb:pi}{\ensuremath{\pi}}%
\usepackage{xstring}
\renewcommand*{\gls}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%to apply this to acronym only (and not symbols as well)
\fpcompare{\glsentrycurrcount{#1} > 1}{\glsentrydesc{#1}}{\cgls{#1}}% fpcompare to detect how often used
}{%
\cgls{#1}%
}%
}%
\renewcommand*{\Gls}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\Glsentrydesc{#1}}{\cgls{#1}}%
}{%
\cgls{#1}%
}%
}%
\renewcommand*{\glspl}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\glsentryplural{#1}}{\cglspl{#1}}%
}{%
\cglspl{#1}%
}%
}%
\renewcommand*{\Glspl}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\Glsentryplural{#1}}{\cglspl{#1}}%
}{%
\cglspl{#1}%
}%
}%
\renewcommand*{\GLS}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\GLSentrydesc{#1}}{\cgls{#1}}%
}{%
\cgls{#1}%
}%
}%
\renewcommand*{\GLSpl}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\GLSentryplural{#1}}{\cglspl{#1}}%
}{%
\cglspl{#1}%
}%
}%
\begin{document}
\section{A}
\gls{wn}\\
\glspl{wn}\\
\glspl{wn}\\
\glspl{wn}\\
count: \glsentrycurrcount{wn}\\
\gls{symb:pi}
\section{B}
\glspl{wn}\\
count: \glsentrycurrcount{wn}
\section{C}
\Glspl{wn}\\
\gls{wn}\\
count: \glsentrycurrcount{wn}
\end{document}
其產生:
然而,我本來期望/希望 A 部分和 C 部分的計數為 4,首先列印完整的描述而不是WNs
如何讓這項工作可靠呢?
答案1
% arara: pdflatex
% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage{glossaries-extra}
\makeglossaries
\GlsXtrEnableEntryCounting
{abbreviation}% list of categories to use entry counting
{2}% trigger value
\newabbreviation{html}{HTML}{hypertext markup language}
\newabbreviation{xml}{XML}{extensible markup language}
\newabbreviation{css}{CSS}{cascading style sheet}
\newglossaryentry{sample}{name={sample},description={sample}}
\begin{document}
This is a sample document that uses entry counting. The entry counting
has been enabled on the \texttt{abbreviation} category.
This means that abbreviations will only be added to the glossary
if they have been used more than $n$ times, where in this
document $n$ has been set to
\glsgetcategoryattribute{abbreviation}{entrycount}.
Entries in other categories behave as normal.
Used once: \gls{html}.
Used twice: \gls{xml} and \gls{xml}.
Used three times: \gls{css} and \gls{css} and \gls{css}.
Used once but this entry is in the ``general'' category
which doesn't have the ``entrycount'' attribute set:
\gls{sample}.
\printglossaries
\end{document}
若要自動重設計數,例如章節,請使用此程式碼(從手冊複製)。注意\GlsXtrEnableEntryUnitCounting
命令。
\documentclass{report}
\usepackage{glossaries-extra}
\GlsXtrEnableEntryUnitCounting{abbreviation}{2}{chapter}
\makeglossaries
\newabbreviation{html}{HTML}{hypertext markup language}
\newabbreviation{css}{CSS}{cascading style sheet}
\newglossaryentry{sample}{name={sample},description={sample}}
\begin{document}
\chapter{Sample}
Used once: \gls{html}.
Used three times: \gls{css} and \gls{css} and \gls{css}.
Used once: \gls{sample}.
\chapter{Another Sample}
Used once: \gls{css}.
Used twice: \gls{html} and \gls{html}.
\printglossaries
\end{document}
為了完整起見,我只是將其貼在這裡,請在其他線程中對答案進行投票。
答案2
您可以透過為縮寫指定特定於部分的名稱來使其成為特定於部分的首字母縮寫。當然,您可以輕鬆地自動化此操作:
\documentclass{book}
\usepackage[xindy,style=long,numberline,savewrites=true,acronym,nomain]{glossaries}
\usepackage{pgffor}
\makeglossaries
\glsenableentrycount
%create a separate acrony for each section
\let\oldnewacronym\newacronym \renewcommand{\newacronym}[3]{\foreach \n in {0,...,10}{\oldnewacronym{c\n-#1}{#2}{#3}}}
%%patch all the commands to use the within-section version of the acronym
\let\oldcgls\cgls \renewcommand{\cgls}[1]{\oldcgls{s\arabic{section}-#1}}
\let\oldcglspl\cglspl \renewcommand{\cglspl}[1]{\oldcglspl{s\arabic{section}-#1}}
\let\oldcGlspl\cGlspl \renewcommand{\cGlspl}[1]{\oldcGlspl{s\arabic{section}-#1}}
\newacronym{wn}{WN}{wireless network}
\begin{document}
\section{A}
\cgls{wn}\\
\cglspl{wn}\\
\cglspl{wn}\\
\cglspl{wn}\\
count: \glsentrycurrcount{s1-wn}\\
\section{B}
\cglspl{wn}\\
count: \glsentrycurrcount{s2-wn}
\section{C}
\cGlspl{wn}\\
\cgls{wn}\\
count: \glsentrycurrcount{s3-wn}
\end{document}
wn
基本上,當定義首字母縮略詞時,它會為每個部分創建單獨的首字母縮略詞,並命名它們s1-wn
等等。現在,當在 X 部分中呼叫縮寫詞時,它會「重定向」並使用sX-wn
.結果:
當然,這還需要一些更多的調整:
- 到目前為止,它僅適用於章節,不適用於章節,但這應該很簡單
- 它透過硬連線最多可工作 10 個部分。您可以增加該數字,引入新的計數器,或執行一些 .aux-file-magic 自動執行此操作,以適用於任意數量的章節/節/小節
- 它還需要一些進一步的調整來處理符號等。
- 到目前為止我還沒有嘗試過
glossaries-extra
,因為我還沒有安裝它。 - 我現在不知道您希望縮寫列表如何顯示。所以現在我的建議忽略了這一點,就像你的問題一樣。
學分:我的解決方案的一部分取自這裡