如何在表中获取所需的行数

如何在表中获取所需的行数

\FPset\rowN{5} 这将设置表中的行数 = 5,并且表有 5 行。

如果 \FPset\rowN{3},表只需要有 3 行...

如何自动确定表中的行数?

没有表格,我使用循环实现了,但对齐不正确...没有表格可以对齐吗?

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

类似于https://stackoverflow.com/questions/2564728/forloop-and-table-in-latex

答案1

最终解决方案形式在表格环境中使用循环的问题

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

相关内容