Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider the following MWE :

\documentclass[12pt]{article}
\usepackage{datatool}
\begin{document}

\DTLnewdb{table}

\DTLnewrow{table}
\DTLnewdbentry{table}{a}{hallo}
\DTLnewdbentry{table}{b}{non}
\DTLnewrow{table}
\DTLnewdbentry{table}{a}{bonjour}
\DTLnewdbentry{table}{b}{non}
\DTLnewrow{table}
\DTLnewdbentry{table}{a}{czesc}
\DTLnewdbentry{table}{b}{oui}
\DTLnewrow{table}
\DTLnewdbentry{table}{a}{hola}
\DTLnewdbentry{table}{b}{oui}

\section{display table}

\DTLdisplaydb{table}

\section{Subtable}

\begin{tabular}{|c|c|c|}
\hline
a & b & row-number \\
\DTLforeach[\DTLiseq{\b}{oui}]{table}{\a=a,\b=b}{%
\a    &    \b & \arabic{DTLrowi}  \\
}%
\end{tabular}

\end{document}

The \DTLforeach loop runs on all rows for with the test [\DTLiseq{\b}{oui}] is true, i.e. all rows with a=oui or rows 3 and 4. The command \arabic{DTLrowi} delivers the row number of the current row in the loop, but it refers to the "subtable" generated by the restriction [\DTLiseq{\b}{oui}]. In this subtable, they are rows 1 and 2.

My question: I want to refer to the original row numbers (3 and 4 in this case). Is it possible ?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

The <condition> associated with \DTLforeach[<condition>]{..}{..}{<body>} should then be used within <body>. Furthermore, the result of \DTLiseq{\b}{oui} returns true or false and should then be used in an \ifthenelse{<condition>}{<true>}{<false>} boolean clause:

\DTLforeach
  %[\DTLiseq{\b}{oui}]
  {table}
  {\a=a,\b=b}{%
    \ifthenelse{\DTLiseq{\b}{oui}}
      {\a    &    \b & \arabic{DTLrowi}  \\}
      {}%
  }%

This definition in your MWE produces:

enter image description here

share|improve this answer
    
simple and efficient :) I'm nevertheless puzzled, that the original rownumber is not anymore available in the DTLloop with the [test] –  nicolas roy Jan 20 '12 at 12:03
    
@nicolasroy: It is, but it is only stepped (incremented) when the <condition> is true. That's why migration to the body means it the default condition always holds (which is true), stepping the counter at every row, resulting in the correct output. –  Werner Jan 20 '12 at 21:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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