避免词汇表包中的双点

避免词汇表包中的双点

我想采纳 Herbert 建议的解决方案,可以在这里,在某些词汇表记录中。

请考虑以下示例:

\documentclass[a4paper]{book}

\usepackage{glossaries}
\makeglossaries
\makeatletter
    \newcommand{\fs}{\@ifnextchar.{}{.}}% test of next token 
\makeatother

\newglossaryentry{pg}{
    name={pg.},
    description={pg.},
    first={pg.},
    firstplural={pp.},
    plural={pp.}
}
\newglossaryentry{fig}{
    name={fig.},
    description={fig.},
    first={fig.},
    firstplural={figs.},
    plural={figs.}
}


\begin{document}
    as can be seen in the \glspl{fig} on the following \gls{pg}.
\end{document}

排版没有问题,但是会在句子末尾创建一个双点。

我想到的一个解决方案是尝试将定义的宏放入\fs词汇表记录中,但是返回了错误,即以下导致上述内容无法编译:

\newglossaryentry{pg}{
    name={pg\fs},
    description={pg\fs},
    first={pg\fs},
    firstplural={pp\fs},
    plural={pp\fs}
}

另一种方法可能是修改\gls{...}和类似的命令,检查评估的记录是否以“。”结尾,如果是,则用宏替换\fs,但是词汇表包中有许多命令,不确定如何以这种方式执行。

如何实现这一点?我使用词汇表作为一种手段,在整个文档范围内托管许多这样的缩写。

答案1

这需要添加一个以上级别\gls和类似的命令。可以重新定义它们,而不是使用新命令,这留作练习。

\documentclass[a4paper]{book}

\usepackage{glossaries,amsthm}
\makeglossaries

\makeatletter
% define a non space skipping version of \@ifnextchar
\newcommand\fussy@ifnextchar[3]{%
  \let\reserved@d=#1%
  \def\reserved@a{#2}%
  \def\reserved@b{#3}%
  \futurelet\@let@token\fussy@ifnch}
\def\fussy@ifnch{%
  \ifx\@let@token\reserved@d
    \let\reserved@c\reserved@a 
  \else
    \let\reserved@c\reserved@b
  \fi
 \reserved@c}

\DeclareRobustCommand{\Gls}[1]{%
  \gls{#1}\fussy@ifnextchar.{\@checkperiod}{\@}}
\DeclareRobustCommand{\Glspl}[1]{%
  \glspl{#1}\fussy@ifnextchar.{\@checkperiod}{\@}}

\newcommand{\@checkperiod}[1]{%
  \ifnum\sfcode`\.=\spacefactor\else#1\fi
}
\makeatother

\newglossaryentry{box}{
  name=box,
  description=box,
  plural=boxes,
}

\newglossaryentry{pg}{
  name={pg.},
  description={pg.},
  first={pg.},
  firstplural={pp.},
  plural={pp.}
}
\newglossaryentry{fig}{
  name={fig.},
  description={fig.},
  first={fig.},
  firstplural={figs.},
  plural={figs.}
}


\begin{document}
As can be seen in the \Glspl{fig} on the following \Gls{pg}.

As can be seen in \Glspl{box} or in \Gls{box}.

As can be seen in the \Glspl{fig}, on the following \Gls{pg}, we have commas.

As can be seen in \Glspl{box}, or in \Gls{box}, we have commas.

\end{document}

请注意,这amsthm很重要;实际上需要的是如何重新定义,\frenchspacing以便由句点而不是通用的 1000 设置唯一的空间因子代码。

在此处输入图片描述

答案2

添加\fs定义不会起作用,因为命令生成的文本可能\gls非常深地嵌入内部工作中。至少,它将随后取消设置第一次使用标志。有两种替代方案:

方法 1:

跟踪哪些条目带有点(例如在标签中包含点以提醒自己),并且不要添加句子点。例如:

\documentclass[a4paper]{book}

\usepackage{glossaries}
\makeglossaries

\newglossaryentry{pg.}{
    name={pg.},
    description={pg.},
    first={pg.},
    firstplural={pp.},
    plural={pp.}
}
\newglossaryentry{fig.}{
    name={fig.},
    description={fig.},
    first={fig.},
    firstplural={figs.},
    plural={figs.}
}

\begin{document}
    as can be seen in the \glspl{fig.} on the following \gls{pg.}
This sentence refers to the \gls{fig.}['s] caption.
\end{document}

结果:

结果图像

方法 2:

不要在定义中添加点。而是定义一个将在需要的地方添加点的命令。例如:

\documentclass[a4paper]{book}

\usepackage{glossaries}
\makeglossaries

\makeatletter

  \newcommand{\abbrev}[2][]{%
    \new@ifnextchar[%
     {\@abbrev{#1}{#2}}% get the final optional argument
     {%
        \new@ifnextchar.%
        {\@gls@{#1}{#2}[]}% end of sentence found
        {\@gls@{#1}{#2}[.\spacefactor=1000]}% not end of sentence, add abbreviation dot
     }%
  }

  \def\@abbrev#1#2[#3]{%
    \new@ifnextchar.%
    {\@gls@{#1}{#2}[#3]}% end of sentence found
    {\@gls@{#1}{#2}[.#3]}% not end of sentence, add abbreviation dot
  }

  % Similar code for plural:

  \newcommand{\abbrevpl}[2][]{%
    \new@ifnextchar[%
     {\@abbrevpl{#1}{#2}}% get the final optional argument
     {%
        \new@ifnextchar.%
        {\@glspl@{#1}{#2}[]}% end of sentence found
        {\@glspl@{#1}{#2}[.\spacefactor=1000]}% not end of sentence, add abbreviation dot
     }%
  }

  \def\@abbrevpl#1#2[#3]{%
    \new@ifnextchar.%
    {\@glspl@{#1}{#2}[#3]}% end of sentence found
    {\@glspl@{#1}{#2}[.#3]}% not end of sentence, add abbreviation dot
  }

  % Do likewise for any other required variation

\makeatother

\newglossaryentry{pg}{
    name={pg.},
    text={pg},
    description={pg},
    first={pg},
    firstplural={pp},
    plural={pp}
}
\newglossaryentry{fig}{
    name={fig.},
    text={fig},
    description={fig},
    first={fig},
    firstplural={figs},
    plural={figs}
}

\begin{document}
    as can be seen in the \abbrevpl{fig} on the following \abbrev{pg}.
This sentence refers to the \abbrev{fig}['s] caption.
\end{document}

得出的结果为:

结果图像

第二种方法正确调整了空间因子。

相关内容