显示词汇表中的行号

显示词汇表中的行号

在词汇表中,我需要显示每个术语出现的“页码/行”,例如:

Glossary

  clamour
    1) plainte, cri. 24/10, 40/5
    2) requête. 50/24, 50/28

是否可以使用词汇表包(或者任何其他包)?

答案1

如果您对整理位置列表不感兴趣,可以按照以下步骤操作:

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{article}

\usepackage[pagewise]{lineno}
\usepackage{etoolbox}
\usepackage[subentrycounter]{glossaries}

\usepackage{lipsum}

\makeglossaries

\newcounter{glslabcount}

\renewcommand*{\glsentryfmt}{%
  \stepcounter{glslabcount}%
  \edef\thislabel{glslab:\arabic{glslabcount}}%
  \expandafter\linelabel\expandafter{\thislabel}%
  \ifcsundef{glsreflist.\glslabel}%
  {\csdef{glsreflist.\glslabel}{}}{}%
  \listcsxadd{glsreflist.\glslabel}{\thislabel}%
  \glsgenentryfmt
}

\newglossaryentry{clamour}{name={clamour},description={}}
\newglossaryentry{clamour1}{parent={clamour},description={plainte, cri}}
\newglossaryentry{clamour2}{parent={clamour},description={requ\^ete}}

