如何按数字检索单词列表中的项目?

如何按数字检索单词列表中的项目?

我正在为 Fate RPG 制作一个角色表,该角色表使用与形容词等级相对应的数字等级来评估其技能。我一直在尝试编写一个宏,以\skillAdj{4}比使用这些条件更优雅的方式将其转换为“Great”。

理想情况下,我能够从两个方向进行查找,但我更关心从数字到形容词。

\usepackage{calc}
\usepackage{ifthen}
%
\newcounter{skillcounter}
\set{skillcounter}{0}
%
\newcommand[1]{\skillAdj}{\ifthenelse{\equal{#1}{8}
Legendary
}
{\ifthenelse{\equal{#1}{7}
Epic
}
{\ifthenelse{\equal{#1}{6}
Fantastic
}
{\ifthenelse{\equal{#1}{5}
Superb
}
{\ifthenelse{\equal{#1}{4}
Great
}
{\ifthenelse{\equal{#1}{3}
Legendary
}
{\ifthenelse{\equal{#1}{2}
Legendary
}
{\ifthenelse{\equal{#1}{1}
Legendary
}
{\ifthenelse{\equal{#1}{0}
Mediocre
}
{\ifthenelse{\equal{#1}{-1}
Poor
}
{\ifthenelse{\equal{#1}{-2}
Terrible
}
{\ifthenelse{\equal{#1}{-3}
Awful
}
{\ifthenelse{\equal{#1}{-4}
Abysmal
}
{Error}}}}}}}}}}}}}

这就是规模。

+8 传奇
+7 史诗
+6梦幻
+5 极好
+4 很好
+3 良好
+2
一般 +1 平均
+0 平庸
-1 较差
-2 糟糕
-3 糟糕透顶
-4 极其糟糕

我非常感谢大家提供的任何有用建议。我还没有在任何软件包的文档中找到任何似乎可以提供此功能的内容。

答案1

有几种方法可以满足您的需求。以下是其中一种expl3

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\skillAdj}{m}
 {
  \int_case:nnn { #1 }
   {
     {8}{Legendary}
     {7}{Epic}
     {6}{Fantastic}
     {5}{Superb}
     {4}{Great}
     {3}{Good}
     {2}{Fair}
     {1}{Average}
     {0}{Mediocre}
    {-1}{Poor}
    {-2}{Terrible}
    {-3}{Awful}
    {-4}{Abysmal}
   }
   {Error}
 }
\ExplSyntaxOff

\begin{document}

X is \skillAdj{8}

Y is \skillAdj{5}

Z is \skillAdj{-4}

\texttt{egreg} is \skillAdj{10}

Come on! How can it be?

\end{document}

在此处输入图片描述

\skillAdj{\value{skillcounter}}如果您愿意并且已经定义了计数器,您也可以说。


请注意,您的定义中存在一些问题:

\set{skillcounter}{0}

应该

\setcounter{skillcounter}{0}

尽管

\newcommand[1]{\skillAdj}{\ifthenelse{\equal{#1}{8}...

应该

\newcommand{\skillAdj}[1]{\ifthenelse{\equal{#1}{8}...

一个非常相似的解决方案使用xstring

\documentclass{article}

\usepackage{xstring}
\newcommand{\skillAdj}[1]{%
  \IfEqCase{#1}{%
     {8}{Legendary}%
     {7}{Epic}%
     {6}{Fantastic}%
     {5}{Superb}%
     {4}{Great}%
     {3}{Legendary}%
     {2}{Legendary}%
     {1}{Legendary}%
     {0}{Mediocre}%
    {-1}{Poor}%
    {-2}{Terrible}%
    {-3}{Awful}%
    {-4}{Abysmal}%
   }[Error]
 }

\newcounter{skillcounter}

\begin{document}

\setcounter{skillcounter}{8}

X is \skillAdj{\theskillcounter}

Y is \skillAdj{5}

Z is \skillAdj{-4}

\texttt{egreg} is \skillAdj{10}

Come on! How can it be?

\end{document}

请注意,这里您必须使用,\theskillcounter因为这样\value{skillcounter}才不起作用。

答案2

在此处输入图片描述

还有一个TeX \ifcase版本,没有额外的内容ifnum

\def\skillAdj#1{%
\count255=8\advance\count255by-#1 %
\ifcase\the\count255 %
Legendary%
\or Epic%
\or Fantastic%
\or Superb%
\or Great%
\or Good%
\or Fair%
\or Average%
\or Mediocre%
\or Poor%
\or Terrible%
\or Awful%
\or Abysmal%
\else Error%
\fi%
}

\def\test#1{#1 \skillAdj{#1}}
\obeylines\tt
\test{+8}
\test{+7}
\test{+6}
\test{+5}
\test{+4}
\test{+3}
\test{+2}
\test{+1}
\test{+0}
\test{-1}
\test{-2}
\test{-3}
\test{-4}
\test{-7}
\test{+9}

\newcount\cnt\cnt5

\test{\the\cnt}

\bye

答案3

您还可以使用TeX内置\ifcase构造,​​如下所示 - 无需任何包!

截屏

% arara: pdflatex
\documentclass{article}

\newcommand{\skillAdj}[1]{%
    \ifcase\numexpr#1+4\relax
            Abysmal      % -4
            \or Awful    % -3
            \or Terrible % -2
            \or Poor     % -1
            \or Mediocre  % 0
            \or Average   % 1
            \or Fair      % 2
            \or Good      % 3
            \or Great     % 4
            \or Superb    % 5
            \or Fantastic % 6
            \or Epic      % 7
            \or Legendary % 8
    \else 
       Error
    \fi
}

\newcounter{skillcounter}

\begin{document}


\setcounter{skillcounter}{-5}

\loop
\theskillcounter: \skillAdj{\theskillcounter}
\stepcounter{skillcounter}\par
\ifnum\value{skillcounter}<10 \repeat

\end{document}

如果最小值(上面的 -4)发生变化,只需更新该行

\ifcase\numexpr#1+4\relax

因此。

答案4

以下是对PGF/TikZ:如何将字符串存储在数组中?调整适应负值(不包括元素计数,因为它可能不需要):

在此处输入图片描述

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newcounter{listtotal}\newcounter{listcntr}%
\newcommand{\skilladj}[1]{% \skilladj{<number>}
  \setcounter{listcntr}{-5}% Start from -4
  \renewcommand*{\do}[1]{\stepcounter{listcntr}\ifnum\value{listcntr}=#1\relax##1\fi}%
  \expandafter\docsvlist\expandafter{\skilladjarray}% Process list again
}
% Skill Adjustment array
\newcommand{\skilladjarray}{Abysmal,Awful,Terrible,Poor,Mediocre,Average,Fair,Good,Great,Superb,Fantastic,Epic,Legendary}%
\begin{document}
\verb|\skilladj{-2}:|\ \skilladj{-2} \par
\verb|\skilladj{2}:|\ \skilladj{2} \par
\verb|\skilladj{0}:|\ \skilladj{0} \par
\verb|\skilladj{5}:|\ \skilladj{5} \par
\verb|\skilladj{-5}:|\ \skilladj{-5} \par
\verb|\skilladj{9}:|\ \skilladj{9} \par
\verb|\skilladj{8}:|\ \skilladj{8}
\end{document}

相关内容