2

I would like to create something like a=array[1,2,15,6,18] and then a[0]=1, a[1]=2. Is it possible to create an array in LaTeX (with pgf or another package)?

3
  • Probably, but you'll most likely have to use a different syntax.
    – Werner
    Oct 12 '17 at 4:57
  • 1
    Welcome to TeX.SX! What would be the usage of such arrays? What are you thinking to store in them?
    – egreg
    Oct 12 '17 at 6:21
  • I am a teacher and I would like to store the number of points for each exercise at the beginning of my document. For exemple \points=[5,2,8] would mean that the first exercise has a value of 5 points, the second exercise has a value of 2 points... In my document, I would then have something like: Exercise 1 (\points[0] points)
    – R Ducreux
    Oct 12 '17 at 7:14
4

The package listofitems is designed for that. Here is a small example:

\documentclass{article}
\usepackage{listofitems}
\usepackage[svgnames]{xcolor}

\begin{document}

\readlist*\myarray{1,2,15, lalala, 6,18}
\myarray[3]

\myarray[]

\textcolor{Tomato}{\textsf{\myarray[-3]}} = \textcolor{Tomato}{\textsf{\myarray[4]}}

\end{document} 

enter image description here

3

PGF

Here is how you use arrays with the pgfmath package:

\documentclass{article}
\usepackage{pgfmath}
\newcommand\Arr[1]{#1 $\mapsto $ \pgfmathparse{\myarray[#1]}\pgfmathresult}

\begin{document}
\def\myarray{{1,2,15,6,18}}

  \Arr{0},
  \Arr{1},
  \Arr{2},
  \Arr{3},
  \Arr{4},

\end{document}

Here is the output:

enter image description here

In particular, pgfmath arrays are 0-based (i.e. the indexing starts from 0). For more details search for "array operators" in the pgf manual (section 89.2, page 927 for version 3.0.1a).

LaTeX3

Here is how you can define and access array-like variables using xparse/expl3:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
% Usage: \DefineArray{array name}{data as comma separate list}
\NewDocumentCommand\DefineArray{ mm }{
  \seq_new:c {l_internal_array_#1_seq}% define an array/sequence
  \seq_set_from_clist:cn{l_internal_array_#1_seq}{#2}% set array
}
% Usage: \Array{array name}{index}
\NewDocumentCommand\Array{ mm }{
  #2 $\mapsto$ \seq_item:cn{l_internal_array_#1_seq}{#2}
}
% As clemens says in the comments, for real use the following is better:
% \NewExpandableDocumentCommand\Array{ \seq_item:cn{l_internal_array_#1_seq}{#2} }
\ExplSyntaxOff

\begin{document}

    \DefineArray{myarray}{1,2,15,6,18}

    \Array{myarray}{3},
    \Array{myarray}{1},
    \Array{myarray}{4},
    \Array{myarray}{2}.

\end{document}

The output in this case is:

enter image description here

In particular, these arrays are 1-based. This is using the "sequence" data-type from expl3. See Part VIII of the interface3 manual for more information.

3
  • 1
    The output for the second example is “15, 1, 6, 2.”. In case of the second example you could also use \NewExpandableDocumentCommand\Array because \seq_item:Nn is expandable. This might or might not be useful depending on the use case of the OP
    – cgnieder
    Oct 12 '17 at 8:47
  • @clemens Thanks, I guess I changed the macro after I posted the code - I've fixed up the post to agree with the stated output. I agree, using, `\NewExpandableDocumentCommand` is better.
    – user30471
    Oct 12 '17 at 8:51
  • Although I would argue as soon as it expands to stuff like $\mapsto$ the macro being expandable is dangerous again… :)
    – cgnieder
    Oct 12 '17 at 8:54

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.