4

In my attempts to get grasp on expl3 I have come into a wall, I cannot figure out how store the result of a function.

For example in pseudocode of what I want to do is this, store the second character of tempstring (that is the character b) in otherstring without changing tempstring

string tempstring = abcde
string otherstring = tempstring[2]

print tempstring
print otherstring

This would result in the output of abcde b

In the following expl3 code I believe I have declared my variables correctly, but the 4th line where I do my operation does not work.

\tl_new:N \templist
\tl_new:N \otherlist

\tl_set:Nn \templist {abcde}
\tl_set:Nn \otherlist {\tl_item:Nn \templist {2}}
\templist
\otherlist

I would have expected this to output abcde b as well but it does not.

And unfortunately \otherlist = \templist doesn't seem to do what I need either.

I have also tried \tl_set_eq:Nn \otherlist {\tl_item:Nn \templist {2}} but that doesn't work either.


I have edited the code to show a minimal broken example, apparently what I wrote above works just fine.

Edited again, I found my error. David was correct. In my main code I had forgotten the :Nn after \tl_item

  • Try \tl_set:Nx \otherlist {\tl_item:Nn \templist {2}}. – Henri Menke Feb 3 '16 at 16:14
  • To insert spaces in an expl3 context use ~ as in \templist ~ \otherlist. – Henri Menke Feb 3 '16 at 16:19
7

The code works as you describe (it is always best to give a full document that shows an error?)

enter image description here

\documentclass{article}
\usepackage{expl3}
\begin{document}

\ExplSyntaxOn

\tl_new:N \templist
\tl_new:N \otherlist

\tl_set:Nn \templist {abcde}
\tl_set:Nn \otherlist {\tl_item:Nn \templist {2}}

\ExplSyntaxOff

\templist

\otherlist

\end{document}

Note that this defines \otherlist to extract the second token. If you want it to be the result of that, use

\tl_set:Nx \otherlist {\tl_item:Nn \templist {2}}

the visual output is the same, b in both cases.

  • Hmm, that is strange. I have updated the question to show my exact problem. Also could you elaborate on what you mean by n vs x? – Bob Feb 3 '16 at 16:47
  • @Bob with n it is \def\otherlist{\tl_item:Nn \templist {2}} so \otherlist is defined to get the second item of \templist at the time it is used. With x it is \edef and so \otherlist will be defined to be b and will be b even if \templist is changed. – David Carlisle Feb 3 '16 at 20:05
  • @Bob Remember we are using macro expansion: simply setting a token list stores exactly those tokens (expl3 calls this n-type), whereas we can force expansion using x-type code. – Joseph Wright Feb 3 '16 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.