# algorithm Logic: Make Loop for each 3 rows in datatools

i want to make loop with process:

Current: (with below code)

• each row create one page
• Box A, B, C: Same data from one row of datatool

Need update:

• Create one page only \Break=break or each 3 rows

• Box A = data of row: 1,4,7...,

• Box B = data of row :2,5,8…

• Box C= data of row: 3,6,9

• Each 3 rows in datatools create one page

Exampe:

Page 1:

A: data of row: 1

B: data of row: 2

C: data of row: 3

Page 2:

A: data of row: 4

B: data of row: 5

C; data of row: 6

Minimal coding:

\documentclass[a5paper,twoside,8pt]{article}
\usepackage[a5paper,landscape,left=1.0cm,right=0.3cm,top=0.5cm,bottom=0.5cm]{geometry}
\usepackage{tcolorbox}
\tcbuselibrary{poster}
\usepackage{tikz,everypage}
\usepackage[absolute,overlay]{textpos}
\usepackage{filecontents}

\begin{filecontents*}{product.tex}
%Type =1,2...10
No,Type,Name,Description,Break
1,1,A1,D1,xx
2,1,A1,D2,yy
3,1,A1,D3,break
4,1,A1,D30,ll
5,1,A2,D31,mm
6,1,A2,D131,break
7,1,A3,D132,bb
8,1,A3,D133,tt
9,1,A3,D134,break
10,1,A4,D249,ii
11,1,A10,D1000,bb
12,1,A2,D11,break
13,1,A3,D13,qq
14,1,A3,D135,gg
15,1,A3,D137,break
16,1,A4,D249,ff
17,1,A10,D100,gg
18,1,A43,D318,break
19,1,A44,D319,ss
20,1,A40,D320,ww
21,1,A43,D318,break
22,2,A44,D319,as
23,2,A40,D320,aw
\end{filecontents*}

\usepackage{datatool}
\usepackage{ifthen}

