循环同义词命令

循环同义词命令

是否可以定义一个命令来循环显示预定义同义词列表?例如,为\adverb(或类似\cycle{adverb}) 命令定义一个副词列表 (however、nevertheless、nonetheless、notwithstanding),以便每次调用\adverb(或\cycle{adverb}) 命令时,都会显示不同的副词。

区分“但是”和“然而”输出会更好(例如\cycle{Adverb}\cycle{adverb}共享相同的同义词列表)。

答案1

这将循环遍历定义的单词列表并根据需要重复。大写版本是根据 OP 的要求实现的。使用或定义列表\synonymlist{listname}{item1,item2,item3,...}并使用大写版本调用列表。\listname\Listname

\documentclass{article}
\usepackage{listofitems}
\def\casecmd{}
\newcommand\synonymlist[2]{%
  \newcounter{#1cnt}%
  \expandafter\readlist\expandafter*\csname#1list\endcsname{#2}%
  \expandafter\newcommand\csname#1\endcsname{%
    \stepcounter{#1cnt}%
    \ifnum\value{#1cnt}>\expandafter\listlen\csname#1list\endcsname[]\relax
      \setcounter{#1cnt}{1}%
    \fi
    \expandafter\casecmd
    \csname#1list\endcsname[\csname the#1cnt\endcsname]%
  }%
  \expandafter\newcommand\csname\expupperchar#1\endcsname{\bgroup
    \def\casecmd{\expandafter\expandafter\expandafter\expupperchar}%
    \csname#1\endcsname\egroup
  }%
}
\def\expupperchar#1{%
  \ifcase\numexpr`#1-`a\relax
   A\or B\or C\or D\or E\or F\or G\or H\or I\or J\or K\or L\or M\or
   N\or O\or P\or Q\or R\or S\or T\or U\or V\or W\or X\or Y\or Z\else
   #1\fi
}
\begin{document}
\synonymlist{adverb}{however, nevertheless, nonetheless, notwithstanding}
\adverb, \Adverb, \adverb\\
\adverb, \Adverb \\
\adverb

\synonymlist{noun}{truck, bike, train}
\Noun{} is a fine noun, different from\\
\noun{} or \noun.
\end{document}

在此处输入图片描述

答案2

我会用它*来选择大写,而不是更改列表名称。

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\makesynonymlist}{mm}
 {
  \seq_clear_new:c { g_thompsonh_syn_#1_seq }
  \seq_set_from_clist:cn { g_thompsonh_syn_#1_seq } { #2 }
 }

\NewDocumentCommand{\cycle}{sm}
 {
  \seq_gpop_left:cN { g_thompsonh_syn_#2_seq } \l__thompsonh_syn_tmp_tl
  \seq_gput_right:cV { g_thompsonh_syn_#2_seq } \l__thompsonh_syn_tmp_tl
  \IfBooleanTF { #1 }
   {
    \text_titlecase:n { \tl_use:N \l__thompsonh_syn_tmp_tl }
   }
   {
    \tl_use:N \l__thompsonh_syn_tmp_tl
   }
 }

\ExplSyntaxOff

\makesynonymlist{adverb}{however, nevertheless, nonetheless, notwithstanding}

\begin{document}

\cycle*{adverb} \cycle{adverb} \cycle{adverb} \cycle{adverb} \cycle{adverb}

\end{document}

在此处输入图片描述

修改为从列表中随机选择成员。

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\makesynonymlist}{mm}
 {
  \seq_clear_new:c { g_thompsonh_syn_#1_seq }
  \seq_set_from_clist:cn { g_thompsonh_syn_#1_seq } { #2 }
 }

\NewExpandableDocumentCommand{\cycle}{sm}
 {
  \IfBooleanTF { #1 }
   {
    \text_titlecase:n { \__thompsonh_syn_get:n { #2 } }
   }
   {
    \__thompsonh_syn_get:n { #2 }
   }
 }

\cs_new:Nn \__thompsonh_syn_get:n
 {
  \seq_item:cn { g_thompsonh_syn_#1_seq }
   {
    \int_rand:n { \seq_count:c { g_thompsonh_syn_#1_seq } }
   }
 }

\ExplSyntaxOff

\makesynonymlist{adverb}{however, nevertheless, nonetheless, notwithstanding}

\begin{document}

\cycle*{adverb} \cycle{adverb} \cycle{adverb} \cycle{adverb} \cycle{adverb}

\end{document}

在此处输入图片描述

相关内容