2

I want to use the command \num from the siunitx package with some numerical data stored in an array data structure of the arrayjob package but I get an error message that I understand but I can't resolve.

here is an exemple

\documentclass{article}
   \usepackage[T1]{fontenc}
   \usepackage{siunitx,arrayjobx,amsmath}
   \begin{document}
     \newarray\somevalues
     \readarray{somevalues}{1&2&3.5&4&5}
     \begin{align*}
         v_1    &= \somevalues(1)\\
         v_2    &= \somevalues(2)\\
         v_3    &= \somevalues(3)\\
         v_4    &= \somevalues(4)\\
         v_5    &= \somevalues(5)   
     \end{align*}

    \begin{align*}
        v_1 &= \num{\somevalues(1)}\\
        v_2 &= \num{\somevalues(2)}\\
        v_3 &= \num{\somevalues(3)}\\
        v_4 &= \num{\somevalues(4)}\\
        v_5 &= \num{\somevalues(5)} 
    \end{align*}
  • Sorry, but there's no way you can do this with arrayjobx – egreg Mar 30 '16 at 22:38
  • ok!!! is there an other way to store data in a array form ? – Hafid Boukhoulda Mar 30 '16 at 23:01
2

There is no way this can work with arrayjobx, because \num wants to see an explicit number (after full expansion), not a set of instructions to print one.

Here's a reimplementation of \newarray and \readarray with expl3:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{siunitx,amsmath}

\ExplSyntaxOn
\NewDocumentCommand{\newarray}{m}
 {
  \seq_new:c { l_hafid_array_ \cs_to_str:N #1 _seq }
  \cs_new:Npn #1 (##1)
   {
    \seq_item:cn { l_hafid_array_ \cs_to_str:N #1 _seq } { ##1 }
   }
 }
\NewDocumentCommand{\readarray}{mm}
 {
  \seq_set_split:cnn { l_hafid_array_#1_seq } { & } { #2 }
 }
\cs_generate_variant:Nn \seq_set_split:Nnn { c }
\ExplSyntaxOff

\begin{document}

\newarray\somevalues
\readarray{somevalues}{1&2&3.5&4&5}

\begin{align*}
v_1 &= \somevalues(1)\\
v_2 &= \somevalues(2)\\
v_3 &= \somevalues(3)\\
v_4 &= \somevalues(4)\\
v_5 &= \somevalues(5)
\end{align*}

\begin{align*}
v_1 &= \num{\somevalues(1)}\\
v_2 &= \num{\somevalues(2)}\\
v_3 &= \num{\somevalues(3)}\\
v_4 &= \num{\somevalues(4)}\\
v_5 &= \num{\somevalues(5)} 
\end{align*}

\end{document}

enter image description here

3

You can use another package like, ok, let me think, hmm, for example ah yes xinttools.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{xinttools}
\begin{document}
\xintAssignArray {1}{2}{3.5}{4}{5e100}{3.141592653}{2.718281828}\to\somevalues

\begin{align*}
     v_1    &= \somevalues{1}\\
     v_2    &= \somevalues{2}\\
     v_3    &= \somevalues{3}\\
     v_4    &= \somevalues{4}\\
     v_5    &= \somevalues{5}\\
     v_6    &= \somevalues{6}\\
     v_7    &= \somevalues{7}\\
     v_{1+3} &= \somevalues{1+3}\\
     v_{3+2} &= \somevalues{3+2} 
\end{align*}

\begin{align*}
    v_1 &= \num{\somevalues{1}}\\
    v_2 &= \num{\somevalues{2}}\\
    v_3 &= \num{\somevalues{3}}\\
    v_4 &= \num{\somevalues{4}}\\
    v_5 &= \num{\somevalues{5}}\\
     v_6    &= \num{\somevalues{6}}\\
     v_7    &= \num{\somevalues{7}}\\
     v_{1+3} &= \num{\somevalues{1+3}}\\
     v_{3+2} &= \num{\somevalues{3+2}}
\end{align*}
\end{document}

enter image description here

  • However xinttools provides currently (xint 1.2g) nothing else to manipulate arrays like extracting a sub-array, joining arrays, etc... \somevalues{0} returns the number of elements of the array \somevalues as defined by \xintAssignArray {..}...{..}\to \somevalues. The entries are indexed starting at 1. One uses braces, not parentheses. – user4686 Mar 31 '16 at 7:20
2

You can use the \check... mechanism to save the recovered data in a macro, then use that:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{siunitx,arrayjobx,amsmath}
\begin{document}
  \newarray\somevalues
  \readarray{somevalues}{1&2&3.5&4&5}
  \makeatletter
  \newcommand\ArrayNum[1]{%
    \ArrayNumAux#1\stop
  }
  \def\ArrayNumAux#1#2\stop{%
    \csname check\expandafter\@gobble\string#1\endcsname #2%
    \num{\cachedata}%
  }
  \makeatother
  \begin{align*}
      v_1    &= \somevalues(1)\\
      v_2    &= \somevalues(2)\\
      v_3    &= \somevalues(3)\\
      v_4    &= \somevalues(4)\\
      v_5    &= \somevalues(5)   
  \end{align*}
 \begin{align*}
     v_1 &= \ArrayNum{\somevalues(1)}\\
     v_2 &= \ArrayNum{\somevalues(2)}\\
     v_3 &= \ArrayNum{\somevalues(3)}\\
     v_4 &= \ArrayNum{\somevalues(4)}\\
     v_5 &= \ArrayNum{\somevalues(5)} 
 \end{align*}
\end{document}
  • what does the \arrayNumAux#1 command do ??? – Hafid Boukhoulda Apr 3 '16 at 18:50
  • @HafidBoukhoulda It's used to split the argument of \ArrayNum into two parts so that no syntax change is required. – Joseph Wright Apr 3 '16 at 19:34
  • where to find some documentation on it ? – Hafid Boukhoulda Apr 3 '16 at 21:35
  • @HafidBoukhoulda It's just a macro I wrote in the demo above: the technique is a general TeX programming one – Joseph Wright Apr 4 '16 at 6:14

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.