Debian Codegraph @

texmf-dist/doc/latex/spreadtab/spreadtab-en.tex

raw
1 % !TeX TS-program = lualatex
2 % Documentation de l'extension "spreadtab", traduction chatgpt, deepL, chat mistral
3 \documentclass[english,a4paper,10pt]{article}
4 \usepackage[a4paper,margin=2.5cm]{geometry}
5 \usepackage[table]{xcolor}
6
7 \usepackage{%
8 amsmath,amssymb,array,
9 longtable,arydshln,tabularx,
10 xspace,fancyhdr,babel,
11 siunitx,enumitem,spreadtab
12 }
13 \setlist[1]{labelindent=2em}
14 \setlist{
15 leftmargin=!,
16 itemsep=.6ex plus.3ex minus.3ex,
17 parsep=.2ex plus.1ex minus.1ex,
18 topsep=\glueexpr.7ex plus.3ex minus.3ex-\parskip\relax,
19 partopsep=0ex
20 }
21
22 \usepackage[no-math]{fontspec}
23 \usepackage[libertine]{newtxmath}
24 \usepackage{libertineRoman}
25 \setmonofont[Scale=MatchLowercase]{DejaVu Sans Mono}
26
27 \fancyhead[L]{}\fancyhead[C]{}\fancyhead[R]{\scriptsize\slshape \leftmark}
28 \fancyfoot[l]{}\fancyfoot[c]{}\fancyfoot[r]{\thepage}
29 \renewcommand\headrulewidth{0pt}
30
31 \usepackage[bottom]{footmisc}
32 \usepackage[protrusion=true,final,verbose=false,babel=true]{microtype}
33
34 %-- environnement \exemple
35 \makeatletter
36 \newcommand\make@car@active[1]{%
37 \catcode`#1\active
38 \begingroup
39 \lccode`\~`#1\relax
40 \lowercase{\endgroup\def~}%
41 }
42 \newif\if@exstar
43 \newcommand\exemple{%
44 \begingroup
45 \parskip\z@
46 \@makeother\;\@makeother\!\@makeother\?\@makeother\:% neutralise french
47 \@ifstar
48 {\@exstartrue\exemple@}
49 {\@exstarfalse\exemple@}%
50 }
51
52 \newcommand\exemple@[1][65]{%
53 \medbreak
54 \noindent
55 \begingroup
56 \let\do\@makeother\dospecials
57 \make@car@active\ {\space}%
58 \make@car@active\^^M{\par\noexpand\color{black}\leavevmode\noexpand\normalshape}%
59 \make@car@active\^^I{\space\space\space\space}%
60 \make@car@active\,{\leavevmode\kern\z@\string,}%
61 \make@car@active\-{\leavevmode\kern\z@\string-}%
62 \make@car@active\>{\leavevmode\kern\z@\string>}%
63 \make@car@active\<{\leavevmode\kern\z@\string<}%
64 \make@car@active\%{\noexpand\itshape\noexpand\color{gray}\char37 }
65 \exemple@@{#1}%
66 }
67
68 \newcommand\exemple@@[2]{%
69 \def\@tempa##1#2{\exemple@@@{#1}{##1}}%
70 \@tempa
71 }
72
73 \newcommand\exemple@@@[2]{%
74 \xdef\the@code{#2}%
75 \endgroup
76 \vskip0.5ex
77 \fboxsep5pt
78 \fboxrule.4pt
79 \if@exstar
80 \fbox{%
81 \vtop{%
82 \hsize\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax
83 \hbox{%
84 \vtop{%
85 \hsize\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax
86 \parskip0pt
87 \ttfamily
88 \footnotesize
89 \the@code
90 }%
91 }
92 \kern5pt
93 \hrule
94 \kern5pt
95 \hbox{%
96 \vtop{%
97 \hsize\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax
98 \footnotesize
99 \newlinechar`\^^M
100 \everyeof{\noexpand}%
101 \centering
102 \scantokens{#2}%
103 }%
104 }
105 }%
106 }%
107 \else
108 \fbox{%
109 $
110 \vcenter{%
111 \hsize\dimexpr0.#1\linewidth-\fboxsep-\fboxrule\relax
112 \parskip0pt
113 \ttfamily
114 \footnotesize
115 \the@code
116 }%
117 \vcenter{%
118 \hsize\dimexpr\linewidth-0.#1\linewidth-\fboxsep-\fboxrule\relax
119 \footnotesize
120 \newlinechar`\^^M
121 \everyeof{\noexpand}%
122 \scantokens{#2}%
123 }$%
124 }%
125 \fi
126 \medbreak
127 \endgroup
128 }
129
130 %%%%%%%%%%% environnement OOcalc
131 \newcount\cntlin
132 \newcount\cntcol
133 \newcommand*\tabfont{\sffamily\footnotesize}% police du tableau (helvetica ici)
134 \newcommand*\tableheadformat{\bfseries}% police des en-têtes du tableau (en gras)
135 \colorlet{tabheadcolor}{gray!40}% couleur de font des en-têtes du tableau
136 \newtoks\t@b
137 \long\def\ifremain@lines#1\\#2\@nil{%
138 \csname @\ifx\@empty#2\@empty second\else first\fi oftwo\endcsname}
139 \long\def\subst@eol#1\\#2\@nil{\addtot@b{#1\\\cline{1-1}\hdashline}%
140 \ifremain@lines#2\\\@nil{\addtot@b&\subst@eol#2\@nil}{\addtot@b{#2\end{tabular}}}}
141 \long\def\collect@body#1\end{\subst@eol#1\@nil\end}
142
143 \newcommand\addtot@b[1]{\t@b\expandafter{\the\t@b#1}}
144 \newcommand\edftot@b[1]{\edef\temp@{#1}\expandafter\addtot@b\expandafter{\temp@}}
145
146 \newenvironment{OOocalc}[2][1.5cm]{% #1=largeur colonnes #2 = nombre de colonnes
147 \arrayrulewidth0.4pt \dashlinedash0.4pt \relax\dashlinegap3pt
148 \global\cntlin\m@ne\global\cntcol\z@
149 \tabfont
150 \tabcolsep0pt
151 \t@b{\begin{tabular}{%
152 |>{\cellcolor{tabheadcolor}%
153 \global\cntcol\z@\global\advance\cntlin\@ne
154 \centering\arraybackslash
155 \ifnum\cntlin>\z@\tableheadformat\number\cntlin\fi}
156 m{2em}|*{#2}{>{\centering\arraybackslash}m{#1}:}}%
157 \hline
158 \rowcolor{tabheadcolor}}%
159 \loop
160 \ifnum\cntcol<#2
161 \advance\cntcol\@ne
162 \addtot@b{&\multicolumn{1}{>\centering m{#1}|}}%
163 \edftot@b{{\noexpand\tableheadformat\@Alph\cntcol}}%
164 \repeat
165 \addtot@b{\\\hline&}%
166 \collect@body}{\the\t@b}
167
168 \def\engdate{\expandafter\engdatei\STdate\@nil}
169 \def\engdatei#1/#2/#3\@nil{\number#3\relax\space\ifcase #2 \or January\or February\or March\or April\or May\or June\or July\or August\or September\or October\or November\or December\fi{} #1}
170
171 \newif\ifchevrons \chevronstrue
172 \long\def\centerverb#1{%
173 \def\centerverb@i##1#1{##1\smallbreak\egroup}%
174 \bgroup
175 \ttfamily\@noligs
176 \parindent3em
177 \par
178 \smallskip\nobreak
179 \parskip2pt
180 \let\do\@makeother \dospecials
181 \@vobeyspaces
182 \obeylines
183 \make@car@active\^^I{\leavevmode\space\space\space\space}%
184 \ifchevrons
185 \make@car@active\<{\begingroup$\langle$\itshape}%
186 \make@car@active\>{\/$\rangle$\endgroup}%
187 \fi
188 \centerverb@i
189 }
190 \begingroup
191 \catcode`\<13
192 \catcode`\>13
193 \gdef\Verb{%
194 \relax\ifmmode\hbox\else\leavevmode\null\fi
195 \bgroup
196 \verb@eol@error
197 \let\do\@makeother \dospecials
198 \verbatim@font\@noligs
199 \catcode`\<13
200 \catcode`\>13
201 \def<{\begingroup$\langle$\itshape}%
202 \def>{\/$\rangle$\endgroup}%
203 \@ifstar\@sverb\@verb
204 }
205 \endgroup
206 \makeatother
207
208 \newcommand*\ST{\textsf{\STname}\xspace}
209 \newcommand*\xfp{\texttt{l3fp}\xspace}
210 \newcommand*\falseverb[1]{\texttt{\detokenize{#1}}}
211 \newcommand*\NEW{\marginpar{\vspace{-1.5ex}\itshape\color{red}New v\STver}}
212 \newcommand*\chevrons[1]{\textlangle\textit{#1}\textrangle}
213 \newcommand*\STkey[1]{{\color{teal}\texttt{\detokenize{#1}}}}
214 \newcommand*\STval[2][\itshape]{{\color{teal}\textlangle{\ttfamily#1#2}\textrangle}\if\relax\detokenize{#2}\relax\quad(empty)\fi}
215 \newcommand*\STkv[2]{\STkey{#1}{\color{teal}${}={}$}\STval{#2}}
216 \newcommand*\STmacro[1]{\texttt{#1}}
217
218 \usepackage[bookmarks=true,bookmarksopen=true,colorlinks=true,hyperfootnotes=false,filecolor=black,linkcolor=blue,urlcolor=magenta,pdfauthor={Christian TELLECHEA},pdftitle={spreadtab},pdfsubject={Spreadtab provides spreadsheet features for LaTeX table environments},pdfkeywords={spreadtab},pdfcreator={LaTeX}]{hyperref}
219
220 \begin{document}
221 \parindent0pt
222 \pagestyle{fancy}
223 \begin{titlepage}
224 \null\par\vfill
225 \begin{center}
226 \begin{minipage}{0.75\linewidth}
227 \begin{center}
228 \Huge\bfseries\ST\par\vspace{5pt}
229 \small v\STver\par\vspace{35pt}
230 \normalsize User's manual
231 \end{center}
232 \end{minipage}
233 \end{center}
234 \vspace{1cm}
235 \begin{center}
236 Christian {\sc Tellechea}\par\small
237 \href{mailto:[email protected]}{\texttt{\textbf{[email protected]}}}\par\vspace{5pt}
238 \engdate
239 \end{center}
240 \vfill
241 \begin{center}
242 \begin{minipage}{0.8\linewidth}
243 \noindent\hrulefill\par
244 \hfill\textbf{\textit{Résumé}}\hfill{}\medskip\par\footnotesize
245 This package allows to use spreadsheet features in any "table" environment with \LaTeX{}.\par\smallskip
246 The main functionality is being able to write formulas in the cells of a table that refer to other cells, calculate the formulas contained in the cells and display the numerical results of these formulas in the table.\par
247 \hrulefill
248 \end{minipage}
249 \end{center}
250 \vfill{}
251 \end{titlepage}
252
253 \tableofcontents\newpage
254 \parskip\medskipamount
255 \section{Presentation}
256 This manual\footnote{What you are reading is only a translation from French done by AIs (DeepL, ChatGPT, and Mistral Chat). I have barely reviewed it, so there may be errors or inconsistencies. For \emph{reliable} information, please refer to the French manual.} describes the \ST package. It lets you build spreadsheet-like tables. Table cells have coordinates (column and row) which can be used in formulas to calculate values in other cells. To load the package, write in the preamble
257 \centerverb|\usepackage{spreadtab}|
258
259 This package requires the \LaTeXe{} format after 2022/06/01 as well as the \href{https://ctan.org/pkg/xstring}{\texttt{xstring}} and \href{https://ctan.org/pkg/simplekv}{\texttt{simplekv}} packages.
260
261 This package is intended to be compatible with all table environments, provided that column separators are "\verb=&=" and line breaks are "\verb=\\=". \ST acts completely independently of the table environment. This means that reading the table, processing and calculating formulas takes place before the table environment takes over and "sees" the body of the table.
262
263 Therefore, \ST proceeds in 3 steps:
264 \begin{itemize}
265 \item First, \ST reads the body of the array. If the macro \verb|\STxp| is present one or more times, its argument is expanded to the maximum. The body of the table is then divided into rows and cells;
266 \item the formulas contained in the cells are then calculated, having first calculated the dependent cells;
267 \item the body of the table is finally reconstructed, having replaced each formula with the numerical value previously calculated, and the whole is given to the table environment specified by the user.
268 \end{itemize}
269
270 The syntax is:
271 \centerverb/\begin{spreadtab}[<options>]{{<tabname>}{<colspec>}}
272 table with formulas and numbers
273 \end{spreadtab}/
274
275 where \Verb-<tabname>- represents the name of any array environment available with \LaTeX{} or with a package. You can replace \verb|\begin{spreadtab}...\end{spreadtab}| with \verb|\spreadtab...\endspreadtab|.
276
277 The \Verb|<options>| must be in the form \STkv{key}{value} to apply only to the current array. To specify \Verb|<options>| valid for all future arrays, use
278 \centerverb|\STset{<key>=<value>}|
279 The list of \STkey{keys} and associated \STval{values} is available on page~\pageref{key/val}.
280
281 Although the spreadsheet-like functionality of \LaTeX{} is appreciable, you should not lose sight of the fact that the 3 steps described above are time-consuming. As a result, compilation times are much longer than with a conventional table.
282
283 \section{New in v0.61}
284 \subsection{Deletion}
285 The macro functions \STmacro{ifeq}, \STmacro{iflt} and \STmacro{ilgt} have been removed, since \xfp provides the ternary operator
286 \centerverb|<test>?<true>:<false>|
287
288 The following macros, available in previous versions, have been removed: \verb|\STautoround|, \verb|\STsetdecimalsep|, \verb|\STeol|, \verb|\STsetdisplaymarks|, \verb|\STmessage|, \verb|\STnumericfieldmarker|, \verb|\STtextcell| and \verb|\STtranposechar|. All settings are now made via \Verb|\STset{<keys>=<values>}|.
289
290 \subsection{Macro \texttt{\textbackslash STrep}}
291 See page~\pageref{STrep}.
292
293 \subsection{Macro-function \STmacro{test}}
294 See page~\pageref{test}.
295
296 \section{Common Features}
297 \subsection{Calculation}
298 A cell can now contain any calculation understood by \xfp: basic operations, trigonometry and inverses, exponentials and logarithms, comparisons, logical connectors\footnote{If you wish to use the logical connector "and" in \xfp, denoted by "\texttt{\string&\string&}", it is \emph{mandatory} to enclose this operator in braces within a table to prevent the \texttt{\string&} tokens from being interpreted as column separators. In a table cell, for example, to check if the content of the cell \texttt{a1} belongs to an interval, you would write: "\texttt{a1>1 \string{\string&\string&\string} a1<10}".}, factorials, random numbers, etc. See the documentation for \href{http://mirrors.ctan.org/macros/latex/required/l3kernel/interface3.pdf}{\texttt{interface3}}.
299
300 As a result, the macro-functions \STmacro{rand}, \STmacro{rnd}, and \STmacro{fact} are still usable but are \emph{no longer} part of \ST: they are now directly understood by \xfp.
301
302 \subsection{Line Breaks}
303 By default, \ST considers a line to end with \verb-\\-, which is standard in tables. The optional argument \Verb-[<dimension>]- that follows is taken into account. This line-break marker can be modified using the key \STkey{tabline sep}\label{tabline sep}. For example, you can write:
304 \centerverb|tabline sep = \tabularnewline|
305
306 The line breaks that will be inserted into the final table will always be "\verb-\\-", even if the line break marker was modified when \ST \emph{reads} the table.
307
308 Just after the line break, \ST recognizes most horizontal rules as well as their optional arguments: \verb|\hline|, \verb|\cline|, \verb|\hhline|, \verb|\noalign|, \verb|\toprule|, \verb|\midrule|, \verb|\bottomrule|, \verb|\cmidrule|, \verb|\addlinespace|, \verb|\morecmidrules|, and \verb|\specialrule|.
309
310 For other cases, see page~\pageref{filets horizontaux} for further instructions.
311
312 \subsection{Absolute References}
313 In the table, cells are identified by their absolute references as follows:
314 \begin{itemize}
315 \item the column is represented by a letter, either uppercase or lowercase, from \falseverb a to \falseverb z, where \falseverb a represents the first column on the left: we are thus initially limited to 26 columns, which should be sufficient for the vast majority of cases;
316 \item immediately following the letter, a strictly positive integer represents the row number, with row number 1 being the topmost row.
317 \end{itemize}
318
319 An absolute reference is thus written, for example: "\falseverb{b4}", "\falseverb{C1}", or "\falseverb{d13}". Visually, this can be illustrated by a table similar to a spreadsheet, here intentionally limited to 5 rows and 5 columns:
320 \begin{center}
321 \begin{OOocalc}[6em]{5}
322 &&&&\\&&&&\\&&&&\\&&&&\\&&&&\\
323 \end{OOocalc}
324 \end{center}
325 In this example, we calculate the sum of each row and column, and then the total sum:
326 \exemple/\begin{spreadtab}{{tabular}{rr|r}}
327 22 & 54 & a1+b1 \\
328 43 & 65 & a2+b2 \\
329 49 & 37 & a3+b3 \\
330 \hline
331 a1+a2+a3 & b1+b2+b3 & a4+b4
332 \end{spreadtab}/
333
334 In this other example, we calculate a few lines of Pascal's triangle:
335 \exemple/\begin{spreadtab}{{tabular}{ccccc}}
336 1 & & & & \\
337 a1 & a1 & & & \\
338 a2 & a2+b2 & b2 & & \\
339 a3 & a3+b3 & b3+c3 & c3 & \\
340 a2 & a4+b4 & b4+c4 & c4+d4 & d4
341 \end{spreadtab}/
342
343 \subsection{Relative references}
344 To reference a cell, you can specify its position relative to the cell containing the formula. Thus, the "relative" coordinates of a cell are 2 relative numbers written according to this syntax: \Verb|[<x>,<y>]|, where \Verb|<x>| is the horizontal offset from the cell containing the formula and \Verb|<y>| is the vertical offset. Thus, \falseverb{[-2,3]} refers to the cell 2 columns before (left) and 3 rows after (lower) the cell containing the formula.
345
346 Here again is the Pascal triangle seen above, but the references are relative and the "\verb-matrix-" environment of the \href{https://ctan.org/pkg/amsmath}{\texttt{amsmath}} package is used:
347 \exemple/$
348 \begin{spreadtab}{{matrix}{}}
349 1\\
350 [0,-1] & [-1,-1]\\
351 [0,-1] & [-1,-1]+[0,-1] & [-1,-1]\\
352 [0,-1] & [-1,-1]+[0,-1] & [-1,-1]+[0,-1] & [-1,-1]\\
353 [0,-1] & [-1,-1]+[0,-1] & [-1,-1]+[0,-1] & [-1,-1]+[0,-1] & [-1,-1]
354 \end{spreadtab}
355 $/
356
357 A mixture of absolute and relative references can be used in the same formula.
358
359 \subsection{Text cells}
360 If you want to put text in a cell, simply place the "\falseverb @" character somewhere in the cell. In doing so, the cell is ignored by \ST and becomes an inert cell that cannot be referred to anywhere else in the table.
361
362 The key \STkey{text mark}\label{text mark} is used to modify the text marker. For example
363 \centerverb/\STset{text mark=`}/
364 will cause a cell containing the character "\verb=`=" to be understood as a text cell. It's important to note, however, that the \STkey{text mark} key detokenizes its value, making the text marker made up of token(s) of catcode 12: to be recognized by \ST, these tokens must have this catcode 12 at the time the array is read.
365 \exemple/\begin{spreadtab}{{tabular}{|r|ccc|}}\hline
366 @ values of $x$ & -5 & -1 & 4 \\
367 @ $f(x)=2x$ & \STcopy>{2*[0,-1]} & & \\\hline
368 \end{spreadtab}\medbreak
369 \STset{text mark=`}
370 \begin{spreadtab}{{tabular}{|r|ccc|}}\hline
371 `values of $x$ & -5 & -1 & 4 \\
372 `$f(x)=2x$ & \STcopy>{2*[0,-1]} & & \\\hline
373 \end{spreadtab}/
374
375 If a cell is empty or made up entirely of spaces, then \ST will treat it as a text cell.
376
377 \subsection{Mixed Cells}
378 In reality, each cell consists of \emph{two} fields. On one side, the \chevrons{numeric field} contains the formula, and on the other side, the \chevrons{text field} will be ignored by the calculation engine and does not contribute to the calculations:
379 \begin{itemize}
380 \item In a cell, if nothing is specified, the entire cell is considered the \chevrons{numeric field}, and the \chevrons{text field} is empty;
381 \item If the cell contains "\falseverb{@}", then the entire cell is considered the \chevrons{text field}. The \chevrons{numeric field} is empty and inaccessible.
382 \item If the cell contains "\verb-:=-", then the argument in curly braces that follows is the \chevrons{numeric field}, and everything else is the \chevrons{text field}. The cell has this structure:
383 \centerverb|<text field>:={<numeric field>}<text field>|
384 With the key \STkey{numeric mark}\label{numeric mark}, you can change this marker to "\verb-\=-" with:
385 \centerverb|numeric mark={\=}|
386 In this case, the definition of \verb-\=- would have no importance and would not intervene in the process: for \ST, it is simply a marker for the start of the formula that is searched for and recognized without being expanded.
387 \end{itemize}
388 Once the \chevrons{numeric field} is calculated, only it and the marker "\verb-:=-" will be replaced by the calculated numeric value.
389
390 It is worth noting that the \chevrons{numeric field} can be inside curly braces.
391
392 To illustrate the idea, here is a very simple example:
393 \exemple!\begin{spreadtab}{{tabular}{|c|c||c|}}\hline
394 val 1: :={50} & val 2: :={29} & mean: \textbf{:={(a1+b1)/2}}\\\hline
395 \end{spreadtab}!
396
397 Note also that "\verb-:={}-", which defines an empty formula, has the same effect as "\verb-@-" in a cell: the cell is understood as a text cell. However, "\verb-@-" and "\verb-:={}-" \emph{are not equivalent} because a text cell containing the latter can receive a formula by copying (see the next section), which is impossible with "\verb-@-".
398
399 \subsection{Copying formulas}
400 To avoid having to re-enter identical formulas in adjacent cells, \ST provides the \verb-\STcopy- instruction.
401
402 This command is placed in a cell using the syntax \verb|\STcopy{>|\Verb|<x>,v<y>}{<formula>}|, where \Verb|<x>| and \Verb|<y>| are positive numbers representing horizontal and vertical offsets from the cell containing the instruction. The cell containing the command and the cell obtained by these offsets define a rectangular range of cells that will receive the \Verb-<formula>-\footnote{The copy can therefore only be made to cells that are to the right and below the cell where the command is located.}. The \verb-\STcopy- \emph{must not} be in a cell where a numeric field marker "\verb-:=-" is present.
403
404 For each target cell, all references in the formula are incremented to account for the offset between the target cell and the source cell. The formula may also contain relative references, which, being relative, will not be modified.
405
406 By preceding a coordinate reference with a "\verb-!-" character, this coordinate remains unchanged during the copy. The character "\verb-!-" can be changed with \STkv{freeze char}{character}\label{freeze char}. It is important to note that the \STval{character} is detokenized.
407
408 In the syntax \verb|\STcopy{>|\Verb|<x>,v<y>}{<formula>}|, if the number \Verb|<x>| is omitted, the copy in the horizontal direction is made to the right up to the right edge of the table. If \Verb|<y>| is omitted, the copy in the vertical direction is made down to the bottom of the table.
409
410 This allows generating the multiplication table from 1 to 10:
411 \exemple*?\begin{spreadtab}{{tabular}{|c|*{10}{c}|}}
412 \hline
413 @$\times$ & 1 & \STcopy{>}{b1+1} & & & & & & & & \\\hline
414 1 & \STcopy{>,v}{!a2*b!1} & & & & & & & & & \\
415 \STcopy{v}{a2+1} & & & & & & & & & & \\
416 & & & & & & & & & & \\
417 & & & & & & & & & & \\
418 & & & & & & & & & & \\
419 & & & & & & & & & & \\
420 & & & & & & & & & & \\
421 & & & & & & & & & & \\
422 & & & & & & & & & & \\
423 & & & & & & & & & & \\\hline
424 \end{spreadtab}?
425
426 When copying a formula, if the target cell already contains a non-empty numeric field, this is not overwritten and the copy is not made.
427
428 \subsection{Macros provided by \ST}
429 \subsubsection{Macro \texttt{\textbackslash STset}}
430 The \verb|\STset| macro includes in its mandatory argument a csv of \STkv{keys}{values} to specify settings that operate for the rest of the document.
431
432 The list of \STkey{keys} and associated \STval{values} is available on the~\pageref{key/val} page.
433
434 \subsubsection{The macros \texttt{\textbackslash STxp} and \texttt{\textbackslash STrep}}
435 The presence\footnote{\ST tests its \emph{presence} and then acts on its argument: \texttt{\textbackslash STxp} takes the place of a marker and is not executed when the array is read.} of \verb|\STxp{<argument>}| in the body of the table has the effect of fulle xpansion of the \Verb|<argument>|. If one or more parts of the \verb|<argument>| are not expandable (e.g. non-robust macros), it's up to the user to block full expansion using, for example, the \verb|\noexpand| or \verb|\unexpanded| primitives. The \verb|\STxp| macro must not be enclosed in braces: it will be ignored and executed much later, causing a compilation error.
436 \bigbreak
437
438 The expandable macro \verb|\STrep|\label{STrep} replicates \Verb| | times an arbitrary \Verb|<code>|:
439 \centerverb!\STrep{<n>}{<code>}!
440
441 Used in the argument of \verb|\STxp|, it allows you to generate empty cells or identical lines in a concise way.
442 \exemple*/\begin{spreadtab}{{tabular}{|c|*{10}{c}|}}
443 \hline
444 @$\times$ & 1 & \STcopy{>}{b1+1} \STxp{\STrep8&}\\\hline
445 1 & \STcopy{>,v}{!a2*b!1} \STxp{\STrep9&}\\
446 \STcopy{v}{a2+1} \STxp{\STrep9{\STrep{10}&\\}}
447 \hline
448 \end{spreadtab}/
449
450 If one or more parts of the \verb|<code>| cannot be expanded (non-robust macros in particular), it is up to the user to stop the full expansion initiated by \verb|\STxp| with, for example, the \verb|\noexpand| or \verb|\unexpanded| primitives.
451
452 \section{Table formatting}
453 \subsection{Codes executed before or after the table}
454 The \STkv{pretab code}{code}\label{pretab code} and \STkv{posttab code}{code}\label{posttab code} keys define arbitrary codes executed just before and just after table display.
455 \exemple!A simple
456 \begin{spreadtab}[pretab code=\begingroup\color{red},
457 posttab code=\endgroup]{{tabular}[t]{r}}
458 149\\
459 287\\\hline
460 a1+a2
461 \end{spreadtab} addition.!
462
463 \subsection{Decimal Separator}
464 Since \xfp gives decimal results with the dot as the decimal separator, it is possible to change this decimal separator so that all results are returned as if they accounted for it.
465
466 The key \STkv{dec sep}{char} allows changing the decimal separator to a comma, but this choice is not neutral in math mode. In fact, it is considered punctuation, which explains why it is followed by a space.
467
468 To avoid this typographic error, you can:
469 \begin{itemize}
470 \item define the decimal separator in curly braces with \verb|dec sep={{,}}|\label{dec sep}
471 \item execute the code \verb|\mathcode`,="013B\relax| before displaying the table
472 \end{itemize}
473 The most elegant solution is to use a configurable package specialized in number formatting, such as \href{http://mirrors.ctan.org/macros/latex/contrib/siunitx/siunitx.pdf}{\texttt{siunitx}}.
474
475 \exemple[70]!\begin{spreadtab}[dec sep={,}]{{tabular}{|*3{>{$}r<{$}}|}}\hline
476 @x & @y & @\text{Mean}\\\hline
477 5 & -4 & (a2+b2)/2\\
478 -6.1 & -8 & (a3+b3)/2\\
479 9.85 & 3.7 & (a4+b4)/2\\\hline
480 \end{spreadtab}\par\smallskip
481 \begin{spreadtab}[dec sep={{,}}]{{tabular}{|*3{>{$}r<{$}}|}}\hline
482 @x & @y & @\text{Mean}\\\hline
483 5 & -4 & (a2+b2)/2\\
484 -6.1 & -8 & (a3+b3)/2\\
485 9.85 & 3.7 & (a4+b4)/2\\\hline
486 \end{spreadtab}\par\smallskip
487 \begin{spreadtab}[dec sep ={,},
488 pretab code ={\begingroup\mathcode`\,="013B\relax},
489 posttab code={\endgroup}]{{tabular}{|*3{>{$}r<{$}}|}}\hline
490 @x & @y & @\text{Mean}\\\hline
491 5 & -4 & (a2+b2)/2\\
492 -6.1 & -8 & (a3+b3)/2\\
493 9.85 & 3.7 & (a4+b4)/2\\\hline
494 \end{spreadtab}!
495
496 \subsection{Automatic rounding}
497 The \xfp engine gives up to 16 significant decimals. If you don't want such precision, you can automatically round off the result of each calculation with the \STkey{autoround}\label{autoround} key. Acting on this key implies a \emph{deliberate limitation} of the precision of internal calculations.
498 \exemple*!\begin{spreadtab}{{tabular}{*4c}} \hline
499 @$x$ & 3 & 17 & 751 \\\hline
500 @$1/x$ & \STcopy>{1/b1} & & \\\hline
501 @$x\cdot(1/x)$ & \STcopy>{b2*b1} & & \\\hline
502 \end{spreadtab}
503 \bigbreak
504 \begin{spreadtab}[autoround=6]{{tabular}{*4c}} \hline
505 @$x$ & 3 & 17 & 751 \\\hline
506 @$1/x$ & \STcopy>{1/b1} & & \\\hline
507 @$x\cdot(1/x)$ & \STcopy>{b2*b1} & & \\\hline
508 \end{spreadtab}!
509
510 To return to the default behavior and do no rounding, set an empty value:
511 \centerverb|autoround={}|
512
513 \subsection{Macro \texttt{\textbackslash STprintnum}}
514 Each number processed by \ST is given to the \verb|\STprintnum| macro in the final array. By default, this macro does not modify any of its arguments, and its definition is as follows:
515 \centerverb|\def\STprintnum#1{#1}|
516
517 If you wish, you can modify using the \href{http://mirrors.ctan.org/macros/latex/contrib/siunitx/siunitx.pdf}{\texttt{siunitx}} package and its \verb|\num| macro:
518 \exemple[60]!\def\STprintnum#1{\num[round-mode = places,
519 round-precision = 6,
520 drop-zero-decimal=true,
521 output-decimal-marker = {,}]{#1}%
522 }
523 \begin{spreadtab}{{tabular}{*4c}} \hline
524 @$x$ & 3 & 17 & 751 \\\hline
525 @$1/x$ & \STcopy>{1/b1} & & \\\hline
526 @$x\cdot(1/x)$ & \STcopy>{b1*b2} & & \\\hline
527 \end{spreadtab}!
528 In this case, no change is made to the accuracy of internal calculations.
529
530 \subsection{Hide row or column}
531 Sometimes, an entire row or column is used for intermediate calculations that don't need to be displayed in the final table. For this purpose, \verb=\SThiderow= and \verb=\SThidecol= control sequences are available which, when placed in a cell, hide the row or column in which the cell is located.
532 \exemple*!\begin{spreadtab}{{tabular}{|r|ccc|}} \hline
533 @ values of $x$ & -1 & 0\SThidecol & 2 & 3 \\\hline
534 @$f(x)=2x-1$ & 2*[0,-1]-1 & 2*[0,-1]-1 & 2*[0,-1]-1 & 2*[0,-1]-1 \\
535 @$g(x)=x-10$\SThiderow & [0,-2]-10 & [0,-2]-10 & [0,-2]-10 & [0,-2]-10 \\
536 @$h(x)=1-x$ & 1-[0,-3] & 1-[0,-3] & 1-[0,-3] & 1-[0,-3] \\\hline
537 \end{spreadtab}!
538
539 You can see how the row containing $g(x)$ and the column corresponding to the value 0 are hidden.
540
541 Remember that masked rows and columns are \emph{invisible} for the user's chosen array environment, which explains why in the array preamble only 4 columns (\falseverb{|r|ccc|}) have been defined and not 5 as shown.
542
543 \subsection{Export cell value}
544 You may need to save the numerical value of a cell in a macro (the assignment is global) to display it in a text field\footnote{It's easier to use \texttt{<\kern0pt<} and \texttt{>\kern0pt>}, see next section.} or even outside the table. In this case, use the key \STkey{save list}\label{save list}:
545 \centerverb|save list={<macroA> = <refA>, <macroB> = <refB>,...}|
546
547 where each reference must be absolute.
548 \exemple!\STset{ save list = { \resultC = c1 , \resultB = b1 }}
549 \begin{spreadtab}{{tabular}{|c|c|c|c|c|}}\hline
550 10 & a1+10 & b1+10 & a1+b1+c1 & @cell c1: \resultC\\\hline
551 \end{spreadtab}
552 \par\medskip
553 Here is cell c1: \resultC\ and b1: \resultB!
554
555 When the key \STkey{aux save list}\label{aux save list} is used with the same syntax, everything happens as before, but the assignment is also written to the auxiliary file. In this way, the value of a cell can be transmitted between compilations, so that it can be referred to before the array is encountered.
556 \exemple!Valeur of d1 is \cellD.\par
557 \STset{ aux save list = { \cellD = d1 }}
558 \begin{spreadtab}{{tabular}{|c|c|c|c|}}\hline
559 1 & a1+10 & b1+10 & a1+b1+c1\\\hline
560 \end{spreadtab}!
561
562 In both cases, the value of the \STkey{save cell} and \STkey{aux save cell} keys is reset to empty after the table following \verb|\STset| is displayed.
563
564 It is also possible, using the macro function \STmacro{tag}, to export a value to the auxiliary file, see page~\pageref{tag}.
565
566 \subsection{Display cell value}
567 To simply display the value of a cell's numeric field in a text field, you can use the syntax \verb-<<-\Verb-<reference>-\verb->>- which will be replaced by the cell's numeric value \Verb-<reference>- where \Verb-<reference>- can be relative or absolute. If what's between \verb-<<- and \verb->>- is not a reference, then nothing is done. The \verb-<reference>- must not contain any spaces. If you write \verb-<< a1>>- then the space means that the reference is not recognized.
568 \exemple!\begin{spreadtab}{{tabular}{|c|c||c|}}\hline
569 23 & 32 & Moy $= \frac{<<a1>>+<<b1>>}{2} = :={(a1+b1)/2}$\\\hline
570 \end{spreadtab}!
571
572 Reference delimiter characters are set to \verb-<<- and \verb->>- by default. They can be modified with the key \STkey{display marks}\label{display marks}. If you write \verb-display marks={|;|}-, you'll have to write \verb-|<reference>|-:
573 \exemple!\begin{spreadtab}[display marks={|;|}]{{tabular}{|c|c||c|}}\hline
574 23 & 32 & Mean $= \frac{|a1|+|b1|}{2} = :={(a1+b1)/2}$\\\hline
575 \end{spreadtab}!
576
577 \subsection{Use \ttfamily\textbackslash multicolumn}
578 The \ST package supports the \Verb=\multicolumn{<number>}{<type>}{<content>}= syntax, which merges \Verb-<number>- cells into a cell of the type and content specified in the arguments.
579
580 By using \verb=\multicolumn=, \ST even maintains consistency in cell referencing. On this table where cells are merged, the table cells contain the absolute references seen by \ST:
581 \begin{center}
582 \ttfamily
583 \begin{tabular}{|*7{c|}}\hline
584 a1&b1&c1&d1&e1&f1&g1\\\hline
585 a2&\multicolumn{2}{l|}{b2}&d2&e2&f2&g2\\\hline
586 \multicolumn{3}{|l|}{a3}&d3&\multicolumn{2}{l|}{e3}&g3\\\hline
587 \end{tabular}
588 \end{center}
589 So, whatever the number of cells merged, the next cell has a column number that takes into account the number of cells merged.
590
591 On the last line, cells \falseverb{a3}, \falseverb{b3} and \falseverb{c3} are merged, and if cell \falseverb{a3 } contains a formula, cells \falseverb{b3} and \falseverb{c3} \emph{doesn't exist} for \ST: these cells can't be referred to anywhere.
592
593 Here's an example where each number on the top line is the product of the 2 numbers below it:
594 \exemple*/\newcolumntype{K}[1]{@{}>{\centering\arraybackslash}p{#1cm}@{}}
595 \begin{spreadtab}{{tabular}{*6{K{0.5}}}}
596 \cline{2-5}
597 &\multicolumn{2}{|K{1}|}{:={a2*c2}} & \multicolumn{2}{K{1}|}{:={c2*e2}} &\\\hline
598 \multicolumn{2}{|K{1}}{:=8}&\multicolumn{2}{|K{1}}{:=7}&\multicolumn{2}{|K{1}|}{:=6}\\\hline
599 \end{spreadtab}/
600 Note that the numeric field marker "\falseverb{:=}" is required in every cell containing the command \verb-\multicolumn-. If this marker didn't exist, the entire cell - i.e. \Verb-\multicolumn{2}{|c|}{<formula>}- would be considered a formula.
601
602 \section{Macro-functions}
603 \subsection{Math macro-functions}
604 \subsubsection{Sum cells}
605 The \STmacro{sum} function is used to sum one or more cell ranges.
606
607 Its syntax is \Verb=sum(<range 1>;<range 2>;...;<range n>)=, where a cell range is:
608 \begin{itemize}
609 \item either an isolated cell such as "\falseverb{a1}" or "\falseverb{[2,1]}";
610 \item either a rectangular area delimited by the upper left and lower right cell in this way \Verb=<cell 1>:<cell 2>=.
611 \end{itemize}
612
613 In cell ranges, empty cells or cells containing only text are considered to contain the number 0. The same applies to cells masked by \verb-\multicolumn-.
614
615 Relative and absolute references can be used together, as the user sees fit. Here's an example of summing Pascal's triangle coefficients:
616 \exemple/\begin{spreadtab}{{tabular}{*5c}}
617 \multicolumn{5}{c}{sum=:={sum(a2:e6)}}\\
618 [0,1] & [-1,1]+[0,1] & [-1,1]+[0,1] & [-1,1]+[0,1] & [-1,1]\\
619 [0,1] & [-1,1]+[0,1] & [-1,1]+[0,1] & [-1,1] & \\
620 [0,1] & [-1,1]+[0,1] & [-1,1] & & \\
621 [0,1] & [-1,1] & & & \\
622 1 & & & &
623 \end{spreadtab}/
624
625 \subsubsection{Macro-function \ttfamily sumprod}
626 The \STmacro{sumprod} function multiplies the corresponding elements of 2 or more ranges and then adds these products together.
627
628 Its syntax is: \Verb|sumprod(<range 1>;<range 2>;...;<range n>)|. All rectangular ranges must have the same dimensions.
629
630 Here's a simple example of calculating the average age of a group of children aged between 10 and 15:
631 \exemple*!\begin{spreadtab}{{tabular}{r*6c}}
632 @Ages & 10 & 11 & 12 & 13 & 14 & 15\\
633 @Number & 5 & 8 & 20 & 55 & 9 & 3 \\\hline
634 \multicolumn{7}{c}{Mean = :={sumprod(b1:g1;b2:g2)/sum(b2:g2)}}
635 \end{spreadtab}!
636 As with the \verb-sum- macro function, text cells, whether empty or merged by a \verb-\multicolumn-, are considered to contain the number 0.
637
638 \subsubsection{GCD and LCM}
639 The macro functions \STmacro{gcd} and \STmacro{lcm} calculate the Greatest Common Divisor (GCD) and the Least Common Multiple (LCMM) of numbers passed as arguments and separated by commas:
640 \centerverb|gcd(<nombre1>,<nombre2>,...,<nombreN>)
641 lcm(<nombre1>,<nombre2>,...,<nombreN>)|
642
643 \exemple*!\begin{spreadtab}{{tabular}{|r|r|r||c|c|}}\hline
644 \multicolumn3{|c||}{@Numbers}& @GCD & @LCM \\\hline
645 24 & 18 & 12 & \STcopy{v}{gcd(a2,b2,c2)} & \STcopy{v}{lcm(a2,b2,c2)}\\
646 15 & 10 & 25 & & \\
647 16 & 12 & 15 & & \\\hline
648 \end{spreadtab}!
649
650 \subsubsection{Scientific Notation}
651 The macro function \STmacro{scitodec} allows converting a number written in scientific notation to a decimal number. The syntax is \Verb-scitodec(<text>)-, where \Verb-<text>- is:
652 \begin{enumerate}
653 \item a sequence of characters in the syntax \Verb-<mantissa>EE<exponent>-, where the \Verb-<mantissa>- is a decimal number and the \Verb-<exponent>- is an integer. The "E"s can be uppercase or lowercase;
654 \item a reference to the \emph{text} field of a cell, which must contain characters following the syntax described in the previous point.
655 \end{enumerate}
656
657 \exemple[60]!\begin{spreadtab}{{tabular}{|r|r|}}\hline
658 @Scientific notations & @Decimal \\\hline
659 @4EE2 & \STcopy{v}{scitodec([-1,0])}\\
660 @-3.1EE-3 & \\
661 @15ee5 & \\
662 @-0.025ee7 & \\
663 @2.125EE0 & \\
664 @3.1575EE-4 & \\\hline
665 \end{spreadtab}!
666
667 The \xfp engine natively understands numbers written in scientific notation in the form \Verb|<a>e<b>| but using this syntax is \emph{impossible} to use with \ST because the number \verb|4e3|, which is 4000, would be understood by \ST as 4 followed by the content of the cell \verb|e3|.
668
669 \subsubsection{Identity}
670 The simplest macro function is \STmacro{id}, since \Verb=id(<number>)= returns the \Verb-<number>- enclosed in brackets. Mathematically, it's of little use, but with \ST, it lets you pass mathematical expressions to macros that don't admit them. For example, it can be used in the cell ranges of \STmacro{sum}.
671
672 In the code below, the \STmacro{id} macro is used to determine the cell range to be added with \STmacro{sum}. In this example, the value of the numeric field in cell \falseverb{a2} is 8. Therefore, \verb=sum([0,-1]:[id(a2-1),-1])/a2= is equivalent to \verb=sum([0,-1]:[7,-1])/8=:
673 \exemple*!\begin{spreadtab}{{tabular}{r*{10}c}}
674 @Int from 1 to 10 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10\\
675 Mean of :={8} first int & sum([0,-1]:[id(a2-1),-1])/a2 \\
676 \end{spreadtab}!
677
678 \subsection{Comparison macro-functions}
679 There are no more comparison macros since the \xfp engine has a ternary operator \Verb|<a>?<b>:<c>|. If the test \Verb|<a>| is true, \Verb|<b>| is returned; otherwise, \Verb|<c>| is returned.
680
681 On the other hand, \ST offers the macro function \STmacro{test}\label{test} which has a special syntax with 2 arguments:
682 \centerverb|test(csv of <tests>)<optional spaces>(csv of <texts>)|
683
684 The \Verb|<tests>| are performed from left to right and the first one that is \verb|true|, the corresponding \Verb|<text>| is returned. If there are $n$ \Verb|<test>|, the \Verb|<text>| of index $n+1$ will be returned if all the tests are false.
685
686 If the number of \Verb|<texts>| is insufficient compared to the number of \Verb|<tests>|, the missing \Verb|<texts>| are considered empty.
687
688 \exemple!\begin{spreadtab}{{tabular}{c|c}}
689 \hline
690 @Month & @Season \\\hline
691 1 & \STcopy v{test("a2=1||a2>10 , a2<5 , a2<8")
692 (Winter,Spring,Summer,Fall)} \\
693 \STcopy v{a2+1} \STxp{\STrep{11}{& \\}}% 11 empty lines
694 \hline
695 \end{spreadtab}!
696
697 The part of the formula to be copied between the characters ''\verb|"|'' indicates which part should be taken into account for the offset when copying. It can be modified with the key \STkey{copy char}\label{copy char}.
698
699 If you wish to use the logical connector \verb|&&|, it is \emph{necessary} to enclose it in braces. In this way, the cell to be copied could contain the following equivalent tests\chevronsfalse
700 \centerverb|\STcopy v{test("a2>2{&&}a2<6,a2>5{&&}a2<9,a2>8{&&}a2<12")
701 (Spring,Summer,Fall,Winter)}|
702 \chevronstrue
703
704 \subsection{Macro-functions manipulating dates}
705 \subsubsection{Date to number with \ttfamily engshortdatetonum}\label{datetonum}
706 The macro \verb-engshortdatetonum- converts a short date like 1789/7/14 to an integer which is the number of days passed since the 1st March of the year 0. It is important to note that this macro-function requires a \emph{textual} argument and not a number or the result of a mathematical calculation. Therefore, if the argument of this macro-function refers to a cell, that cell \emph{must} be a text cell, i.e. a cell containing `\verb-@-' or `\verb-:={}-'.
707 \exemple!\begin{spreadtab}{{tabular}{cc}}
708 @1789/7/14 & engshortdatetonum(a1)\\
709 2001/1/1 :={} & engshortdatetonum(a2)\\\hline
710 \end{spreadtab}!
711
712 Another macro-function provides the same feature but with a long date like `December 25, 2024'
713 \exemple!\begin{spreadtab}{{tabular}{cc}}
714 @January 1, 2001 & englongdatetonum(a1)\\
715 @December 25, 2024 & englongdatetonum(a2)
716 \end{spreadtab}!
717
718 \subsubsection{From a number to a date}
719 Several macro functions can be used to translate a number into a date. All these macro-functions have in common that their result is \emph{text}. As a result, immediately after a number-to-text macro function has been evaluated, any other evaluation in the cell is stopped and the cell becomes a text cell. If there was text in the cell with the formula, the result of the formula will be inserted instead of \Verb-:={<formula>}-. As the cell is made up of text only, it can no longer be processed mathematically.
720
721 These macro-functions are:
722 \begin{itemize}
723 \item \verb-numtoengshortdate- translate a number into a short date like `1789/7/14';
724 \item \verb-numtoenglongdate- translate a number into a long date like `July 14, 1789';
725 \item \verb-numtoengmonth- given a number representing a date, it finds the name of the month;
726 \item \verb-numtoengday- same as above but it finds the name of the day.
727 \end{itemize}
728 Here is an example in which we consider 30 days before and 30 days after 2009/6/1. For each of these 2 dates, we calculate the short date, long date, month and day of the week
729 \exemple!\begin{spreadtab}{{tabular}{cc}} \hline
730 \multicolumn{2}{|c|}{@2009/6/1} \\\hline\hline
731 30 & numtoengshortdate(engshortdatetonum(a1)+[-1,0])\\
732 30 & numtoenglongdate(engshortdatetonum(a1)+[-1,0]) \\
733 30 & numtoengmonth(engshortdatetonum(a1)+[-1,0]) \\
734 30 & numtoengday(engshortdatetonum(a1)+[-1,0]) \\\hline
735 -30 & numtoengshortdate(engshortdatetonum(a1)+[-1,0])\\
736 -30 & numtoenglongdate(engshortdatetonum(a1)+[-1,0]) \\
737 -30 & numtoengmonth(engshortdatetonum(a1)+[-1,0]) \\
738 -30 & numtoengday(engshortdatetonum(a1)+[-1,0])
739 \end{spreadtab}!
740
741 \subsection{Coordinate macro-functions}\label{tag}
742 \subsubsection{\falseverb{tag}, \falseverb{cell}, \falseverb{value} and \texttt{\textbackslash STtag}}
743 Rather than referring to a cell by its coordinates, which are difficult to remember and change if a row or column is inserted, it is sometimes much more practical to give a cell a name and refer to it later by that name.
744
745 The macro function \STmacro{tag} has the syntax \Verb-tag(<name>)-. This causes the cell in which it is located to be named. It is not really a macro function like the others since it returns nothing when placed in a formula: it disappears without affecting the mathematical result. Thus, \Verb-tag(<name>)- can be written \emph{anywhere} in the numeric field of a cell. The \Verb-<name>- can be any alphanumeric string, but it is not recommended to use a letter and a number, which could be interpreted as a cell reference and therefore modified during a copy operation with \verb-\STcopy-. This macro function has an additional action: it globally stores the numeric value of the cell in which it is located via a \verb-\gdef-, so that it can be called later \emph{outside the table} using the expandable command \Verb-\STtag{<name>}-.
746
747 Later in the table, instead of using the coordinates of the cell \Verb-<name>-, one can write \Verb-cell(<name>)-, which is a macro function that returns the coordinates of the previously named cell. For example, if \Verb-tag(<name>)- was written in cell \verb-B3-, then later writing \Verb|cell(<name>)| will cause this macro function to return \verb-B3-.
748
749 Here is an example where we sum cells and give the name \verb-foo- to the first number and \verb-bar- to the last. It can be observed that \verb-tag(bar)- is between 1 and 9, and ultimately, since this macro function disappears, the numeric field of the cell will contain 19:
750 \exemple!\begin{spreadtab}{{tabular}{r@{}r}}
751 & 15tag(foo) \\
752 @+ & 37 \\
753 @+ & 13 \\
754 @+ & 48 \\
755 @+ & 1tag(bar)9 \\\cline{2-2}
756 & sum(cell(foo):cell(bar))tag(baz)
757 \end{spreadtab}
758
759 foo=\STtag{foo}, bar=\STtag{bar}, baz=\STtag{baz}!
760
761 To pass values between tables calculated by \ST, it is possible to tag the cell in the first table using the macro function \Verb|tag(<name>)|, and then in the second table, refer to the value \emph{via} the tag with \Verb|value(<name>)|.
762 \exemple!\begin{spreadtab}{{tabular}{|c|c|c|}}\hline
763 100 & 75 & a1+b1tag(ab:sum)\\\hline
764 \end{spreadtab}
765
766 \begin{spreadtab}{{tabular}{|c|c|}}\hline
767 value(ab:sum) & 1.20*value(ab:sum)\\\hline
768 \end{spreadtab}!
769
770 If the key \STkey{tag to aux}\label{tag to aux} is \STval{true}, the assignments are also written to the auxiliary file, making them transferable from one compilation to another. It then becomes possible to place \Verb|\STtag{<name>}| before the table where \Verb|tag(<name>)| is specified.
771
772 \subsubsection{\falseverb{row} and \falseverb{col}}
773 Although at first sight less useful, \ST also provides the macro functions \Verb-row(<name>)- and \Verb-col(<name>)- that return the number of the row or column of the cell \Verb-tag(<name>)-. Here is an example of how to calculate the average of a number of values; the first and last values are tagged `first' and `last' and therefore, the number of values is \verb|row(last)-row(first)+1|:
774 \exemple!mean =
775 \begin{spreadtab}{{tabular}[b]{r}}
776 7tag(first)\\9\\15\\6\\20\\13\\11\\55tag(last)\\
777 \hline\hline
778 sum(cell(first):cell(last))/(row(last)-row(first)+1)
779 \end{spreadtab}!
780
781 \section{Particular care}
782 \subsection{Defining new commands with \ttfamily\textbackslash hline}\label{filets horizontaux}
783 It may be useful to define a new command to produce, for example, a double horizontal line:
784 \centerverb|\newcommand\dline{\hline\hline}|
785
786 and then try to use it in a table as in this simple example that computes the Fibonnacci sequence in the second line::
787 \begin{center}
788 \newcommand\dline{\hline\hline}
789 \def\xtracol{&&&&&&}
790 \begin{spreadtab}{{tabular}{*9c}}
791 0 & 1 & \STcopy>{b1+1} \STxp\xtracol\\\dline
792 :={1} & 1 & \STcopy>{a2+b2}\STxp\xtracol
793 \end{spreadtab}
794 \end{center}
795 But, if you write the following code, there is a problem when compiling:
796 \chevronsfalse\centerverb!\newcommand\dline{\hline\hline}
797 \def\xtracol{&&&&&&}
798 \begin{spreadtab}{{tabular}{*9c}}
799 0 & 1 & \STcopy>{b1+1} \STxp\xtracol\\\dline
800 1 & 1 & \STcopy>{a2+b2}\STxp\xtracol
801 \end{spreadtab}!\chevronstrue
802
803 In the log file, you can read that \verb-\FPeval- fails and complains: \falseverb{! Improper alphabetic constant.}
804
805 The reason is simple, \verb-\dline- in line 4 is not recognized by \ST as a horizontal rule and therefore, \emph{it is placed in the cell in the next line}. For \ST, the cell \falseverb{b1} contains:
806 \centerverb!\dline 1!
807
808 To compile without error, the cell \falseverb{a2} \emph{must} contain a numeric field marker:
809 \exemple!\newcommand\dline{\hline\hline}
810 \def\xtracol{&&&&&&}
811 \begin{spreadtab}{{tabular}{*9c}}
812 0 & 1 & \STcopy>{b1+1} \STxp\xtracol\\\dline
813 :={1} & 1 & \STcopy>{a2+b2}\STxp\xtracol
814 \end{spreadtab}!
815
816 \subsection{The use of {\ttfamily\textbackslash multicolumn} and {\ttfamily\textbackslash SThidecol}}
817 Firstly, in normal use, joint use of \verb|\multicolumn| and \verb-\SThiderow- should not happen, and most users should not encounter this situation and should not read this section.
818
819 Here is the problem: first, a hidden column \emph{must not} contain a cell with the command \verb-\multicolumn-! But what happens if a hidden column hides cells merged with \verb-\multicolumn-?
820
821 In general, there is no compilation error or error messages, but there are some subtleties about the references that are a bit turned upside down in the line after the \verb-\multicolumn- command\ldots
822
823 Let's take an example, and let's say that, in the following table, we want to merge the cell \falseverb{b2} to \falseverb{h2} and we also want to hide the colomns \falseverb{c}, \falseverb{d} and \falseverb{f}, here in gray:
824 \begin{center}
825 \ttfamily
826 \begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
827 \hline
828 a1 & b1 & \cellcolor[gray]{0.6}c1 & \cellcolor[gray]{0.6}d1 & e1 & \cellcolor[gray]{0.6}f1 & g1 & h1 & i1 & j1\\\hline
829 a2 & \multicolumn1c{b2} & \multicolumn1{>{\cellcolor[gray]{0.6}}c}{} & \multicolumn1{>{\cellcolor[gray]{0.6}}c}{} & \multicolumn1c{} & \multicolumn1{>{\cellcolor[gray]{0.6}}c}{} & \multicolumn1{c}{} & \multicolumn1{c|}{} & i2 & j2\\\hline
830 \end{tabular}
831 \end{center}
832 There are 4 visible merged cells, so we write \verb-\multicolumn{4}- because hidden columns are never taken into account when counting the number of \verb-\multicolumn-.
833
834 Then we count 4 letters from \falseverb{b} (this letter included): we obtain the letter \falseverb{e}. In the range \falseverb{b-e}, let's count: 2 gray hidden columns are included (\falseverb c and \falseverb d) and 1 hidden column is not included (\falseverb f). These numbers are important to understand the following, also let's call them $x$ and $y$ in the general case.
835
836 The rule is:
837 \begin{itemize}
838 \item it is necessary to add $y$ signs \verb-&- after \verb-\multicolumn- (in the example above, it would be 1);
839 \item references to columns of cells after the \verb-\multicolumn- will be shifted $x$ to the beginning of the alphabet. For the example given, if we want to refer to the cell \falseverb{i2}, we should write \falseverb{g2} instead of \falseverb{i2}.
840 \end{itemize}
841 Here is an example with a similar structure to the previous ($x=2$ and $y=1$) with simple formulas: add 1 to the number above.
842 \exemple*!\begin{spreadtab}{{tabular}{|*{7}{c|}}}\hline
843 1 & 2 & \SThidecol3 & \SThidecol4 & 5& \SThidecol6 & 7& 8& 9 & 10 \\\hline
844 a1+1& \multicolumn4{l|}{:={b1+1}}& & i1+1 & j1+1\\\hline
845 a2+1& b2+1 & & & & & & & g2+1 & h2+1\\\hline
846 \end{spreadtab}!
847
848 \subsection{Messages delivered by \ST}
849 The package delivers error messages and aborts compilation in these cases:
850 \begin{itemize}
851 \item a circular reference is found in a cell. In this case, the dependent cells are displayed;
852 \item a cell refers to an empty cell or a text cell when a non-empty numeric field is expected;
853 \item a cell refers to an undefined cell (outside the table);
854 \item a cell refers to a cell merged by a \verb-\multicolumn- command;
855 \item a relative reference has bad syntax.²
856 \end{itemize}
857
858 The package can deliver informative messages (in the log file), which it does by default. The key \STkey{messages}\label{messages}, depending on whether it is \falseverb{true} or \falseverb{false}, enables or disables the transmission of information messages.
859
860 To understand the meaning of these messages, let's take a simple table:
861 \exemple!\begin{spreadtab}{{tabular}{|cccc|c|}}\hline
862 b1+1 & c1+1 & d1+1 & 10 & a1+b1+c1+d1\\\hline
863 \end{spreadtab}!
864
865 Here are the messages deliverd by \ST:
866 \centerverb!New spreadtab: \begin{tabular}{|cccc|c|}
867 * reading tab: ok
868 * computing formulas:
869 cell A1-B1-C1
870 cell B1
871 cell C1
872 cell D1
873 cell E1
874 * building tab: ok
875 End of spreadtab: \end{tabular}!
876
877 Preceded by a star, we recognize the 3 steps necessary for \ST to complete its task: reading the table, calculation of the formulas and building the final table.
878
879 For the second step, cells are evaluated from top to bottom, left to right: at line 4 in the code above, \ST says that it begins by trying to calculate the first cell \falseverb{A1}. After a dash, we see that for this, it must first compute the cell \falseverb{B1}, which itself requires that the cell \falseverb{C1} is calculated: the latter can be calculated since it depends only on \falseverb{D1} which is a cell containing the number 10.
880
881 In the following (lines 5 to 8), there is only one cell per line which means that when \ST tries to evaluate the cell, either it contains a number or dependent cells are already calculated.
882
883 \subsection{Debug}
884 To ease the use \ST, a debug mode is available. It is activated when the key \STkey{debug}\label{debug} is not empty and contains at least one of the words \STval{formula}, \STval{text} or \STval{cellcode}, separated by commas. These words each cause a debugging table to be displayed:
885 \begin{itemize}
886 \item \STval{formula}: displays all the numeric fields and the ends of lines;
887 \item \STval{text}: displays all the textual fields;
888 \item \STval{cellcode} displays the internal code of the cells. Indeed, \ST assigns a code to every cell when it reads the table. Here are the possible values of this code:
889 \begin{enumerate}[label=\small\textbullet]
890 \item $-1$ if the cell is merged with \verb-\multicolumn-;
891 \item 0 if the cell is a text cell or is empty;
892 \item 1 if the numeric field of the cell contains a formula which will be computed later;
893 \item 2 if the numeric field of the cell contains a number.
894 \end{enumerate}
895 \end{itemize}
896 You can add the value \STval{show tab} to force the display of the resulting table, which is otherwise not displayed.
897
898 Here's a table for which we display the 3 debugging tables and also the final table obtained:
899 \exemple*!\STset{debug={text,formula,cellcode,show tab}}
900 \begin{spreadtab}{{tabular}{|rr|r|}}\hline
901 @$x$ &@$y$ & @$x+y$\\\hline\hline
902 22 & 54 & \STcopy{v3}{a2+b2} \\
903 43 & 65 & \\
904 49 & 37 & \\\hline
905 $Sx=:={a2+a3+a4}$ & $Sy=:={b2+b3+b4}$ & $Sx+Sy=:={}$\\\hline
906 \multicolumn2{|r|}{$Sy-Sx=:={b5-a5}$} & @\multicolumn1c{}\\\cline{1-2}
907 \end{spreadtab}!
908
909 These 3 debugging tables can help you understand the inner workings of \ST. Table 2 shows that all cells with a numeric field have an internal code of 1 or 2 (see Table 3) and have an associated numeric field marker "\verb-:=-" (see Table 1). This marker represents the place where the result of the numeric field calculation will be inserted by substitution. Once the numerical fields have been calculated, the cells in the final table are reconstituted from the contents of the textual fields in Table 1, by simple substitution.
910
911 In debugging tables, cells containing coordinates are only grayed out if the \href{https://ctan.org/pkg/colortbl}{\texttt{colortbl}} package has been loaded.
912
913 \section{List of keys}\label{key/val}
914 All settings concerning \ST are made from version 0.6 onwards by a system of \chevrons{keys}$=$chevrons{value} which can be found:
915 \begin{enumerate}
916 \item in the \verb|\STset| argument, in which case the settings specified concern all future arrays;
917 \item in the optional argument of \verb|\begin{spreadtab}| or \verb|spreadtab|, in which case the settings specified concern only the current array.
918 \end{enumerate}
919 Here is the list of \chevrons{keys} and their default values:\renewcommand\arraystretch{1.33}%
920 \begin{longtable}{rlcp{9cm}}\hline
921 \chevrons{key} & \chevrons{value} & see page & Details \\\hline\endhead\hline\endfoot
922 \STkey{tabline sep} & \STval[]{\string\\} & \pageref{tabline sep} & separator between table rows \\
923 \STkey{text mark} & \STval[]{\string @} & \pageref{text mark} & the \chevrons{value} is detokenized: if it is present in a cell, \ST understands it as a text cell \\
924 \STkey{numeric mark} & \STval[]{:=} & \pageref{numeric mark} & the argument between braces that follows is the numeric field \\
925 \STkey{freeze char} & \STval[]{!} & \pageref{freeze char} & detokenized character which, if present in front of the letter or number of a reference, does not increment it when the formula is copied \\
926 \STkey{pretab code} & \STval[]{} & \pageref{pretab code} & code executed just before displaying the table\\
927 \STkey{posttab code} & \STval[]{} & \pageref{posttab code} & code executed just after displaying the table\\
928 \STkey{dec sep} & \STval[]{.} & \pageref{dec sep} & decimal separator for numbers after table calculation \\
929 \STkey{autoround} & \STval[]{} & \pageref{autoround} & An empty value is the normal behavior; otherwise, a positive \chevrons{integer} is expected and indicates the rank of the internal calculation rounding.\\
930 \STkey{save list} & \STval[]{} & \pageref{save list} & csv list of the form \chevrons{macro}$=$\chevrons{absolute reference}: each number contained in the referenced cells is stored (globally) in a macro. This key is reset to empty after each table. \\
931 \STkey{aux save list} &\STval[]{} & \pageref{aux save list} & same syntax and behavior as above, but assignments are also written to the auxiliary file. This key is reset to empty after each table. \\
932 \STkey{display marks} & \STval[]{<\kern0pt<;>\kern0pt>} & \pageref{display marks} & markers around an absolute reference in a text field to display the number contained in the referenced cell \\
933 \STkey{copy char} & \STval{"} & \pageref{copy char} & in the argument to \verb|\STcopy|, sets the beginning and end of the part of the formula which must be taken into account for the offsets when copying \\
934 \STkey{tag to aux} & \STval{false} & \pageref{tag to aux} & when \STval{true}, assignments made by the macro function \STmacro{tag} are also written to the auxiliary file. \\
935 \STkey{messages} & \STval{false} & \pageref{messages} & allows \ST to send messages to the log file about what it's doing \\
936 \STkey{debug} & \STval[]{} & \pageref{debug} & must contain words from \STval{text}, \STval{formula} or \STval{cellcode} in csv format to display the corresponding debugging table. If the word \STval{show tab} is added, the table is also displayed. This key is reset to empty after each table.\\
937 \hline
938 \end{longtable}
939 To return to the default values, you can run the \verb|\STreset| macro at any time.
940 \bigbreak
941
942 \begin{center}
943 \Large\parskip0pt \offinterlineskip
944 *\kern0.33em *\par
945 *
946 \end{center}
947
948 Please let me know by \href{mailto:[email protected]}{\texttt{email}} any bugs, regressions or new features to implement that you think might be useful.
949 \end{document}