\newcommand{\glslisthandler}[1]{%
  \glslisthandlersep
  \pageref{#1}/\ref{#1}%
  \def\glslisthandlersep{, }%
}

\newglossarystyle{pagelineno}%
{%
  \setglossarystyle{index}%
  \renewcommand*{\glossentry}[2]{%
     \item\glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}}%
     \ifcsdef{glsreflist.##1}%
     {%
       . 
       \def\glslisthandlersep{}%
       \forlistcsloop{\glslisthandler}{glsreflist.##1}%
     }{}%
  }%
  \renewcommand{\subglossentry}[3]{%
    \ifcase##1\relax
      \item
    \or
      \subitem
      \glssubentryitem{##2}%
    \else
      \subsubitem
    \fi
    \glstarget{##2}%
    \glossentrydesc{##2}.
    \ifcsdef{glsreflist.##2}%
    {%
      \def\glslisthandlersep{}%
      \forlistcsloop{\glslisthandler}{glsreflist.##2}%
    }{}%
  }%
}

\setglossarystyle{pagelineno}

\begin{document}
\linenumbers

\lipsum*[1] TEST: \gls{clamour1}
\lipsum[2-4]

% Have some entries on a page spanning paragraph:
\lipsum*[5] TEST: \gls{clamour2}
\lipsum*[6] TEST: \gls{clamour1}, \gls{clamour2}.
\lipsum[7]

\lipsum[8]

\printglossaries
\end{document}

词汇表如下:

词汇表图片

它的工作原理是每次引用条目时创建一个标签,并将标签存储在与该条目对应的列表中。当条目显示在词汇表中时,将迭代此列表。这仅用于makeindex对条目进行排序。它忽略了实际位置列表。唯一的问题是,如果您的条目在同一行上出现多次,那么您将得到重复的引用。

编辑:

对于交叉引用术语,我假设您不打算在文档中使用此术语,而只是希望它与交叉引用一起出现在词汇表中。这可以通过编辑样式以仅在以下情况下包含位置列表来实现\glsreflist.标签尚未定义:

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{article}

\usepackage[pagewise]{lineno}
\usepackage{etoolbox}
\usepackage[subentrycounter]{glossaries}

\usepackage{lipsum}

\makeglossaries

\newcounter{glslabcount}

\renewcommand*{\glsentryfmt}{%
  \stepcounter{glslabcount}%
  \edef\thislabel{glslab:\arabic{glslabcount}}%
  \expandafter\linelabel\expandafter{\thislabel}%
  \ifcsundef{glsreflist.\glslabel}%
  {\csdef{glsreflist.\glslabel}{}}{}%
  \listcsxadd{glsreflist.\glslabel}{\thislabel}%
  \glsgenentryfmt
}

\newglossaryentry{clamour}{name={clamour},description={}}
\newglossaryentry{clamour1}{parent={clamour},description={plainte,
cri}}
\newglossaryentry{clamour2}{parent={clamour},description={requ\^ete}}

\newglossaryentry{cri}{name={cri},description={bla bla},see=[voir]{clamour}}

\newcommand{\glslisthandler}[1]{%
  \glslisthandlersep
  \pageref{#1}/\ref{#1}%
  \def\glslisthandlersep{, }%
}

\newglossarystyle{pagelineno}%
{%
  \setglossarystyle{index}%
  \renewcommand*{\glossentry}[2]{%
     \item\glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}}%
     \ifcsdef{glsreflist.##1}%
     {%
       . 
       \def\glslisthandlersep{}%
       \forlistcsloop{\glslisthandler}{glsreflist.##1}%
     }%
     {, ##2}% <- add location list to ensure cross-reference is included
  }%
  \renewcommand{\subglossentry}[3]{%
    \ifcase##1\relax
      \item
    \or
      \subitem
      \glssubentryitem{##2}%
    \else
      \subsubitem
    \fi
    \glstarget{##2}%
    \glossentrydesc{##2}.
    \ifcsdef{glsreflist.##2}%
    {%
      \def\glslisthandlersep{}%
      \forlistcsloop{\glslisthandler}{glsreflist.##2}%
    }{}%
  }%
}

\setglossarystyle{pagelineno}

\begin{document}
\linenumbers

\lipsum*[1] TEST: \gls{clamour1}
\lipsum[2-4]

% Have some entries on a page spanning paragraph:
\lipsum*[5] TEST: \gls{clamour2}
\lipsum*[6] TEST: \gls{clamour1}, \gls{clamour2}.
\lipsum[7]

\lipsum[8]

\printglossaries
\end{document}

词汇表现在如下所示:

词汇表图片

答案2

是的,这是可能的。这里有一种方法。我们使用包lineno来进行行号编排。然后我们让glossaries包写入<pagenumber>.<linenumber>原始索引文件。由于在文本中排版特定词汇表术语时不知道行号,因此我们必须编写运行行计数器,并在排版词汇表时根据该计数器计算按页的行号。为此,我们使用的内置宏来glossaries剖析位置列表。这些宏存在于与一起使用hyperref,但我们强制glossaries即使没有也使用它们hyperref。在最后一步,当仅要用排版复合页码时\@gls@numberlink,我们会根据需要将页码制成链接,并计算词汇表术语的正确按页的行号。

\documentclass{article}

\usepackage{lipsum}

\usepackage[colorlinks]{hyperref}
\usepackage[pagewise]{lineno}
\usepackage[subentrycounter]{glossaries}
\usepackage{glossary-tree}
\usepackage{etoolbox}

\makeglossaries
%% number sub entries with a), b) etc.
\renewcommand*{\glssubentrycounterlabel}{\alph{glossarysubentry})\space}

\makeatletter
%% write the running line number into the source index file
\patchcmd{\@@do@wrglossary}
  {\protected@xdef\@glslocref{\theglsentrycounter}}
  {\protected@xdef\@glslocref{\theglsentrycounter.\thelinenumber}}
  {}{\message{^^JPatching failed^^J}}

%% If we feed the page list (containing \delimR ranges or \delimN enumerations)
%% to \glshypernumber (even without hyperref),
%% we end up being our composite numbers (page.line) being fed to
%% \@glshypernumber, thus to \@gls@numberlink
\renewcommand{\glsnumberformat}[1]{%
  \glshypernumber{#1}}

\def\glshypernumber#1{\@glshypernumber#1\nohyperpage{}\@nil}
%% in case hyperref is not loaded, make \hyperlink just write the link text
\providecommand{\hyperlink}[2]{#2}

\let\orig@gls@numberlink\@gls@numberlink
\renewcommand{\@gls@numberlink}[1]{%
  \myformatnumb#1\@nil}

\def\myformatnumb#1.#2\@nil{%
  \orig@gls@numberlink{#1}/\getpagewiselinenumber{#2}}

\makeatother

%% a main entry and two sub-entries
\newglossaryentry{clamour}{%
  name = {clamour},
  description = {\nopostdesc},
}
\newglossaryentry{plainte}{%
  parent = clamour,
  name = {plainte, cri},
  description = {\nopostdesc},
}
\newglossaryentry{requete}{%
  parent = clamour,
  name = {requ\^ete},
  description={\nopostdesc},
}

\pagewiselinenumbers

\begin{document}
\lipsum[1-2]
\gls{plainte}

\lipsum[3-4]
\gls{requete}

\lipsum[5-6]
\gls{requete}

\gls{requete}

\gls{requete}

\lipsum[7-8]
\gls{plainte}

\nolinenumbers%
\printglossary[style=index]
\end{document}

示例输出

相关内容