自动从上一个列表中复制项目

自动从上一个列表中复制项目

在加密中,证明涉及一系列游戏。每个游戏都包含一串行数。相邻的游戏几乎相同,除了少数行数(通常为 1)。我过去常常将旧游戏复制到下一个游戏,然后在新游戏上进行修改。但这真的很不方便,而且很难维护。生成的源文件也大得没有必要。试想一下,一个有 20 个游戏的证明,每个游戏有 10 行。

因此,我决定为此创建一个环境。它看起来像一个“枚举”环境,其中每个项目也是一个“枚举”环境。每次我为外部枚举器创建一个新项目(游戏)时,我还会将要更改的上一款游戏的哪些行指定为可变参数。我希望环境本身自动复制参数中未指定的上一款游戏中的每一行。

知道如何实现这一点吗?

答案1

经过一番小小的斗争之后(第一次使用prop),我认为它是可行的,尽管它有点混乱,但最好考虑一个更好的“用户界面”。

cryptolist首先,你有一个类似于 except 的环境enumerate,它\item{…}有一个参数。cryptolist有一个可选参数,它是列表名称(默认情况下为default),它可让您选择同时维护多个不同的列表。

然后有两个命令\replacecryptoitems(可选参数中的名称和要替换的项的常用键值列表)和\printcryptolist(可选参数中的名称)。例如,您可以\replacecryptoitems{2=Modified second item, 3=modified third item}然后\printcryptolist打印它。

要初始化新的加密列表,您需要\newcryptolist{name}

在此处输入图片描述

\documentclass{scrartcl}
\usepackage{xparse,enumitem}

