生成词汇表术语的宏以提高源的可读性

生成词汇表术语的宏以提高源的可读性

使用 glossaries-extra,我想为每个词汇表术语生成宏,以提高 latex 源的可读性。我发现很难阅读如下所示的文本(尤其是较长的文本):

\Glspl{pig} or \gls{pig}? \Gls{cat} or \glspl{cat}?

让我分心的是,大写字母和数字在命令中,而不是在术语中。理想情况下,该行应为:

\Pigs or \pig? \Cat or \cats?

然而,这并不奏效,因为它会导致下一个空格出现问题。为了解决这个问题我的首选是\Pigs/

可以使用\edef部分来<code>生成这种形式的宏。通过在标签后\glsforeachincategory{<category-list>}{<category>}{<label>}{<code>}附加一个,可以轻松创建复数和单数的宏。s

仍存在两个问题:

  • 根据条款,可能会与现有命令名称发生冲突。解决方案应该对视觉效果和字符数的影响最小。我目前的解决方案是将其添加x到标签前面,产生\xPigs/,但我并不满意,因为可能仍存在冲突,可读性可能会更好。添加_( \_Pigs/) 或.( \.Pigs/) 可读性会更好,但不起作用。有什么建议吗?

  • 术语的大写是我根本无法实现的。我当前的解决方案是将前缀的 大写x,即\Xpigs/。因此问题是:当参数扩展为 时,如何生成名为\xPigs/through 的宏(见下文) ?\defGlspl#1pig

也欢迎更多一般性评论或替代方法!

\documentclass{article}
\usepackage[automake]{glossaries-extra}

% two glossary entries, pig and cat, for our example.
\newglossaryentry{pig}
{
    name={pig},
    description={A pig is any of the animals in the genus Sus.},
    category={animals}
}

\newglossaryentry{cat}
{
    name= {cat},
    description= {The cat (Felis catus) is a small carnivorous mammal.},
    category={animals},
}

\makeglossaries

% macros defining macros that 
% * expand to a glossary command (e.g. \gls)
% * do not inhibit readability (regarding capitalization and singular/plural))
\def\defgls#1{\expandafter\edef\csname x#1\endcsname/{\gls{#1}}}
\def\defGls#1{\expandafter\edef\csname X#1\endcsname/{\Gls{#1}}}
\def\defglspl#1{\expandafter\edef\csname x#1s\endcsname/{\glspl{#1}}}
\def\defGlspl#1{\expandafter\edef\csname X#1s\endcsname/{\Glspl{#1}}}

% apply the macros for all glossary terms in a category
\glsforeachincategory{animals}{\cat}{\lab}{%
    \defgls\lab%
    \defGls\lab%
    \defglspl\lab%
    \defGlspl\lab%
}

%%% My question %%%
% how to change \defGls#1 and \defGlspl#1 above 
% to generate the macros below that, for the demo, 
% I wrote by hand (for the pig and cat entry)
% (the difference is the capitalization of the macro name)
\def\xPigs/{\Glspl{pig}}
\def\xPig/{\Gls{pig}}
\def\xCats/{\Glspl{cat}}
\def\xCat/{\Gls{cat}}

\begin{document}

% demo

% I find this hard to read and scan for errors
\Glspl{pig} or \gls{pig}? \Gls{cat} or \glspl{cat}?

% Better:
\Xpigs/ or \xpig/? \Xcat/ or \xcats/?

% what I would like:
%% note the difference 
%% above:\Xpigs/ below: \xPigs/
%% above:\Xcat/, below:\xCat/)
\xPigs/ or \xpig/? \xCat/ or \xcats/?

\def\Pigs/{Pigs}
\def\pig/{pig}
\def\Cat/{Cat}
\def\cats/{cats}
%ideal, but unrealistic because of possible name-clashes:
\Pigs/ or \pig/? \Cat/ or \cats/?

\printglossaries

\end{document}

答案1

主要问题已经解决,使用技巧埃格尔的回答这个问题

这个技巧应用于宏\myUppermyUpperAux下面的代码中 - 请参阅那里的注释以获取详细解释。

使用此代码,可以\xGlossaryTerms/在通常需要使用的地方进行书写\Glspl{glossaryTerm}

\documentclass{article}
\usepackage[automake]{glossaries-extra}

% two glossary entries, pig and cat, for our example.
\newglossaryentry{pig}
{
    name={pig},
    description={A pig is any of the animals in the genus Sus.},
    category={animals}
}

\newglossaryentry{cat}
{
    name= {cat},
    description= {The cat (Felis catus) is a small carnivorous mammal.},
    category={animals},
}

\makeglossaries

% solution based on the accepted answer of 
% https://tex.stackexchange.com/questions/57551/create-a-capitalized-macro-token-using-csname
\def\myUpperAux#1#2#3\relax{%
    % the \def here is required because \uppercase will replace tokens with 
    % their uppercase form, but will not touch control sequences.
    % With the trick from the stackexchange answer:
    % #1 is the first token of the input
    % #2 is the second token of the input
    % #3 is the rest of the input
    % we define a temporary macro for the value of #1 so it doesn't get uppercased
    % we uppercase the edef with csname
    \def\tmpMyUpperAux{#1}%
    \uppercase{\expandafter\edef\csname \tmpMyUpperAux#2}#3\endcsname/%
}
\newcommand\myUpperDef[1]{%
    % \edef here forces expansion of #1. We need it as a sequence of tokens instead of 
    % whatever control sequences it actually is because the the macro \myUpperAux 
    % wants to split off the first two tokens and uppercase the second
    \edef\myUpperDefTmp{#1}%
    \expandafter\myUpperAux\myUpperDefTmp\relax%
}


% macros defining macros that 
% * expand to a glossary command (e.g. \gls)
% * do not inhibit readability (regarding capitalization and singular/plural))
\def\defgls#1{\expandafter\edef\csname x#1\endcsname/{\gls{#1}}}
\def\defGls#1{\myUpperDef{x#1}{\Gls{#1}}}
\def\defglspl#1{\expandafter\edef\csname x#1s\endcsname/{\glspl{#1}}}
\def\defGlspl#1{\myUpperDef{x#1s}{\Glspl{#1}}}

% apply the macros for all glossary terms in a category
\glsforeachincategory{animals}{\cat}{\lab}{%
    \defgls{\lab}%
    \defGls{\lab}%
    \defglspl{\lab}%
    \defGlspl{\lab}%
}

\begin{document}

% demo

% I find this hard to read and scan for errors
\Glspl{pig} or \gls{pig}? \Gls{cat} or \glspl{cat}?

% here is the desired (but not ideal) solution:
\xPigs/ or \xpig/? \xCat/ or \xcats/?

\def\Pigs/{Pigs}
\def\pig/{pig}
\def\Cat/{Cat}
\def\cats/{cats}
% ideal, but unrealistic because of possible name-clashes:
\Pigs/ or \pig/? \Cat/ or \cats/?
% ... still interested in a better way to avoid name clashes with minimal 
% impact on source readability at the same time.

\printglossaries

\end{document}

相关内容