5

I have a file that contains 1,000 rows of product data. I want to select only a specific product type to print in my document.

Minimal code in main.tex:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{product.tex}

%Type =1,2...10
No.,Type ,Name , Description
1, 1, A1,D1
….
30, 1, A1, D30
31, 2, A2, D31
…
131,2, A2, D131
132,3,A3,D132
….
249,4,A4,D249
…
1.000,10, A10,D1000

\end{filecontents*}

\begin{document}
%Need command can CHOOSE Type = 1 and/or 2, and/or 3, ...10
%i choose type = 1 & 3
\include{run.tex}
\end{document}

Product Types range from 1, 2, ..., 10. And in run.tex:

%Display like:

Name[i] & Description[j]

main.pdf result:

Type = 1

A1 - D1

…

A1 - D30

Type = 3

A3 - D132

…

A3 - D248
3

Use datatool for this:

enter image description here

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{product.tex}
%Type =1,2...10
No,Type,Name,Description
1,1,A1,D1
2,1,A1,D2
3,1,A1,D3
30,1,A1,D30
31,2,A2,D31
131,2,A2,D131
132,3,A3,D132
133,3,A3,D133
134,3,A3,D134
249,4,A4,D249
1000,10,A10,D1000
\end{filecontents*}

\usepackage{datatool}

\DTLloaddb[autokeys=false]{products}{product.tex}

\newcommand{\printtype}[1]{%
  \par
  \section*{Type #1}
  \DTLforeach*
    [\DTLiseq{\Type}{#1}]% Condition
    {products}% Database
    {\No=No,\Type=Type,\Name=Name,\Description=Description}{%
    \noindent\Name \quad \Description\par
  }% 
}

\begin{document}

Here is some text.

\printtype{1}

Some breaking text here.

\printtype{3}

And then some final text.

\end{document}

The \printtype{<type>} command uses \DTLforeach to cycle through the products database and print only items where the \Type equals <type>. You can format the presentation however you want.

  • Thank you. What is code if i need 2 condition (type = range (1-4) and No =1) do A) ; (type = range (1-4) and No != 1) do B) . Thanks – latexforti Feb 23 at 22:13
3

This is an answer heavily based on this answer. The main point here is to remark that you need to do \pgfplotsinvokeforeach in order to loop over the types you want typeset, and not \foreach.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{product.dat}
No.,Type,Name,Description
1, 1, A1,D1
2, 1, A1,D2
30, 1, A1, D30
31, 2, A2, D31
131,2, A2, D131
132,3,A3,D132
249,4,A4,D249
1.000,10, A10,D1000
\end{filecontents*}
\usepackage{amsmath,amssymb}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}
\pgfplotstableread[col sep=comma]{product.dat}{\data}
\pgfplotsinvokeforeach{1,3}{
\subsection*{\boldmath$\text{Type}=#1$}
\pgfplotstabletypeset[string type,
row predicate/.code={%
  \pgfplotstablegetelem{##1}{Type}\of{\data}
  \ifnum\pgfplotsretval=#1\relax
  \else\pgfplotstableuserowfalse\fi}
]{\data}
}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.