1

\FPset\rowN{5} This sets the number of rows in the table = 5 and table have 5 rows.

If \FPset\rowN{3}, table need to have only 3 rows...

How to automatically decide number of rows in a table?

Without table, i achieved using loop, but alignment is not proper... Is it possible to align without table?

\documentclass{article}
\usepackage{xparse,fp,xfp}

\ExplSyntaxOn

\NewDocumentCommand{\newarray}{mO{100}}
 {
  \fparray_new:cn { g_sandu_#1_fparray } { #2 }
  \cs_new:cpn { #1 } ##1
   {
    \fparray_item:cn { g_sandu_#1_fparray } { ##1 }
   }
 }

\NewDocumentCommand{\readarray}{mm}
 {
  \seq_set_split:Nnn \l__sandu_temp_seq { & } { #2 }
  \int_step_inline:nn { \seq_count:N \l__sandu_temp_seq }
   {
    \fparray_gset:cne { g_sandu_#1_fparray } { ##1 }
     { \seq_item:Nn \l__sandu_temp_seq { ##1 } }
   }
 }

\NewDocumentCommand{\setarrayitem}{mmm}
 {
  \fparray_gset:cne { g_sandu_#1_fparray } { #2 } { #3 }
 }

\cs_generate_variant:Nn \fparray_new:Nn { c }
\cs_generate_variant:Nn \fparray_item:Nn { c }
\cs_generate_variant:Nn \fparray_gset:Nnn { cnn, cne }

\ExplSyntaxOff

\FPset\rowN{3}

\newarray{xx}
\readarray{xx}{1&2&3&4&5}

\newarray{yy}
\readarray{yy}{6&7&8&9&10}

\begin{document}

\newarray{zz}

\newcount\counter
\counter=5
\loop
  \setarrayitem{zz}{\counter}{\fpeval{(\xx{\counter}+\yy{\counter})/2}}
  \advance \counter by -1
  \unless\ifnum \counter<1
\repeat

\FPset\ci{1}
\noindent
No \quad xx \quad yy \quad zz \\
\loop
\ci \quad \xx{\ci} \quad \yy{\ci} \quad \zz{\ci}\\
\FPeval\ci{\ci+1}
\FPeval\ci{clip(round(ci:0))}
\unless\ifnum \ci>\rowN
\repeat

\begin{tabular}{cccc}
No & xx & yy & zz   \\ \hline
1 & \xx{1} & \yy{1} &  \zz{1} \\
2 & \xx{2} & \yy{2} &  \zz{2} \\
3 & \xx{3} & \yy{3} &  \zz{3} \\
4 & \xx{4} & \yy{4} &  \zz{4} \\
5 & \xx{5} & \yy{5} &  \zz{5} \\ \hline
\end{tabular}

\end{document}

Something similar to https://stackoverflow.com/questions/2564728/forloop-and-table-in-latex

1 Answer 1

1

Finally the solution form problem with using loop inside the tabular environment

\documentclass{article}
\usepackage{xparse,fp,xfp}
\usepackage{forloop}

\ExplSyntaxOn

\NewDocumentCommand{\newarray}{mO{100}}
 {
  \fparray_new:cn { g_sandu_#1_fparray } { #2 }
  \cs_new:cpn { #1 } ##1
   {
    \fparray_item:cn { g_sandu_#1_fparray } { ##1 }
   }
 }

\NewDocumentCommand{\readarray}{mm}
 {
  \seq_set_split:Nnn \l__sandu_temp_seq { & } { #2 }
  \int_step_inline:nn { \seq_count:N \l__sandu_temp_seq }
   {
    \fparray_gset:cne { g_sandu_#1_fparray } { ##1 }
     { \seq_item:Nn \l__sandu_temp_seq { ##1 } }
   }
 }

\NewDocumentCommand{\setarrayitem}{mmm}
 {
  \fparray_gset:cne { g_sandu_#1_fparray } { #2 } { #3 }
 }

\cs_generate_variant:Nn \fparray_new:Nn { c }
\cs_generate_variant:Nn \fparray_item:Nn { c }
\cs_generate_variant:Nn \fparray_gset:Nnn { cnn, cne }

\ExplSyntaxOff

\FPset\rowN{3}

\newarray{xx}
\readarray{xx}{1&2&3&4&5}

\newarray{yy}
\readarray{yy}{6&7&8&9&10}

\begin{document}

\newarray{zz}

\newcount\counter
\counter=5
\loop
  \setarrayitem{zz}{\counter}{\fpeval{(\xx{\counter}+\yy{\counter})/2}}
  \advance \counter by -1
  \unless\ifnum \counter<1
\repeat

\FPset\ci{1}
\noindent
No \quad xx \quad yy \quad zz \\
\loop
\ci \quad \xx{\ci} \quad \yy{\ci} \quad \zz{\ci}\\
\FPeval\ci{\ci+1}
\FPeval\ci{clip(round(ci:0))}
\unless\ifnum \ci>\rowN
\repeat

\begin{tabular}{cccc}
No & xx & yy & zz   \\ \hline
1 & \xx{1} & \yy{1} &  \zz{1} \\
2 & \xx{2} & \yy{2} &  \zz{2} \\
3 & \xx{3} & \yy{3} &  \zz{3} \\
4 & \xx{4} & \yy{4} &  \zz{4} \\
5 & \xx{5} & \yy{5} &  \zz{5} \\ \hline
\end{tabular}

\bigskip

\def\tand{&}

\newcounter{it}
\begin{tabular}{cccc}%
\hline
No & xx & yy & zz   \\ \hline
\setcounter{it}{1}%
\whiledo{\theit<\rowN}{%
\theit \tand \xx{\theit} \tand \yy{\theit} \tand \zz{\theit}\\%
\stepcounter{it}%
}%loop ends
\theit \tand \xx{\theit} \tand \yy{\theit} \tand \zz{\theit}\\%
\hline
\end{tabular}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .