算法逻辑:在数据工具中对每 3 行进行循环

算法逻辑:在数据工具中对每 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访问缓冲字段

  • 第一个缓冲行是 1,第二个缓冲行是 2,等等(必须小于或等于\lfbufNbBufferedRows);

  • 宏名称macro1macro2、 ... 中的任意一个(来自 的第二个参数的元素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 为单位处理行(数据库的记录)。\lfbufProcessOneRow是的第三个强制参数中的唯一标记\DTLforeach*:它的作用是将读取的行收集到内存中,\DTLforeach*直到有 3 个,此时它将调用\myPrintBufferedData(内容代码环境的参数lfbuffering)。您必须定义\myPrintBufferedData要对缓冲行执行的操作。其定义可能如下所示(给定lfbuffering此示例中的第二个参数使用的值,\myPrintBufferedData可以访问TypeNameDescription字段):

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

相关内容