標題幹擾詞彙表首次/後續使用機制

標題幹擾詞彙表首次/後續使用機制

這是一個後續問題圖表清單中的術語表溢出,其中有人詢問如何glossaries將包的第一個使用方案與圖形標題中的術語表一起使用。我給了一個只要caption包未加載就有效的答案。

\documentclass{article}

\usepackage{caption}
\usepackage[colorlinks]{hyperref}
\usepackage{glossaries}

\makeglossaries
\newglossaryentry{acr}{%
  name        = {ACR-name},%
  description = {ACR-description},%
  first       = {ACR-first-description},%
}

\renewcommand{\glsdisplayfirst}[4]{#1 (1) #4}
\renewcommand{\glsdisplay}[4]{#1 (2+) #4}
\begin{document}
% \listoffigures %% even without \listoffigures the problem shows up

\begin{figure}[h]
  \caption[Short title \glshyperlink{acr}]{Long title \gls{acr}}
\end{figure}

\printglossary
\end{document}

可以看出,如果沒有caption圖形標題,則包含所需的詞彙表條目的第一個使用形式,但有了caption它,則包含後續使用形式。

我怎樣才能在使用時保持它的工作caption

答案1

LaTeX 將標題放入臨時刮痕內\hbox,測量其寬度。如果線寬小於線寬,盒子被重複用於排版標題。因此\gls被稱為一次。但是,如果標題寬度超過行寬,則標題文字將設定兩次,這次作為段落。然後\gls被調用兩次,您將獲得詞彙表項目第二個版本的輸出。

caption使行為更加可預測。如果singlelinecheck設定了選項(預設),則標題文字始終設定兩次,第一次用於測量寬度以檢查文字是否適合一行,然後再次設定文字以獲得最終結果。使用 時singlelinecheck=false,測量將被停用,且文字僅設定一次,但標題文字的居中位置會遺失。

解決方法是將結果放入\gls{acr}臨時框中並在其中使用該框\caption

\documentclass{article}

\usepackage{caption}
\usepackage{glossaries}

\newsavebox\glsscratchbox

\makeglossaries
\newglossaryentry{acr}{%
  name        = {ACR-name},%
  description = {ACR-description},%
  first       = {ACR-first-description},%
}

\renewcommand{\glsdisplayfirst}[4]{#1 (1) #4}
\renewcommand{\glsdisplay}[4]{#1 (2+) #4}
\begin{document}
\listoffigures %% even without \listoffigures the problem shows up

\begin{figure}[h]
  \sbox\glsscratchbox{\gls{acr}}
  \caption[Short title \glshyperlink{acr}]{Long title \unhcopy\glsscratchbox}
\end{figure}

\printglossary
\end{document}

(使用該命令\unhcopy代替\usebox(這是一個\copy)。它會剝離外層\hbox並允許以與行中其他空格相同的方式排版空格。否則 ( \usebox) 重用框內的空格將始終具有自然寬度.)

結果

解決方案\gls在標題與包caption

更新:新增了 Axel Sommerfeldt 評論的簡化。

包包glossaries必須記住首字母縮寫的第一次用法。我們的想法是,如果測量了標題的寬度,則停用此功能,但如果標題文字最終排版,則不停用此功能。

需要包caption,因為它將測試與最終的排版步驟分開。它還提供了\caption@prepareslc測量之前的宏,並在測量之前調用。暫時禁用\glsunset測量。

\documentclass{article}

\usepackage{glossaries}
\usepackage{caption}

\makeatletter
\g@addto@macro\caption@prepareslc{%
  \let\glsunset\@gobble
}
\makeatother

\makeglossaries
\newglossaryentry{acr}{%
  name        = {ACR-name},%
  description = {ACR-description},%
  first       = {ACR-first-description},%
}

\renewcommand{\glsdisplayfirst}[4]{#1 (1) #4}
\renewcommand{\glsdisplay}[4]{#1 (2+) #4}
\begin{document}
\listoffigures

\begin{figure}[h]
  \caption[Short title \glshyperlink{acr}]{Long title \gls{acr}}
\end{figure}

\printglossary
\end{document}

細化

如果\gls{acr}使用,則為縮寫詞設定全域標誌acr以記住縮寫詞使用的狀態。因此後續調用可以使用較短的形式。如果僅測量標題文本,則先前的解決方案會跳過狀態設定。因此,當最終設定標題文字時,狀態並未改變。

然而,有一種情況會被忽視。標題文字\gls多次包含相同的縮寫詞,包括其第一次使用。那麼先前的解決方案將包含首字母縮略詞在測量步驟中的使用的首字母縮略詞的第一個使用形式,因為那裡的狀態設定已被停用。

這是透過允許測量步驟中縮寫詞的正常狀態變化來解決的,但記住第一次用法。在字幕文字最終排版之前會進行重置。

進一步說明:

  • \if@capmeasure引入了一個開關。通常設定為\iffalse。當在內部檢查文字的寬度時\caption,則將其設為\iftrue
  • 需要包caption,因為它將測試與最終的排版步驟分開。它還提供了\caption@prepareslc測量之前的巨集。兩者都在同一個本地組中執行。因此添加\@capmeasurefalse到就足夠了\caption@prepareslc。分組結束後開關自動重設。
  • 測量後,第一個縮寫詞使用的狀態被重置。
  • 軟體包glossaries'\glsunset已進行修補,以尊重首字母縮略詞的設定\if@capmeasure並記住首字母縮略詞的用法。

完整範例:

\documentclass{article}

\usepackage{glossaries}
\usepackage{caption}

\makeatletter
\newif\if@capmeasure
\g@addto@macro\caption@prepareslc{%
  \global\let\after@capmeasure\@empty
  \aftergroup\after@capmeasure
  \@capmeasuretrue
}
\CheckCommand*{\glsunset}[1]{%
  \glsdoifexists{#1}{%
    \expandafter \global \csname glo@#1@flagtrue\endcsname   
  }%
}
\renewcommand*{\glsunset}[1]{%
  \glsdoifexists{#1}{%
    \if@capmeasure
      \expandafter\ifx\csname ifglo@#1@flag\expandafter\endcsname
      \csname iftrue\endcsname
      \else 
        % first use
        \g@addto@macro\after@capmeasure{\glsreset{#1}}%
      \fi
    \fi
    \global\csname glo@#1@flagtrue\endcsname
  }%
}
\makeatother

\makeglossaries
\newglossaryentry{acr}{%
  name        = {ACR-name},%
  description = {ACR-description},%
  first       = {ACR-first-long-description},%
}

\renewcommand{\glsdisplayfirst}[4]{#1\textsuperscript{(1)}#4}
\renewcommand{\glsdisplay}[4]{#1\textsuperscript{(2+)}#4}
\begin{document}
\listoffigures

\begin{figure}[h]
  \caption[Short title \glshyperlink{acr}]%
  {Long title \textit{\gls{acr}} and \gls{acr}}
\end{figure}

\printglossary
\end{document}

細化結果

答案2

使用glossaries-extra你有兩種選擇。看這裡

  1. 您隨時可以\glsxtrshort在字幕內通話。由於顯而易見的原因,這些不會觸發首次使用。如果您還想避免連結並且不希望這些條目出現在使用 建立的術語表列表中,您也可以使用noindex和選項。這讓我們想到:hyper=false\printglossaries
  2. 預測此用法,glossaries-extra包括命令系列\glsfmtshort。這些與相應的命令相同\glsxtrshort[noindex,hyper=false]。因此,您有幾個類似的命令,涵蓋單數和複數形式、短、長和完整形式,以及小寫、首字母大寫和完整大寫形式。

相關內容