# How to use \makebox in \tl_map_function?

While trying to answer LaTeX for music I came to the problem that I can’t use a \makebox inside the function for a \tl_map_function:NN call. The problematic line is commented out in the following code.

With the line I get this error:

! Undefined control sequence.
<argument> \LaTeX3 error:
Erroneous variable \color@endgroup used!
l.43 \function{T}{3---4, ..7-8}


When replacing the #1 inside the \makebox argument by e.g. x there’s no error. Using \tl_set:Nn \l_tmpa_tl { #1 } and \tl_use:N \l_tmpa_tl in the argument I get the same error.

So how can I use the \makebox (or another box of fixed width) in this context?

Code:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\dim_new:N \l_palamas_boxwidth_dim
\dim_set:Nn \l_palamas_boxwidth_dim { 0.5em }

\tl_new:N \g_palamas_line_tl
\tl_set:Nn \g_palamas_line_tl { \rule [ 0.7ex ] { \dim_use:N \l_palamas_boxwidth_dim } { 0.3pt } }
\tl_new:N \g_palamas_space_tl
\tl_set:Nn \g_palamas_space_tl { \hspace { \dim_use:N \l_palamas_boxwidth_dim }  }

\cs_new:Npn \palamas_makebox:n #1 {
% DOESN'T WORK:
%   \makebox [ \dim_use:N \l_palamas_boxwidth_dim ] [ r ] { #1 }
% DOESN'T WORK:
\tl_set:Nn \l_tmpa_tl { #1 }
\makebox [ \dim_use:N \l_palamas_boxwidth_dim ] [ r ] { \tl_use:N \l_tmpa_tl }
% WORKS:
%   #1
}
\cs_new:Npn \palamas_process_list:n #1 {
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_replace_all:Nnn \l_tmpa_tl { - } { \tl_use:N \g_palamas_line_tl }
\tl_replace_all:Nnn \l_tmpa_tl { . } { \tl_use:N \g_palamas_space_tl }
\tl_map_function:NN \l_tmpa_tl \palamas_makebox:n
\\
}

\NewDocumentCommand { \function } { m O{0.1em} m } {
#1
\clist_set:Nn \l_tmpa_clist { #3 }
\kern#2
{
\tiny
\begin{tabular}[b]{@{}l@{}}
\clist_map_function:NN \l_tmpa_clist \palamas_process_list:n
\int_compare:nT { \clist_count:N \l_tmpa_clist < 3 } { \\ }
\end{tabular}
}
}

\ExplSyntaxOff

\begin{document}
\function{s}{6,5}
\end{document}

• Note you are not following the indenting and spacing guidelines of expl3 :) – Manuel Sep 15 '15 at 12:31
• @Manuel: Yep … I know, but I prefer my style ;-) – Tobi Sep 15 '15 at 13:09
• Well, the guidelines are there for a reason, I think having some sort of consensus is good. If this code will never be public, okey, but if something is to be published, it's nice if it follows the guidelines. I personally (after some time) can read much faster expl3 code (if it's correctly formatted). – Manuel Sep 15 '15 at 13:51
• Well … I used the official formatting for some packages but I can read my style better and since I’m am the one who works most with the code, I like it to do ist my way ;-) – Tobi Sep 15 '15 at 14:11

The problem is that you're doing a mapping of a token list and

\tl_use:N \g_palamas_line_tl


are two distinct items in the token list and \palamas_makebox:n { \tl_use:N } is what eventually produces the error.

Either brace the item

\tl_replace_all:Nnn \l_tmpa_tl { - } { {\tl_use:N \g_palamas_line_tl} }
\tl_replace_all:Nnn \l_tmpa_tl { . } { {\tl_use:N \g_palamas_space_tl} }


or just remove \tl_use:N. By the way, you should use \cs_new_protected:Npn in both cases where you have \cs_new:Npn; and \dim_use:N is wrong in each place you have it.

• Great, that solved the problem. Thanks! One questions: So all \xx_use:N are only to be used when I want to “print” the content in the output i.e. where I’d use \the in LaTeX2e? – Tobi Sep 15 '15 at 13:12
• @Tobi Not really; but in all your use cases of \dim_use:N, the rules allow a register, so \dim_use:N just makes TeX do more work. – egreg Sep 15 '15 at 13:17