
入力を連番に変換できるコマンドはありますか (そして、その入力が再度使用されると、コマンドは以前に割り当てられた番号を呼び出します)?
コンテキスト: さまざまな科学実験について議論しています。ソース TEX ファイルでは、実験ノートを参照する実験番号を使用していますが、最終的な PDF では、実験番号を連続番号にしたいと考えています。明らかに、同じ実験には同じ番号を使用したいと考えています。実験が初めて言及されたときは、新しい連続番号が割り当てられ、そうでない場合は、割り当てられた番号が呼び出されます。
% the \magicnumbering is a command name that I invented for this purpose!
\documentclass{report}
\begin{document}
I did \magicnumbering{experimentA} before \magicnumbering{experimentB}, but \magicnumbering{experimentC} was done even before \magicnumbering{experimentA}.
\end{document}
次の MWE を印刷します:
2 の前に 1 をしましたが、3 は 1 よりも前に行われました。
きっと、私が見つけられない非常に簡単な解決策があるはずです。無知で申し訳ありませんが、助けていただければ幸いです。
答え1
以下では、キーをプロパティ リスト ( を使用expl3
) に格納することでこれを実現します。出力の書式を変更するには、 の定義を変更します\magicnumbering_output:n
。
\documentclass[]{report}
\ExplSyntaxOn
\prop_new:N \g_magicnumbering_numbers_prop
\int_new:N \g_magicnumbering_current_int
\tl_new:N \l_magicnumbering_output_tl
\cs_new_protected:Npn \magicnumbering_output:n #1
{
#1
}
\cs_generate_variant:Nn \magicnumbering_output:n { V }
\cs_new_protected:Npn \magicnumbering_parse:n #1
{
\prop_get:NnNTF
\g_magicnumbering_numbers_prop {#1} \l_magicnumbering_output_tl
{ \magicnumbering_output:V \l_magicnumbering_output_tl }
{
\int_gincr:N \g_magicnumbering_current_int
\prop_put:NnV
\g_magicnumbering_numbers_prop {#1} \g_magicnumbering_current_int
\magicnumbering_output:V \g_magicnumbering_current_int
}
}
\NewDocumentCommand \magicnumbering { m } { \magicnumbering_parse:n {#1} }
\ExplSyntaxOff
\begin{document}
I did \magicnumbering{experimentA} before \magicnumbering{experimentB}, but
\magicnumbering{experimentC} was done even before
\magicnumbering{experimentA}.
\end{document}