GeneralHow to emulate string interpolation in the macro/command/function body?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
JohnScience
Posts: 1
Joined: Mon Apr 26, 2021 1:30 am

How to emulate string interpolation in the macro/command/function body?

Post by JohnScience »

It is a copy of my question from https://tex.stackexchange.com/questions ... ction-body.

I am new to LaTeX and I am trying to iterate on the available solution that I found on LaTeX Stack Exchange.

Code: Select all

% Credit: https://tex.stackexchange.com/questions/215563/storing-an-array-of-strings-in-a-command
\ExplSyntaxOn

% Some commands are declared with NewDocumentCommand and NewExpandableDocumentCommand because \ExplSyntaxOn messes up with text
% (produces no spaces) 

\NewDocumentCommand{\storedata}{
m % #1: <token list> array name; for example, ``a_wonderful_name_for_my_array"
m % #2: <sequence> that stores braced list of values; for example, ``{one}{two}{three}"
}
{
	\au_store_data:nn { #1 } { #2 }
}

\NewDocumentCommand{\appenddata}{
m % #1: <token list> array name; for example, ``a_wonderful_name_for_my_array"
m % #2: <sequence> of values to append; for example, ``{one}{two}{three}"
}
{
	\au_append_data:nn { #1 } { #2 }
}

\NewExpandableDocumentCommand{\getdata}{
O{1} % #1: <integer expression> that evalues to the accessed index
m % #2: <token list> array name; for example, ``a_wonderful_name_for_my_array"
}
{
	\au_get_data:nn { #1 } { #2 }
}

\NewExpandableDocumentCommand{\getlength}{
m % #1: <token list> array name; for example, ``a_wonderful_name_for_my_array"
}
{
	\au_get_length:n { #1 }
}

\NewDocumentCommand{\removelast}{
o % #1: <cname> output variable prefixed with a '\'; for example, scratch variable ``\l_tmpa_tl" or ``\scope_module_description_type"
m % #2: <token list> array name; for example, ``a_wonderful_name_for_my_array"
}
{
	\au_remove_last:Nn { #1 } { #2 }
}

\cs_new_protected:Npn \au_store_data:nn
#1 % <token list> array name; for example, ``a_wonderful_name_for_my_array"
#2 % <sequence> that stores braced list of values; for example, ``{one}{two}{three}"
 {
  % create the sequence if it doesn't exist or clear it if it exists
  \seq_clear_new:c { l_au_data_#1_seq }
  % append the items
  \__au_append_data:nn { #1 } { #2 }
 }

\cs_new_protected:Npn \au_append_data:nn
#1 % <token list> array name; for example, ``a_wonderful_name_for_my_array"
#2 % <sequence> of values to append; for example, ``{one}{two}{three}"
{
	% create the sequence if it doesn't exist, do nothing if it exists
	\seq_if_exist:cF { l_au_data_#1_seq }
	{ \seq_new:c { l_au_data_#1_seq } }
	% append the items
	\__au_append_data:nn { #1 } { #2 }
}

\cs_new:Npn \au_get_data:nn
#1 % <integer expression> that evalues to the accessed index
#2 % <token list> array name; for example, ``a_wonderful_name_for_my_array"
{
	% retrieve the requested item
	\seq_item:cn { l_au_data_#2_seq } { #1 }
}

\cs_new:Npn \au_get_length:n
#1 % <token list> array name; for example, ``a_wonderful_name_for_my_array"
{
	\seq_count:c { l_au_data_#1_seq }
}

\cs_new_protected:Npn \au_remove_last:Nn
#1 % <cname> output variable prefixed with a '\'; for example, scratch variable ``\l_tmpa_tl" or ``\scope_module_description_type"
#2 % <token list> array name; for example, ``a_wonderful_name_for_my_array"
{
	\IfNoValueTF { #1 }
	{
		\__au_remove_last:Nn \l_tmpa_tl { #2 }
	}
	{
		\__au_remove_last:Nn #1 { #2 }
	}
}

\cs_new_protected:Npn \__au_append_data:nn
#1 % <token list> array name; for example, ``a_wonderful_name_for_my_array"
#2 % <sequence> of values to append; for example, ``{one}{two}{three}"
{
	% append items one at a time
	\tl_map_inline:nn { #2 }
	{
		\seq_put_right:cn { l_au_data_#1_seq } { ##1 }
	}
}

\cs_new_protected:Npn \__au_remove_last:Nn
#1 % <cname> output variable prefixed with a '\'; for example, scratch variable ``\l_tmpa_tl" or ``\scope_module_description_type"
#2 % <token list> array name; for example, ``a_wonderful_name_for_my_array"
{
	\seq_pop_right:cN { l_au_data_#2_seq } #1
}

\ExplSyntaxOff
% Credit (end)[/latex]

I would like to somehow emulate the behavior of replacing #1 etc with provided arguments. I've placed this before all declarations:

[latex]\tl_new:N \g_au_internal_seq_prefix_tl
\tl_gset:Nn \g_au_internal_seq_prefix_tl {l_au_data_}
\tl_new:N \g_au_internal_seq_postfix_tl
\tl_gset:Nn \g_au_internal_seq_postfix_tl {_seq}
and added this function below:

Code: Select all

\cs_new_protected:Npn \au_get_internal_seq_name_by_array_name:n
#1 % <token list> array name; for example, ``a_wonderful_name_for_my_array"
{
	\tl_set_eq:NN \l_tmpa_tl \g_au_internal_seq_prefix_tl
	\tl_put_right:Nn \l_tmpa_tl { #1 }
	\tl_put_right:NV \l_tmpa_tl \g_au_internal_seq_prefix_tl
	\tl:use:N \l_tmpa_tl
}
Here is how I tried to use it:

Code: Select all

\cs_new_protected:Npn \au_store_data:nn
#1 % <token list> array name; for example, ``a_wonderful_name_for_my_array"
#2 % <sequence> that stores braced list of values; for example, ``{one}{two}{three}"
{
	% create the sequence if it doesn't exist or clear it if it exists
	%\seq_clear_new:c { l_au_data_#1_seq }
	\seq_clear_new:c { \tl_use:N \au_get_internal_seq_name_by_array_name:n{ #1 } }
	% append the items
	\__au_append_data:nn { #1 } { #2 }
}
Once again, I'm new to LaTeX and http://ctan.mirror.colo-serv.net/macros ... rface3.pdf wasn't a big help to me.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Post Reply