\the\thislevelcount 在 ForEachX 循环内可用,但 \thislevelitem 不可用(forarray 包)

\the\thislevelcount 在 ForEachX 循环内可用,但 \thislevelitem 不可用(forarray 包)

我正在尝试循环遍历逗号分隔的列表并打印出表格中的元素。以下是完成此操作的部分代码:

\documentclass[10pt,a4paper]{article}

% \usepackage[includeheadfoot, margin=1.5cm,headsep=25pt,footskip=25pt,a4paper]{geometry} 
\usepackage[left=1.0cm, right=1.0cm, top=2cm, bottom=3cm, bindingoffset=0cm, showframe=true]{geometry}
\setlength{\headsep}{30pt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{marvosym}
%\usepackage{setspace}
\usepackage{array}
\usepackage[english=nohyphenation]{hyphsubst}
\usepackage[english]{babel}
\usepackage{booktabs}
%\setlength{\heavyrulewidth}{2pt}
\usepackage{fancyhdr}
\usepackage{tabularx}
\usepackage{tikz} % for using foreach loops
\usepackage{xspace}
\usepackage{ifthen} % http://texdoc.net/texmf-dist/doc/latex/base/ifthen.pdf

\usepackage[nomessages]{fp} % http://ctan.org/pkg/fp. For doing calculations like add, subtract, divide, etc.
% for moving the tables to the left
\usepackage[strict]{changepage}

%\usepackage{pgfmath,pgfkeys} % https://tex.stackexchange.com/questions/117427/rounding-up-decimal-number-using-latex-3
%\newcommand\RoundingUpFunction[1]{%
%  \pgfmathparse{ceil(#1)}%
%  \pgfmathprintnumber[int trunc]{\pgfmathresult}%
%}
% \RoundingUpFunction{2.7}

\fancyhf{}

%% Top center
\chead{
    This is the header
}
\cfoot{}
\renewcommand{\headrulewidth}{0.4pt}
\pagestyle{fancy}

% This is for the list of skills
\usepackage{forarray}
\newcommand{\nCols}{0} % change this in doc - but first need to define it here in the pre-amble
\newcommand{\nRows}{0}
\newcommand{\skillsList}{0}

% This is for putting ampersands inside conditionals for tables
% https://tex.stackexchange.com/questions/7590/how-to-programmatically-make-tabular-rows-using-whiledo/7594#7594
% https://tex.stackexchange.com/questions/165126/how-do-i-use-the-ampersand-inside-a-foreach-or-conditional-or-other-group-e
\makeatletter
\newtoks\@tabtoks
\newcommand\addtabtoks[1]{\global\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand\eaddtabtoks[1]{\edef\mytmp{#1}\expandafter\addtabtoks\expandafter{\mytmp}}
\newcommand*\resettabtoks{\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother

\begin{document}
\def\and{&\xspace}
% -------------------------------------------------------- Summary --------------------------------------------------------
\begin{adjustwidth}{-0.02cm}{}
\begin{tabularx}{\textwidth}{@{}>{\raggedright}p{2.2cm}@{}p{16.8cm}@{}}
\textbf{Summary} & This is a summary.
\end{tabularx}
\end{adjustwidth}
% -------------------------------------------------------------------------------------------------------------------------


% -------------------------------------------------------- Skills ---------------------------------------------------------
% list the skills 
\renewcommand{\skillsList}{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x} % 
\renewcommand{\nCols}{4} % this is the total number of columns for the skills only (need to add one for the label)

% count all the skills to get the number of skills. This will allow calculation of the number of rows.
\newcounter{nSkills}
\ForEachX{,}{\stepcounter{nSkills}}{\skillsList}

% calculate the number of rows as the ceil(number of skills/number of columns)
\FPdiv\nRows\thenSkills\nCols
%\def\zeropfive{0.5}
%\FPadd\nRows\nRows\zeropfive
\FPround\nRows\nRows0
% nRows is \nRows
number of skills: \thenSkills

% Now create the table
%\textbf{IT Skills} & This & here & is filler & a lot of it \\
%\textbf{IT Skills} & 1 & 2 & 3 & 4

\begin{adjustwidth}{-0.02cm}{}
\begin{tabularx}{\textwidth}{@{}>{\raggedright}p{2.2cm}@{}>{\raggedright}X@{}>{\raggedright}X@{}>{\raggedright}X@{}>{\raggedright\arraybackslash}X@{}}
\resettabtoks
\foreach \r in {1,...,\nRows}{  % https://stackoverflow.com/questions/2561791/iteration-in-latex
    \foreach \c in {1,...,\nCols}{
        \ForEachX{,}{ 
            % The number of the element of the list is: \r-(\c-1)*nRows
            \FPsub\factor\c1
            \FPmul\term\factor\nRows
            \FPadd\rowElementToShow\r\term
            \FPtrunc\rowElementToShow\rowElementToShow0

            \ifthenelse{\equal{\the\thislevelcount}{\rowElementToShow}}{
                %\ifthenelse{\the\thislevelcount < \thenSkills}{
                    \addtabtoks{&} \eaddtabtoks{\the\thislevelcount} \addtabtoks{\thislevelitem}  % \addtabtoks{& blah & blah & blah \\ }
                %}{}
            }{}
        }{\skillsList}  
    }
    \addtabtoks{\\}
}
\printtabtoks
\end{tabularx}
\end{adjustwidth}
% -------------------------------------------------------------------------------------------------------------------------


\end{document}

这段代码的输出如下:

上述代码的 PDF 输出

我希望输出相同,只是每个数字旁边都有技能,等等1:a 7:g。这个变量应该是\thislevelitem,但这个变量在 中addtabtoks{}没有输出,而在 中eaddtabtoks{}给出错误,即使\thislevelcount可用。有什么想法可以解决这个问题吗?

谢谢!

答案1

我真的不知道forarray;这里有一个实现,它似乎以更通用的方式做你想做的事情。

\defineskills{...}您定义技能集时,有一个可选的新集合参数,请参见示例。

使用\numberofskills(可能带有可选参数)打印数字;最后\tableofskills打印表格。

\documentclass{article}
\usepackage{xparse,array,tabularx}

\ExplSyntaxOn
\NewDocumentCommand{\defineskills}{ O{default} m }
 {
  \shafique_defineskills:nn { #1 } { #2 }
 }

\NewDocumentCommand{\numberofskills}{ O{default} }
 {
  \shafique_numberofskills:n { #1 }
 }

\NewDocumentCommand{\tableofskills}{ O{default} m }
 {
  \shafique_tableofskills:nn { #1 } { #2 }
 }

\seq_new:N \g_shafique_skills_default_seq
\int_new:N \l_shafique_rows_int
\tl_new:N \l_shafique_body_tl
\dim_new:N \l_shafique_numberwd_dim

\cs_new_protected:Npn \shafique_defineskills:nn #1 #2
 {
  \seq_if_exist:cF { g_shafique_skills_#1_seq }
   {
    \seq_new:c { g_shafique_skills_#1_seq }
   }
  \seq_set_from_clist:cn { g_shafique_skills_#1_seq } { #2 }
 }

\cs_new:Npn \shafique_numberofskills:n #1
 {
  \int_to_arabic:n { \seq_count:c { g_shafique_skills_#1_seq } }
 }

\cs_new_protected:Npn \shafique_tableofskills:nn #1 #2
 {
  % compute the number of rows
  \int_compare:nTF
   {
    \int_mod:nn { \seq_count:c { g_shafique_skills_#1_seq } } { #2 } = 0
   }
   {% exact multiple
    \int_set:Nn \l_shafique_rows_int { \seq_count:c { g_shafique_skills_#1_seq } / #2 }
   }
   {% else truncate division and add one
    \int_set:Nn \l_shafique_rows_int
     {
      \int_div_truncate:nn { \seq_count:c { g_shafique_skills_#1_seq } } { #2 } + 1
     }
   }
  \tl_clear:N \l_shafique_body_tl
  \hbox_set:Nn \l_tmpa_box { \int_to_arabic:n { \seq_count:c { g_shafique_skills_#1_seq } } }
  \dim_set:Nn \l_shafique_numberwd_dim { \box_wd:N \l_tmpa_box }
  \int_step_inline:nnnn { 1 } { 1 } { \l_shafique_rows_int }
   {
    \shafique_fill_row:nnn { #1 } { ##1 } { #2 }
   }
  \shafique_print_table:n { #2 }
 }

\cs_new_protected:Npn \shafique_fill_row:nnn #1 #2 #3
 {% #1 is the name of the sequence
  % #2 is the row number
  % #3 is the number of columns
  \int_step_inline:nnnn { 0 } { 1 } { #3 - 1 }
   {
    \tl_put_right:Nx \l_shafique_body_tl
     {
      \int_compare:nF
       {
        #2 + \l_shafique_rows_int * ##1 > \seq_count:c { g_shafique_skills_#1_seq }
       }
       {
        \exp_not:N \makebox[\l_shafique_numberwd_dim][r]
         {
          \int_to_arabic:n { #2 + \l_shafique_rows_int * ##1 }
         }
        : \c_space_tl
        \seq_item:cn { g_shafique_skills_#1_seq } { #2 + \l_shafique_rows_int * ##1 }
       }
      \int_compare:nTF { ##1 = #3 -1 } { \exp_not:N \\ } { & }
     }
   }
 }

\cs_new_protected:Npn \shafique_print_table:n #1
 {
  \par\noindent
  \begin{tabularx}{\textwidth}
   {
    @{} *{#1}{>{\raggedright\arraybackslash}X@{}}
   }
  \tl_use:N \l_shafique_body_tl
  \end{tabularx}
 }

\ExplSyntaxOff

\begin{document}
\defineskills{a,b,c,d,e,f,g}

The number of skills is \numberofskills

\medskip

\tableofskills{4}

\bigskip

\defineskills[new]{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x}

The number of skills is \numberofskills[new]

\medskip

\tableofskills[new]{4}

\end{document}

在此处输入图片描述

我将技能放入一个序列中,为了打印表格,我做了一个双循环,首先在行上,然后在列上,以检索正确的项目。

相关内容