\ExplSyntaxOn
\NewDocumentCommand \newcryptolist { m }
 {
  \prop_new:c { g_cryptolist_#1_prop }
  \int_new:c  { g_cryptolist_#1_int  }
 }
\NewDocumentEnvironment { cryptolist } { O{default} }
 {
  \tl_set:Nn \l_cryptolist_name_tl { #1 }
  \int_gzero:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _int }
  \prop_gclear:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _prop }
  \enumerate
   \cs_set_eq:NN \normalitem \item
   \cs_set_eq:NN \item \cryptoitem
 }
 { \endenumerate }
\NewDocumentCommand \cryptoitem { o m }
 {
  \int_gincr:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _int }
  \tl_set:Nx \l_cryptolist_item_tl
   { \int_use:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _int } }
  \prop_gput:con { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _prop }
   { \l_cryptolist_item_tl }
   { #2 }
  \IfValueTF { #1 } { \normalitem [ #1 ] } { \normalitem }
  #2
 }
\NewDocumentCommand \printcryptolist { O{default} }
 { \cc_printitems:n { #1 } }
\NewDocumentCommand \replacecryptoitems { O{default} m }
 { \cc_replaceitems:nn { #1 } { #2 } }

\tl_new:N \l_cryptolist_name_tl
\tl_new:N \l_cryptolist_item_tl
\prop_new:N \g_cryptolist_default_prop
\int_new:N  \g_cryptolist_default_int
\keys_define:nn { cryptolist }
 {
  unknown .code:n =
   \prop_gput:cVn { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _prop }
    \l_keys_key_tl { #1 }
 }
\cs_new_protected:Npn \cc_printitems:n #1
 {
  \begin{enumerate}
    \tl_set:Nn \l_cryptolist_name_tl { #1 }
    \int_step_inline:nnnn { 1 } { 1 }
     { \int_use:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _int } }
     {
      \item
      \prop_item:cn { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _prop }
       { ##1 }
     }
  \end{enumerate}
 }
\cs_new_protected:Npn \cc_replaceitems:nn #1 #2
 {
  \tl_set:Nn \l_cryptolist_name_tl { #1 }
  \keys_set:nn { cryptolist } { #2 }
 }
\ExplSyntaxOff

\newcryptolist{anotherlist}

\begin{document}

\begin{cryptolist}[anotherlist]
  \item{First item to be used later;}
  \item{second item.}
\end{cryptolist}

\begin{cryptolist}
  \item{First item}
  \item{Second item}
  \item{Third item}
  \item{Last item}
\end{cryptolist}

\replacecryptoitems{2 = Second (modified) item, 4 = Last modified item.}
\printcryptolist

\replacecryptoitems[anotherlist]{1=Changing the first one to be used now;}
\printcryptolist[anotherlist]

\end{document}

扩展代码以帮助处理新请求。在这种情况下,每次使用\replacecryptoitems替换的条目都会被记录下来。下一次\printcryptoitems给出(仅有的下次使用一次后(重置)会\gamediff{…}编辑更改的条目。

在此处输入图片描述

\documentclass{scrartcl}
\usepackage{xparse,enumitem}
\usepackage[x11names]{xcolor}

\ExplSyntaxOn
\NewDocumentCommand \gamediff { m }
 { \textcolor { Tomato3 } { #1 } }
\NewDocumentCommand \newcryptolist { m }
 {
  \prop_new:c { g_cryptolist_#1_prop }
  \int_new:c  { g_cryptolist_#1_int  }
  \seq_new:c  { g_cryptolist_modifieditems_#1_seq  }
 }
\NewDocumentEnvironment { cryptolist } { O{default} }
 {
  \tl_set:Nn \l_cryptolist_name_tl { #1 }
  \int_gzero:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _int }
  \prop_gclear:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _prop }
  \enumerate[noitemsep]
   \cs_set_eq:NN \normalitem \item
   \cs_set_eq:NN \item \cryptoitem
 }
 { \endenumerate }
\NewDocumentCommand \cryptoitem { o m }
 {
  \int_gincr:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _int }
  \tl_set:Nx \l_cryptolist_item_tl
   { \int_use:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _int } }
  \prop_gput:con { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _prop }
   { \l_cryptolist_item_tl }
   { #2 }
  \IfValueTF { #1 } { \normalitem [ #1 ] } { \normalitem }
  #2
 }
\NewDocumentCommand \printcryptolist { O{default} }
 { \cc_printitems:n { #1 } }
\NewDocumentCommand \replacecryptoitems { O{default} m }
 { \cc_replaceitems:nn { #1 } { #2 } }

\tl_new:N \l_cryptolist_name_tl
\tl_new:N \l_cryptolist_item_tl
\prop_new:N \g_cryptolist_default_prop
\seq_new:N \g_cryptolist_modifieditems_default_seq
\int_new:N  \g_cryptolist_default_int
\keys_define:nn { cryptolist }
 {
  unknown .code:n =
   \seq_gput_right:cV
    { g_cryptolist_modifieditems_ \tl_use:N \l_cryptolist_name_tl _seq }
    \l_keys_key_tl
   \prop_gput:cVn { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _prop }
    \l_keys_key_tl { #1 }
 }
\cs_new_protected:Npn \cc_printitems:n #1
 {
  \begin{enumerate}[noitemsep]
    \tl_set:Nn \l_cryptolist_name_tl { #1 }
    \int_step_inline:nnnn { 1 } { 1 }
     { \int_use:c { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _int } }
     {
      \item
      \group_begin:
      \seq_if_in:cnF
       { g_cryptolist_modifieditems_ \tl_use:N \l_cryptolist_name_tl _seq }
       { ##1 }
       { \cs_set_eq:NN \gamediff \prg_do_nothing: }
      {
       \prop_item:cn { g_cryptolist_ \tl_use:N \l_cryptolist_name_tl _prop }
        { ##1 }
      }
      \group_end:
     }
  \end{enumerate}
  \seq_gclear:c
   { g_cryptolist_modifieditems_ \tl_use:N \l_cryptolist_name_tl _seq }
 }
\cs_new_protected:Npn \cc_replaceitems:nn #1 #2
 {
  \tl_set:Nn \l_cryptolist_name_tl { #1 }
  \keys_set:nn { cryptolist } { #2 }
 }
\ExplSyntaxOff

\newcryptolist{anotherlist}

\begin{document}

\begin{cryptolist}
  \item{First item}
  \item{Second item}
  \item{Third item}
  \item{Last item}
\end{cryptolist}

\replacecryptoitems{2 = Second \gamediff{(modified)} item, 4 = Last modified \gamediff{item}.}
\printcryptolist

And this next time they are not highlighted.
\printcryptolist

\end{document}

答案2

\documentclass{article}
\usepackage{color}
\usepackage{expl3}

\newcommand\gamediff[2][red]{\color{#1}{\underline{\color{black}{#2}}}\color{black}{}}
\newsavebox\MBox
\newcommand\gamediffeq[2][red]{{\sbox\MBox{$#2$}\rlap{\usebox\MBox}\color{#1}\rule[-1.2\dp\MBox]{\wd\MBox}{0.5pt}}}

\ExplSyntaxOn
\seq_new:N \g_crypto__gameseq_seq
\tl_new:N \g_crypto__gameseq_tl
\int_new:N \g_crypto__gameseq_i
\cs_new:Npn \crypto__gameseq_move:ccn #1#2#3
  {
    \int_step_inline:nnnn { 1 } { 1 } { #3 }
      {
        \seq_gpop_left:cN { #1 } \g_crypto__gameseq_tl
        \seq_gput_left:cV { #2 } \g_crypto__gameseq_tl
      }
  }
\cs_new:Npn \crypto__gameseq_store:cn #1#2
  {
    \crypto__gameseq_move:ccn { #1 } { g_crypto__gameseq_seq } { #2 }
  }
\cs_new:Npn \crypto__gameseq_load:cn #1#2
  {
    \crypto__gameseq_move:ccn { g_crypto__gameseq_seq } { #1 } { #2 }
  }
\cs_new:Npn \crypto__gameseq_out:N #1
  {
    \item \tl_use:N #1
  }

\cs_new:Npn \crypto_gameseq_new:c #1
  {
    \seq_gclear_new:c { g_crypto_gameseq_#1 }
    \int_gzero_new:c { g_crypto_gameseq_#1_num }
  }
\cs_new:Npn \crypto_gameseq_add:cnn #1#2#3
  {
    \crypto__gameseq_store:cn { g_crypto_gameseq_#1 } { \int_eval:n { #2 - 1 } }
    \seq_gput_left:cn { g_crypto_gameseq_#1 } { #3 }
    \crypto__gameseq_load:cn { g_crypto_gameseq_#1 } { \int_eval:n { #2 - 1 } }
  }
\cs_new:Npn \crypto_gameseq_del:cnn #1#2#3
  {
    \crypto__gameseq_store:cn { g_crypto_gameseq_#1 } { \int_eval:n { #2 - 1 } }
    \seq_gpop_left:cN { g_crypto_gameseq_#1 } \g_crypto__gameseq_tl
    \crypto__gameseq_load:cn { g_crypto_gameseq_#1 } { \int_eval:n { #2 - 1 } }
  }
\cs_new:Npn \crypto_gameseq_mod:cnn #1#2#3
  {
    \crypto__gameseq_store:cn { g_crypto_gameseq_#1 } { \int_eval:n { #2 - 1 } }
    \seq_gpop_left:cN { g_crypto_gameseq_#1 } \g_crypto__gameseq_tl
    \seq_gput_left:cn { g_crypto_gameseq_#1 } { #3 }
    \crypto__gameseq_load:cn { g_crypto_gameseq_#1 } { \int_eval:n { #2 - 1 } }
  }
\cs_new:Npn \crypto_gameseq_get:c #1
  {
    \int_gincr:c { g_crypto_gameseq_#1_num }
    \item[Game~\int_use:c { g_crypto_gameseq_#1_num }.]
    \begin{enumerate}
    \seq_map_function:cN { g_crypto_gameseq_#1 } \crypto__gameseq_out:N
    \end{enumerate}
  }

\cs_new_eq:NN \seqnew \crypto_gameseq_new:c
\cs_new_eq:NN \seqout \crypto_gameseq_get:c
\cs_new_eq:NN \seqadd \crypto_gameseq_add:cnn
\cs_new_eq:NN \seqdel \crypto_gameseq_del:cnn
\cs_new_eq:NN \seqmod \crypto_gameseq_mod:cnn
\ExplSyntaxOff

\begin{document}
\begin{itemize}
\seqnew{cc}
\seqadd{cc}{1}{first item}
\seqadd{cc}{2}{third item}
\seqadd{cc}{2}{fourth item}
\seqout{cc}
\seqmod{cc}{2}{\gamediff{second} item}
\seqout{cc}
\seqout{cc}
\end{itemize}
\end{document}

为了解决第一个问题,我改用seq而不是prop。结果是 在此处输入图片描述

但我不知道如何解决第二个问题。我有两个想法:

  1. 每次打印游戏时,检查所有项目并删除所有对 的调用gamediff。障碍gamediff可能不在最顶层。它可以是{{gamediff}}$gamediff$

  2. 当将项目存储到列表中时,用户不会gamediff直接调用,而是调用一个函数,该函数将生成当前游戏的gamediff[i]位置i。当打印出游戏时,gamediff会进行评估,只有第一个参数等于当前游戏的才会突出显示其内容。


按照Manuel的想法,问题2也得到了解决。

\documentclass{article}
\usepackage{color}
\usepackage{expl3}

\newcommand\highlight[2][red]{\color{#1}{\underline{\color{black}{#2}}}\color{black}{}}
\newsavebox\MBox
\newcommand\highlighteq[2][red]{{\sbox\MBox{$#2$}\rlap{\usebox\MBox}\color{#1}\rule[-1.2\dp\MBox]{\wd\MBox}{0.5pt}}}

\ExplSyntaxOn
\seq_new:N \g_crypto__gameseq_seq
\tl_new:N \g_crypto__gameseq_tl
\int_new:N \g_crypto__gameseq_i
\cs_new:Npn \crypto__gameseq_move:ccn #1#2#3
  {
    \int_step_inline:nnnn { 1 } { 1 } { \int_eval:n { \int_eval:n { #3 } * 2 } }
      {
        \seq_gpop_left:cN { #1 } \g_crypto__gameseq_tl
        \seq_gput_left:cV { #2 } \g_crypto__gameseq_tl
      }
  }
\cs_new:Npn \crypto__gameseq_store:cn #1#2
  {
    \crypto__gameseq_move:ccn { #1 } { g_crypto__gameseq_seq } { #2 }
  }
\cs_new:Npn \crypto__gameseq_load:cn #1#2
  {
    \crypto__gameseq_move:ccn { g_crypto__gameseq_seq } { #1 } { #2 }
  }

\cs_new:Npn \crypto_gameseq_new:c #1
  {
    \seq_gclear_new:c { g_crypto_gameseq_#1 }
    \int_gzero_new:c { g_crypto_gameseq_#1_num }
  }
\cs_new:Npn \crypto_gameseq_add:cnn #1#2#3
  {
    \crypto__gameseq_store:cn { g_crypto_gameseq_#1 } { #2 - 1 }
    \seq_gput_left:cn { g_crypto_gameseq_#1 } { #3 }
    \seq_gput_left:cn { g_crypto_gameseq_#1 } { 1 }
    \crypto__gameseq_load:cn { g_crypto_gameseq_#1 } { #2 - 1 }
  }
\cs_new:Npn \crypto_gameseq_del:cnn #1#2
  {
    \crypto__gameseq_store:cn { g_crypto_gameseq_#1 } { #2 - 1 }
    \seq_gpop_left:cN { g_crypto_gameseq_#1 } \g_crypto__gameseq_tl
    \seq_gpop_left:cN { g_crypto_gameseq_#1 } \g_crypto__gameseq_tl
    \crypto__gameseq_load:cn { g_crypto_gameseq_#1 } { #2 - 1 }
  }
\cs_new:Npn \crypto_gameseq_mod:cnn #1#2#3
  {
    \crypto__gameseq_store:cn { g_crypto_gameseq_#1 } { #2 - 1 }
    \seq_gpop_left:cN { g_crypto_gameseq_#1 } \g_crypto__gameseq_tl
    \seq_gpop_left:cN { g_crypto_gameseq_#1 } \g_crypto__gameseq_tl
    \seq_gput_left:cn { g_crypto_gameseq_#1 } { #3 }
    \seq_gput_left:cn { g_crypto_gameseq_#1 } { 1 }
    \crypto__gameseq_load:cn { g_crypto_gameseq_#1 } { #2 - 1 }
  }
\cs_new:Npn \crypto_gameseq_get:c #1
  {
    \int_gincr:c { g_crypto_gameseq_#1_num }
    \item[Game~\int_use:c { g_crypto_gameseq_#1_num }.]
    \begin{enumerate}
    \int_step_variable:nnnNn { 1 } { 1 } { \seq_count:c { g_crypto_gameseq_#1 } } \g_crypto__gameseq_i
      {
        \seq_gpop_left:cN { g_crypto_gameseq_#1 } \g_crypto__gameseq_tl
        \int_if_odd:nTF \g_crypto__gameseq_i
          {
            \tl_if_empty:NTF { \g_crypto__gameseq_tl }
              {
                \cs_set_eq:NN \gamediff \prg_do_nothing:
                \cs_set_eq:NN \gamediffeq \prg_do_nothing:
              }
              {
                \cs_set_eq:NN \gamediff \highlight
                \cs_set_eq:NN \gamediffeq \highlighteq
              }
            \seq_gput_left:Nn \g_crypto__gameseq_seq {}
          }
          {
            \item \tl_use:N \g_crypto__gameseq_tl
            \seq_gput_left:cV { g_crypto__gameseq_seq } \g_crypto__gameseq_tl
          }
      }
    \crypto__gameseq_load:cn { g_crypto_gameseq_#1 } { \int_eval:n { \seq_count:c { g_crypto__gameseq_seq } / 2 } }
    \end{enumerate}
  }

\cs_new_eq:NN \seqnew \crypto_gameseq_new:c
\cs_new_eq:NN \seqout \crypto_gameseq_get:c
\cs_new_eq:NN \seqadd \crypto_gameseq_add:cnn
\cs_new_eq:NN \seqdel \crypto_gameseq_del:cnn
\cs_new_eq:NN \seqmod \crypto_gameseq_mod:cnn
\cs_new_eq:NN \gamediff \highlight
\cs_new_eq:NN \gamediffeq \highlighteq
\ExplSyntaxOff

\begin{document}
\begin{itemize}
\seqnew{cc}
\seqadd{cc}{1}{first item}
\seqout{cc}
\seqadd{cc}{2}{\gamediff{third} item}
\seqout{cc}
\seqadd{cc}{2}{fourth item}
\seqout{cc}
\seqmod{cc}{2}{\gamediff{second} item}
\seqout{cc}
\seqout{cc}
\seqdel{cc}{2}
\seqout{cc}
\end{itemize}
\end{document}

结果是在此处输入图片描述

相关内容