# [Tex/LaTex] expl3 variables and functions

expl3

I am trying to learn how to use expl3 and am a little confused and could use some direction.

I'm trying to figure out this whole naming scheme for variables and functions as addressed in the expl3 documentation, and cannot figure it out. There is quite a lot of text dedicated to talking about name space, but are there actually global variables and functions? Or is it simply an artificial creation using a strict naming scheme.

Second, can I get an example of a function that would do the following without using any extra packages other than expl3? Or is that something that would be out of the expl3 scope. What I am trying to get out of this is a way to program essentially in a c or java like manner.

void myexamplefunction(int count, string[] line){
for(int k = 0; k < count; k = k + 1){
print k;
print line[k];
}
}


The function takes in an integer called count and a string array called line.
It will then using a for loop iterate k from 0 to count, printing the number k (the current iteration number) followed by the string from line associated with k.

Thanks a bunch.

There are no functions or variables as you would think of them in another language like perl or java or most any other programming language you might like. LaTeX works by expanding control sequences into tokens and then further expanding those tokens. On the surface, it may look like you have variables and functions, but that's not at all what's happening.

Beginners of LaTeX can perhaps get away moderately harmlessly thinking in terms of functions and variables. But in the long run, this sort of thinking will frustrate you and get in the way of writing the code you want to accomplish what you want.

You need to keep in mind what LaTeX (and ultimately TeX) was designed to do: format text. As such, it doesn't really have data structures. What's nice about Expl3 is that it creates the feel of having a variety of data structures to choose from (and, if it creates that feel is that really any different from actually having them?). Nevertheless, Expl3 provides a variety of tools for managing and manipulating textual content, but at heart it's still inhabiting the same world as LaTeX/TeX where it is tokens and their content that your are working with. Ultimately, if you're going to write code in LaTeX, you will have to come to understand what's happening under the hood.

So when we write

\newcommand\mycommand[1]{do something with #1'}


all that \mycommand is really doing when it is used is expanding into whatever was placed between its defining brackets. And when we use it, it just expands as:

\mycommand{stuff}->do something with stuff'


A lot of the functionality (pun intended) of some very nicely written packages such as pgf is how this expansion process is taken advantage of so that on the surface the code is easy to write. But underneath the hood, there's a lot going on.

Likewise, when you get to Expl3, things are really no different. Expl3 is providing control sequences that have been carefully defined to give the appearance of functioning in a particular way.

\tl_new:N \l_ae_variable_tl


may look like variable declaration, but it's just priming things for a later

\tl_set:Nn \l_ae_variable_tl {content}


And I think the idea is that at some point there will be bells and whistles built into \tl_set:Nn to make sure that the following control sequences has already been primed. But currently, this is really nothing more than

\let\l_ae_variable_tl


and then later

\def\l_ae_variable_tl{content}


I may be wrong in the particulars of whether \tl_new:N is essentially letting to \relax, but my point is that it's still operating under the same principals of LaTeX/TeX.

Here's an approach to create the sort of functionality you want. I've taken a slightly different approach than Manual (and I think his use of \int_step_inline:nnnn is cleaner and perhaps clearer)

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn

\int_new:N \l_ae_count_value_int

\cs_new_protected:Nn \__ae_space: { ~ }

\cs_new_protected:Npn \myexamplefunction #1#2 {
\int_set:Nn \l_ae_count_value_int {0}
\noindent
\tl_set:Nn \l_ae_string_tl {#2}
%% make sure that spaces in the user passed string don't
%% get obliterated by the inline map
\tl_replace_all:Nnn \l_ae_string_tl {~}{\__ae_space:}
\tl_map_inline:Nn \l_ae_string_tl
{
\int_use:N \l_ae_count_value_int
##1
\newline
\int_incr:N \l_ae_count_value_int
\int_compare:nT
{ \l_ae_count_value_int > #1 }
{ \tl_map_break: }
}
}

\ExplSyntaxOff

\begin{document}

\myexamplefunction{5}{this is my string}

\end{document}