在加密貨幣中,證明涉及一系列遊戲。每個遊戲都包含一個台詞清單。相鄰遊戲除了少量行數(通常為 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
.結果是
但我不知道如何解決第二個問題。我有兩個想法:
每次列印出遊戲時,請檢查所有項目並刪除對 的所有呼叫
gamediff
。障礙gamediff
可能不在最頂層。它可以是{{gamediff}}
或$gamediff$
。當項目儲存到列表中時,使用者不會
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}
結果是