Quantcast
Channel: Working with clist indices in LaTeX3 - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 2

Working with clist indices in LaTeX3

$
0
0

Friends, I'm trying to port an old code of mine to a LaTeX3 implementation. Thanks to egreg, Joseph and David, I made a good progress! But then, I got stuck.

From the link above, I'm porting my songbook project. As you know, I have several indices for my songs, and I used to hardcode all of them beforehand. The code is not extensible without the need of adding more and more conditionals.

My idea: have a list (a comma separated list, or in L3 jargon, a clist) holding the index name and another list holding the abbreviation name. If I want to add, say, the word Hello world to an index with F as an abbreviation, I search the abbreviations list for the position of my item, then check its name counterpart.

From now on, the word index will be used to refer to an integer pointer that identifies an element of an array, and not the list of information we have in typography.

As usual, egreg saved the day and helped me with a L3 code to look for an item in a clist and return its index i. The code has the following approach:

  • i > 0 : the first occurrence of the item in the clist. It's important to note that clist's indices start with 1 .
  • i = 0 : the item is not found in the list.

I'm struggling with using the value from this lookup code in order to get the item from the other list, with \clist_item:Nn. Let's see some code:

% arara: pdflatex: { shell: on }% arara: pdflatex\documentclass{article}\usepackage[T1]{fontenc}\usepackage[utf8]{inputenc}\usepackage{xparse}\usepackage{imakeidx}\ExplSyntaxOn\clist_new:N \l_vozes_extensions_clist\clist_new:N \l_vozes_abbreviations_clist\int_new:N \l_vozes_index_step_int\bool_new:N \l_vozes_index_found_bool\cs_new:Npn \vozes_getitemindex:Nn #1 #2 {  \int_zero:N \l_vozes_index_step_int  \bool_set_false:N \l_vozes_index_found_bool  \clist_map_inline:Nn #1   {    \int_incr:N \l_vozes_index_step_int    \tl_if_eq:nnT { #2 } { ##1 }    { \clist_map_break:n {  \bool_set_true:N \l_vozes_index_found_bool } }   }   \bool_if:NTF{ \l_vozes_index_found_bool }{ \int_to_arabic:n { \l_vozes_index_step_int } }{ 0 }}\cs_new:Npn \vozes_newthematicindex:nnnn #1 #2 #3 #4{   \clist_put_right:Nn  \l_vozes_extensions_clist { #1 }   \clist_put_right:Nn \l_vozes_abbreviations_clist { #3 }   \makeindex[name=#1,title=#2,columns=#4]}\NewDocumentCommand{\newthematicindex}{ m m m m }{   \vozes_newthematicindex:nnnn { #1 }{ #2 }{ #3 }{ #4 }}\NewDocumentCommand{\thematicindex}{ m m }{   %\clist_item:Nn \l_vozes_extensions_clist {   % \vozes_getitemindex:Nn \l_vozes_abbreviations_clist { #1 }   %}   \index[ \clist_item:Nn \l_vozes_extensions_clist { 1 } ]{ #2 }}\NewDocumentCommand{\printthematicindex}{ m }{   %\clist_item:Nn \l_vozes_extensions_clist {   % \vozes_getitemindex:Nn \l_vozes_abbreviations_clist { #1 }   %}   \printindex[ \clist_item:Nn \l_vozes_extensions_clist { 1 } ]}\ExplSyntaxOff\newthematicindex{ifoo}{The list of Foo}{F}{2}\newthematicindex{ibar}{Bar is cool}{B}{2}\newthematicindex{ifb}{Foo and Bar}{FB}{2}\begin{document}Hello.\thematicindex{F}{Hello Foo}\thematicindex{B}{Hello Bar}\thematicindex{FB}{Hello Foobar}\printthematicindex{F}\printthematicindex{B}\printthematicindex{FB}\end{document}

Of course, this code doesn't work. :) The offending part is in the commented parts:

%\clist_item:Nn \l_vozes_extensions_clist {% \vozes_getitemindex:Nn \l_vozes_abbreviations_clist { #1 }%}

I'm lost with integer values. :( And speaking of which, I wonder how can I save the return of a command to a variable, should I use \def?

One last question: Is it good practice to mix L2 and L3? Should I \ExplSyntaxOff every time I have a L2 code, or there isn't any side effect?

Any enlightenment is appreciated. :)


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images