After a small fight with it (first time using prop
) I think this works, although it's a bit of a mix of things, and may it would be better to think of a better “user interface”.
First, you have an environment cryptolist
which is like an enumerate
except that \item{…}
does have an argument. cryptolist
has an optional argument which is the name of the list (by default it's default
) which gives you the option to mantain many different lists at the same time.
Then there are two commands \replacecryptoitems
(the name in the optional argument and a usual key-value list of the items to replace) and \printcryptolist
(the name in the optional argument). For instance you can \replacecryptoitems{2=Modified second item, 3=modified third item}
and then \printcryptolist
to print it.
To initialise a new crypto list you need \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}
Extended code to help the new request. In this case, each time you use \replacecryptoitems
the replaced entries are recorded. And the next time \printcryptoitems
is given (only the next time, after one usage it's reset) the changed entries are \gamediff{…}
ed.
\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}
expl3
, but, for instance, in order to define the first time the list it would be easier if the item was an argument to the\item
command.