\newcommand{\printtype}[1]{%
\DTLforeach*
[\DTLiseq{\Type}{#1}]% Condition
{products}% Database
{\NoCoding=No,\Type=Type,\Name=Name,\Description=Description,\Break=Break}{%

\begin{tcbposter}[
poster = {
columns=1,
rows=2,
spacing=3mm,
height=14cm,
width=12cm,
},
]
%Box A
\posterbox[
colframe = red,
width=5cm, height= 5cm
]{xshift=1 cm,yshift=-3cm}{\includegraphics[height=2cm]{example-image-a}
\\
}
%Box B
\posterbox[
colframe = blue,
width=5cm, height= 5cm
]{xshift=7cm,yshift =-3cm }{\includegraphics[height=3cm]{example-image-b}
\\
}
%Box C
\posterbox[
colframe = green,
width=5cm, height= 5cm
]{xshift=13cm,yshift =-3cm }{\includegraphics[height=3cm]{example-image-c}
\\
}

\end{tcbposter}
\newpage
}%
}
\begin{document}
\printtype{1}

\end{document}


Example Image of current code

The following code implements buffering on top of datatool, in order to allow you to process the rows n by n. This works via an environment called lfbuffering which is called this way:

\begin{lfbuffering}{n}{macro names for needed columns}{code}
\DTLforeach*{database}% Database
{\macro1=colname1, \macro2=colname2, ..., \macrop=colnamep}
{\lfbufProcessOneRow}
\end{lfbuffering}


This will call the code in the third argument of the lfbuffering environment every time n rows have been read (buffered) by \DTLforeach*. If less than n rows are available for the last execution of code, it will still be executed; \lfbufNbBufferedRows tells how many rows are available in the buffer (technically, \lfbufNbBufferedRows is a \countdef token; in particular, it is a TeX 〈number〉, i.e., an integer).

So, for instance, if n is 4 and \DTLforeach* provides 11 database rows in total, the successive invocations of code will see \lfbufNbBufferedRows equal to 4, 4 then 3 (4 + 4 + 3 = 11). code can be a macro name or more tokens. It has access to the buffered fields using \lfbufField{k}{macroName} where

• k is 1 for the first buffered row, 2 for the second buffered row, etc. (k must be less than or equal to \lfbufNbBufferedRows);

• macroName is any of macro1, macro2, ... (elements from the second argument of lfbuffering, corresponding to part or all of the macro names defined in the \DTLforeach* call's second mandatory argument, without the leading backslashes).

Let's take a simple example:

\begin{lfbuffering}{3}{Type, Name, Description}{\myPrintBufferedData}
\DTLforeach*{products}% Database
{\NoCoding=No,\Type=Type,\Name=Name,\Description=Description,\Break=Break}
{\lfbufProcessOneRow}
\end{lfbuffering}


Here, we process the rows (records of your products database) 3 by 3. \lfbufProcessOneRow is the only token in the third mandatory argument of \DTLforeach*: its role is to gather in memory the rows read by \DTLforeach* until it has 3, at which point it will call \myPrintBufferedData (contents of the code argument of the lfbuffering environment). You have to define \myPrintBufferedData to say what you want to do with the buffered rows. Its definition can look like this (given the value used for the second argument of lfbuffering in this example, \myPrintBufferedData can access the Type, Name and Description fields):

\newcommand*{\myPrintBufferedData}{%
\setlength{\parindent}{0pt}% for instance
\ifnum\lfbufNbBufferedRows>0 % <-- space or end-of-line here, important!
\lfbufField{1}{Type}, \lfbufField{1}{Name},
\lfbufField{1}{Description}\par
\fi
%
\ifnum\lfbufNbBufferedRows>1 % here too
\lfbufField{2}{Type}, \lfbufField{2}{Name},
\lfbufField{2}{Description}\par
\fi
%
\ifnum\lfbufNbBufferedRows>2 % and here
\lfbufField{3}{Type}, \lfbufField{3}{Name},
\lfbufField{3}{Description}\par\medskip
\fi
}


Since the code argument of the lfbuffering environment is never called with an empty buffer, the first test (\ifnum\lfbufNbBufferedRows>0 [ended by a space]) could be omitted. But this way, all cases follow the same pattern. Here is a complete example similar to what we just explained:

\RequirePackage{filecontents}
\begin{filecontents*}{product.tex}
%Type =1,2...10
No,Type,Name,Description,Break
1,1,A1,D1,xx
2,1,A1,D2,yy
3,1,A1,D3,break
4,1,A1,D30,ll
5,1,A2,D31,mm
6,1,A2,D131,break
7,1,A3,D132,bb
8,1,A3,D133,tt
9,1,A3,D134,break
10,1,A4,D249,ii
11,1,A10,D1000,bb
12,1,A2,D11,break
13,1,A3,D13,qq
14,1,A3,D135,gg
15,1,A3,D137,break
16,1,A4,D249,ff
17,1,A10,D100,gg
18,1,A43,D318,break
19,1,A44,D319,ss
20,1,A40,D320,ww
21,1,A43,D318,break
22,2,A44,D319,as
23,2,A40,D320,aw
\end{filecontents*}

\documentclass{article}
\usepackage{xparse}
\usepackage{datatool}

\ExplSyntaxOn

\int_new:N \l_lfbuf_buffer_depth_int
\seq_new:N \l_lfbuf_colnames_seq
\tl_new:N \l_lfbuf_output_callback_tl

% #1: zero-based index of buffered row
% #2: field name
% #3: value
\cs_new_protected:Npn \lfbuf_store_field_aux:nnn #1#2#3
{
\tl_set:cn { l_lfbuf_data_#1_#2_tl } {#3}
}

\cs_generate_variant:Nn \lfbuf_store_field_aux:nnn { nnV }

% #1: zero-based index of buffered row
% #2: field name
\cs_new_protected:Npn \lfbuf_store_field:nn #1#2
{
% Get the field contents; this requires 3 expansion steps
\tl_set:No \l_tmpa_tl { \use:c {#2} }
\exp_args:NNNo \exp_args:NNo \tl_set:No \l_tmpa_tl { \l_tmpa_tl}
\lfbuf_store_field_aux:nnV {#1} {#2} \l_tmpa_tl
}

\cs_generate_variant:Nn \lfbuf_store_field:nn { Vn }

\cs_new_protected:Npn \lfbuf_clear_buffer_vars:
{
\int_step_inline:nnn { 0 } { \l_lfbuf_buffer_depth_int - 1 }
{
\seq_map_inline:Nn \l_lfbuf_colnames_seq
{ \tl_clear_new:c { l_lfbuf_data_##1_####1_tl } }
}
}

% These two are often identical, but not always
\int_new:N \l_lfbuf_buffered_row_index_int
\int_new:N \lfbufNbBufferedRows       % user-accessible from callback code

\cs_new_protected:Npn \lfbuf_process_one_row:
{
\seq_map_inline:Nn \l_lfbuf_colnames_seq
{ \lfbuf_store_field:Vn \l_lfbuf_buffered_row_index_int {##1} }

% Advance the index, but stay modulo \l_lfbuf_buffer_depth_int
\int_set:Nn \l_lfbuf_buffered_row_index_int
{ \int_mod:nn
{ \l_lfbuf_buffered_row_index_int + 1 }
{ \l_lfbuf_buffer_depth_int }
}

% Is the buffer full?
\int_compare:nNnT { \l_lfbuf_buffered_row_index_int } = { 0 }
{
% Print output and start over with an empty buffer.
\int_set_eq:NN \lfbufNbBufferedRows \l_lfbuf_buffer_depth_int
\tl_use:N \l_lfbuf_output_callback_tl
}
}

\cs_new:Npn \lfbuf_get_field:nn #1#2
{
\use:c { l_lfbuf_data_#1_#2_tl }
}

\cs_generate_variant:Nn \lfbuf_get_field:nn { f }

% *********************************************************************
% As opposed to all code-level functions, document commands use 1-based
% indexing (datatool also uses 1-based indexing for rows and columns).
% *********************************************************************

% Expand to field #2 (column title) of buffered row #1 (index starting from 1).
\NewExpandableDocumentCommand \lfbufField { m m }
{
\lfbuf_get_field:fn { \int_eval:n {#1-1} } {#2}
}

\NewDocumentCommand \lfbufProcessOneRow { }
{
\lfbuf_process_one_row:
}

\NewDocumentEnvironment { lfbuffering } { m m +m }
{
\int_set:Nn \l_lfbuf_buffer_depth_int {#1}
\seq_set_from_clist:Nn \l_lfbuf_colnames_seq {#2}
\tl_set:Nn \l_lfbuf_output_callback_tl {#3}
\int_set:Nn \l_lfbuf_buffered_row_index_int { 0 }
\lfbuf_clear_buffer_vars:
\ignorespaces
}
{
\unskip
% If there is buffered data that hasn't been output, process it now (this
% means that the last row of the datatool table didn't fill the buffer).
\int_compare:nNnT { \l_lfbuf_buffered_row_index_int } > { 0 }
{
\int_set_eq:NN \lfbufNbBufferedRows \l_lfbuf_buffered_row_index_int
\tl_use:N \l_lfbuf_output_callback_tl
}
}

\ExplSyntaxOff

\newcommand*{\myPrintBufferedData}{%
\setlength{\parindent}{0pt}%
% I keep this test for symmetry with the other cases, but it is always true.
% You can remove it if you prefer.
\ifnum\lfbufNbBufferedRows>0 % if there remains at least one row
\lfbufField{1}{NoCoding}, \lfbufField{1}{Type}, \lfbufField{1}{Name},
\lfbufField{1}{Description}, \lfbufField{1}{Break}\par
\fi
%
\ifnum\lfbufNbBufferedRows>1
\lfbufField{2}{NoCoding}, \lfbufField{2}{Type}, \lfbufField{2}{Name},
\lfbufField{2}{Description}, \lfbufField{2}{Break}\par
\fi
%
\ifnum\lfbufNbBufferedRows>2
\lfbufField{3}{NoCoding}, \lfbufField{3}{Type}, \lfbufField{3}{Name},
\lfbufField{3}{Description}, \lfbufField{3}{Break}\par\medskip
\fi
}

\begin{document}

% Read and process 3 lines at a time. Call \myPrintBufferedData every time
% the buffer is full as well as at the end (i.e., the last call can have 1,
% 2 or 3 lines, as indicated by \lfbufNbBufferedRows).
\begin{lfbuffering}{3}{NoCoding, Type, Name, Description, Break}
{\myPrintBufferedData}
\DTLforeach*{products}% Database
{\NoCoding=No,\Type=Type,\Name=Name,\Description=Description,\Break=Break}
{\lfbufProcessOneRow}
\end{lfbuffering}

\end{document}


And here is the example with your tcbposter:

\RequirePackage{filecontents}
\begin{filecontents*}{product.tex}
%Type =1,2...10
No,Type,Name,Description,Break
1,1,A1,D1,xx
2,1,A1,D2,yy
3,1,A1,D3,break
4,1,A1,D30,ll
5,1,A2,D31,mm
6,1,A2,D131,break
7,1,A3,D132,bb
8,1,A3,D133,tt
9,1,A3,D134,break
10,1,A4,D249,ii
11,1,A10,D1000,bb
12,1,A2,D11,break
13,1,A3,D13,qq
14,1,A3,D135,gg
15,1,A3,D137,break
16,1,A4,D249,ff
17,1,A10,D100,gg
18,1,A43,D318,break
19,1,A44,D319,ss
20,1,A40,D320,ww
21,1,A43,D318,break
22,2,A44,D319,as
23,2,A40,D320,aw
\end{filecontents*}

\documentclass{article}
\usepackage[landscape,hscale=0.8]{geometry}
\usepackage{tcolorbox}
\tcbuselibrary{poster}
\usepackage{xparse}
\usepackage{datatool}

\ExplSyntaxOn

\int_new:N \l_lfbuf_buffer_depth_int
\seq_new:N \l_lfbuf_colnames_seq
\tl_new:N \l_lfbuf_output_callback_tl

% #1: zero-based index of buffered row
% #2: field name
% #3: value
\cs_new_protected:Npn \lfbuf_store_field_aux:nnn #1#2#3
{
\tl_set:cn { l_lfbuf_data_#1_#2_tl } {#3}
}

\cs_generate_variant:Nn \lfbuf_store_field_aux:nnn { nnV }

% #1: zero-based index of buffered row
% #2: field name
\cs_new_protected:Npn \lfbuf_store_field:nn #1#2
{
% Get the field contents; this requires 3 expansion steps
\tl_set:No \l_tmpa_tl { \use:c {#2} }
\exp_args:NNNo \exp_args:NNo \tl_set:No \l_tmpa_tl { \l_tmpa_tl}
\lfbuf_store_field_aux:nnV {#1} {#2} \l_tmpa_tl
}

\cs_generate_variant:Nn \lfbuf_store_field:nn { Vn }

\cs_new_protected:Npn \lfbuf_clear_buffer_vars:
{
\int_step_inline:nnn { 0 } { \l_lfbuf_buffer_depth_int - 1 }
{
\seq_map_inline:Nn \l_lfbuf_colnames_seq
{ \tl_clear_new:c { l_lfbuf_data_##1_####1_tl } }
}
}

% These two are often identical, but not always
\int_new:N \l_lfbuf_buffered_row_index_int
\int_new:N \lfbufNbBufferedRows       % user-accessible from callback code

\cs_new_protected:Npn \lfbuf_process_one_row:
{
\seq_map_inline:Nn \l_lfbuf_colnames_seq
{ \lfbuf_store_field:Vn \l_lfbuf_buffered_row_index_int {##1} }

% Advance the index, but stay modulo \l_lfbuf_buffer_depth_int
\int_set:Nn \l_lfbuf_buffered_row_index_int
{ \int_mod:nn
{ \l_lfbuf_buffered_row_index_int + 1 }
{ \l_lfbuf_buffer_depth_int }
}

% Is the buffer full?
\int_compare:nNnT { \l_lfbuf_buffered_row_index_int } = { 0 }
{
% Print output and start over with an empty buffer.
\int_set_eq:NN \lfbufNbBufferedRows \l_lfbuf_buffer_depth_int
\tl_use:N \l_lfbuf_output_callback_tl
}
}

\cs_new:Npn \lfbuf_get_field:nn #1#2
{
\use:c { l_lfbuf_data_#1_#2_tl }
}

\cs_generate_variant:Nn \lfbuf_get_field:nn { f }

% *********************************************************************
% As opposed to all code-level functions, document commands use 1-based
% indexing (datatool also uses 1-based indexing for rows and columns).
% *********************************************************************

% Expand to field #2 (column title) of buffered row #1 (index starting from 1).
\NewExpandableDocumentCommand \lfbufField { m m }
{
\lfbuf_get_field:fn { \int_eval:n {#1-1} } {#2}
}

\NewDocumentCommand \lfbufProcessOneRow { }
{
\lfbuf_process_one_row:
}

\NewDocumentEnvironment { lfbuffering } { m m +m }
{
\int_set:Nn \l_lfbuf_buffer_depth_int {#1}
\seq_set_from_clist:Nn \l_lfbuf_colnames_seq {#2}
\tl_set:Nn \l_lfbuf_output_callback_tl {#3}
\int_set:Nn \l_lfbuf_buffered_row_index_int { 0 }
\lfbuf_clear_buffer_vars:
\ignorespaces
}
{
\unskip
% If there is buffered data that hasn't been output, process it now (this
% means that the last row of the datatool table didn't fill the buffer).
\int_compare:nNnT { \l_lfbuf_buffered_row_index_int } > { 0 }
{
\int_set_eq:NN \lfbufNbBufferedRows \l_lfbuf_buffered_row_index_int
\tl_use:N \l_lfbuf_output_callback_tl
}
}

\ExplSyntaxOff

\newcommand*{\myPrintBufferedData}{%
\begin{tcbposter}[poster={columns=1, rows=2, spacing=3mm,
height=14cm, width=12cm}]
% Box A
\posterbox[colframe=red, width=6cm, height=5cm]{xshift=0cm, yshift=-3cm}
{% I keep this test for symmetry with the other cases, but it is always
% true. You can remove it if you prefer.
\ifnum\lfbufNbBufferedRows>0
\includegraphics[width=4cm]{example-image-a}\\
\noindent
\lfbufField{1}{Break}%
\fi
}%
% Box B
\posterbox[colframe=blue, width=6cm, height=5cm]{xshift=8cm, yshift=-3cm}
{%
\ifnum\lfbufNbBufferedRows>1
\includegraphics[width=4cm]{example-image-b}\\
\noindent
\lfbufField{2}{Break}%
\fi
}%
% Box C
\posterbox[colframe=green, width=6cm, height=5cm]{xshift=16cm, yshift=-3cm}
{%
\ifnum\lfbufNbBufferedRows>2
\includegraphics[width=4cm]{example-image-c}\\
\noindent
\lfbufField{3}{Break}%
\fi
}%
\end{tcbposter}%
\newpage
}

\newcommand{\printtype}[1]{%
% Read and process 3 lines at a time. Call \myPrintBufferedData every time
% the buffer is full as well as at the end (i.e., the last call can have 1,
% 2 or 3 lines, as indicated by \lfbufNbBufferedRows).
\begin{lfbuffering}{3}{NoCoding, Type, Name, Description, Break}
{\myPrintBufferedData}
\DTLforeach*
[\DTLiseq{\Type}{#1}]% Condition
{products}% Database
{\NoCoding=No,\Type=Type,\Name=Name,\Description=Description,\Break=Break}
{\lfbufProcessOneRow}
\end{lfbuffering}%
}

\begin{document}

\printtype{1}

\end{document}


Page 1:

Page 2:

Page 3:

...

Page 7:

• super. Thank so much. I will update: Loop each 9 rows. What i need to change in your code. Thanks – latexforti Aug 15 '19 at 15:20
• Just change the {3} into {9}... By the way: if you are sure that the number of rows considered by \DTLforeach* (after possible filtering via \DTLiseq or so) is a multiple of the buffer depth (first argument of the lfbuffering environment—the 3 or 9 here), then you don't need the \ifnum ... \fi tests, since every time the code in the third argument of lfbuffering is run, the buffer will be full. These tests are useful in the last run in general, because this is where the buffer may not be full if the “number of rows is multiple of buffer depth” condition is not met. – frougon Aug 15 '19 at 15:29
• Thanks. I will try and hope it work in my casw – latexforti Aug 15 '19 at 15:42
• In my full examples, read carefully everything after \ExplSyntaxOff: this is the part you need to understand. – frougon Aug 15 '19 at 15:44