Lógica del algoritmo: hacer un bucle para cada 3 filas en herramientas de datos

Lógica del algoritmo: hacer un bucle para cada 3 filas en herramientas de datos

quiero hacer un bucle con el proceso:

Actual: (con el siguiente código)

  • cada fila crea una página
  • Cuadro A, B, C: los mismos datos de una fila de la herramienta de datos

Necesita actualización:

  • Cree una sola página \Break=break o cada 3 filas

  • Cuadro A = datos de la fila: 1,4,7...,

  • Cuadro B = datos de la fila: 2,5,8…

  • Cuadro C= datos de la fila: 3,6,9

  • Cada 3 filas en herramientas de datos crean una página

Ejemplo:

Página 1:

A: datos de la fila: 1

B: datos de la fila: 2

C: datos de la fila: 3

Página 2:

A: datos de la fila: 4

B: datos de la fila: 5

C; datos de la fila: 6

Codificación mínima:

\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}
\DTLloaddb[autokeys=false]{products}{product.tex}

\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}
  \\
  \noindent \NoCoding \quad \Name  \quad \Break\par
  }
  %Box B
  \posterbox[
  colframe = blue,
  width=5cm, height= 5cm
  ]{xshift=7cm,yshift =-3cm }{\includegraphics[height=3cm]{example-image-b}
\\
  \noindent \NoCoding \quad \Name \quad \Break\par
  }
%Box C
  \posterbox[
  colframe = green,
  width=5cm, height= 5cm
  ]{xshift=13cm,yshift =-3cm }{\includegraphics[height=3cm]{example-image-c}
\\
  \noindent \NoCoding \quad \Name  \quad \Break \par
  }

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

\end{document}

Imagen de ejemplo del código actual ingrese la descripción de la imagen aquí

Gracias de antemano

Respuesta1

El siguiente código implementa el almacenamiento en búfer además datatoolde para permitirle procesar las filas.nortepornorte. Esto funciona a través de un entorno llamado lfbufferingque se llama de esta manera:

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

Esto llamará al código en el tercer argumento del lfbufferingentorno cada vez.nortelas filas han sido leídas (almacenadas en búfer) por \DTLforeach*. Si es menos denorteLas filas están disponibles para la última ejecución decódigo, aún se ejecutará; \lfbufNbBufferedRowsindica cuántas filas hay disponibles en el buffer (técnicamente, \lfbufNbBufferedRowses un \countdeftoken; en particular, es un "número" TeX, es decir, un número entero).

Así, por ejemplo, sinortees 4 y \DTLforeach*proporciona 11 filas de base de datos en total, las invocaciones sucesivas decódigoVerás \lfbufNbBufferedRowsigual a 4, 4 y luego 3 (4 + 4 + 3 = 11).códigopuede ser un nombre de macro o más tokens. Tiene acceso a los campos almacenados en buffer usando \lfbufField{k}{macroName}donde

  • kes 1 para la primera fila almacenada en el búfer, 2 para la segunda fila almacenada en el búfer, etc. (kdebe ser menor o igual que \lfbufNbBufferedRows);

  • nombre de macroes cualquiera de macro1, macro2, ... (elementos del segundo argumento de lfbuffering, correspondientes a parte o todos los nombres de macro definidos en el \DTLforeach*segundo argumento obligatorio de la llamada, sin las barras invertidas iniciales).

Tomemos un ejemplo sencillo:

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

Aquí, procesamos las filas (registros de su productsbase de datos) 3 por 3. \lfbufProcessOneRowes el único token en el tercer argumento obligatorio de \DTLforeach*: su función es reunir en la memoria las filas leídas \DTLforeach*hasta que tenga 3, momento en el cual llamará \myPrintBufferedData( contenidos de lacódigoargumento del lfbufferingmedio ambiente). Tienes que definir \myPrintBufferedDatapara decir qué quieres hacer con las filas almacenadas en el buffer. Su definición puede verse así (dado el valor utilizado para el segundo argumento de lfbufferingen este ejemplo, \myPrintBufferedDatapuede acceder a los Typecampos Namey Description):

\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
}

desde elcódigoEl argumento del lfbufferingentorno nunca se llama con un búfer vacío, la primera prueba ( \ifnum\lfbufNbBufferedRows>0[terminada por un espacio]) podría omitirse. Pero de esta manera, todos los casos siguen el mismo patrón. Aquí tienes un ejemplo completo similar a lo que acabamos de explicar:

\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}
\DTLloaddb[autokeys=false]{products}{product.tex}

\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}

Captura de pantalla

Y aquí está el ejemplo con tu 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}
\DTLloaddb[autokeys=false]{products}{product.tex}

\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}{NoCoding}\quad
        \lfbufField{1}{Name}\quad
        \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}{NoCoding}\quad
        \lfbufField{2}{Name}\quad
        \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}{NoCoding}\quad
        \lfbufField{3}{Name}\quad
        \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}

Página 1:

Página 1

Página 2:

Página 2

Página 3:

Página 3

...

Página 7:

Página 7

información relacionada