演算法邏輯:在datatools中每3行進行循環

演算法邏輯:在datatools中每3行進行循環

我想用進程循環:

目前:(使用以下程式碼)

  • 每行創建一頁
  • 框 A、B、C:來自數據工具一行的相同數據

需要更新:

  • 僅建立一頁 \Break=break 或每 3 行

  • 框 A = 行資料: 1,4,7...,

  • B 框=第 2、5、8…行的數據

  • 框 C= 行資料:3,6,9

  • datatools中每3行建立一頁

範例:

第 1 頁:

A:行數據:1

B:行數據:2

C:行數據:3

第2頁:

A:行數據:4

B:行數據:5

C;行數據:6

最少編碼:

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

目前程式碼的範例圖像 在此輸入影像描述

預先感謝

答案1

以下程式碼在 之上實現緩衝datatool,以便允許您處理行n經過n。這是透過一個名為的環境來運作的,lfbuffering其呼叫方式如下:

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

lfbuffering這將每次調用環境的第三個參數中的程式碼n行已被讀取(緩衝)\DTLforeach*。如果小於n行可用於最後一次執行程式碼,仍然會被執行;\lfbufNbBufferedRows告訴緩衝區中有多少行可用(從技術上講,\lfbufNbBufferedRows是一個\countdef標記;特別是,它是一個 TeX <number>,即一個整數)。

所以,舉例來說,如果n為 4,\DTLforeach*總共提供 11 條資料庫行,連續調用程式碼將看到\lfbufNbBufferedRows等於 4、4 然後 3 (4 + 4 + 3 = 11)。程式碼可以是巨集名稱或多個標記。它可以使用\lfbufField{k}{macroName}where訪問緩衝字段

  • k第一個緩衝行為 1,第二個緩衝行為 2,依此類推(k必須小於或等於\lfbufNbBufferedRows);

  • 宏名macro1, macro2, ... 中的任何一個(來自 的第二個參數的元素lfbuffering,對應於調用的第二個強制參數中定義的部分或全部宏名稱\DTLforeach*,不帶前導反斜杠)。

讓我們舉一個簡單的例子:

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

在這裡,我們處理行(資料庫的products記錄) 3 by 3 \lfbufProcessOneRow。(的內容\DTLforeach*\DTLforeach*\myPrintBufferedData程式碼環境論證lfbuffering)。您必須定義\myPrintBufferedData要對緩衝行執行的操作。它的定義可以如下所示(給lfbuffering定本範例中第二個參數使用的值,\myPrintBufferedData可以存取Type,NameDescription欄位):

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

自從程式碼環境參數lfbuffering永遠不會用空緩衝區調用,第一個測試(\ifnum\lfbufNbBufferedRows>0[以空格結尾])可以省略。但這樣一來,所有案例都遵循相同的模式。這是一個與我們剛剛解釋的類似的完整範例:

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

螢幕截圖

這是您的範例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}

第 1 頁:

第1頁

第2頁:

第2頁

第 3 頁:

第3頁

第 7 頁:

第7頁

相關內容