我有两个 CSV 文件。一个有 5 列,另一个有 6 列。
需要检查 CSV 文件并确定列数,我成功做到了。我将列数保存在 \mycolumncount 中。
此外,我还制定了一个条件,根据列数生成相应的表,但是对于这部分,我无法获得输出表。
值得注意的是,需要使用csvsimple-l3包。
怎么了?
\begin{filecontents*}{file1.csv}
a,b,c,d,e
1,2,3,4,5
4,5,6,7,8
\end{filecontents*}
\begin{filecontents*}{file2.csv}
a,b,c,d,e,f
1,2,3,4,5,6
4,5,6,7,8,9
\end{filecontents*}
\documentclass{article}
\usepackage{csvsimple-l3}
\usepackage{ifthen}
\ExplSyntaxOn
\NewDocumentCommand{\GetColumnCount}{m}
{
\int_zero_new:N \l_tmpa_int
\ior_open:Nn \g_tmpa_ior {#1}
\ior_str_map_inline:Nn \g_tmpa_ior
{
\clist_set:Nn \l_tmpa_clist {##1}
\int_set:Nn \l_tmpa_int {\clist_count:N \l_tmpa_clist}
\int_gset:Nn \g_tmpa_int {\l_tmpa_int}
}
\int_use:N \g_tmpa_int
\ior_close:N \g_tmpa_ior
}
\ExplSyntaxOff
\newcommand{\customtable}[1]{%
\edef\mycolumncount{\GetColumnCount{#1}}
%
\ifthenelse{\equal{\mycolumncount}{5}}{%
\begin{tabular}{|*{5}{c|}}
\hline
\csvreader[
head to column names,
late after line=\\\hline,
]{#1}{}{%
\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv
}
\end{tabular}
}{\ifthenelse{\equal{\mycolumncount}{6}}{%
\begin{tabular}{|*{6}{c|}}
\hline
\csvreader[
head to column names,
late after line=\\\hline,
]{#1}{}{%
\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi
}
\end{tabular}
}{}
}
}
\begin{document}
\customtable{file1.csv}
The number of columns is: \mycolumncount.
\end{document}
我没有输出表格,只有列数信息。我还注意到一个重要信息,即表格的列宽度不同。
答案1
问题似乎是\GetColumnCount
无法扩展,正如所写的那样。由于我不熟悉 L3,我求助于readarray
包来获取列数,该数在执行\ncols
a 后提供。\readdef
\begin{filecontents*}{file1.csv}
a,b,c,d,e
1,2,3,4,5
4,5,6,7,8
\end{filecontents*}
\begin{filecontents*}{file2.csv}
a,b,c,d,e,f
1,2,3,4,5,6
4,5,6,7,8,9
\end{filecontents*}
\documentclass{article}
\usepackage{readarray}
\readarraysepchar{,}
\usepackage{csvsimple-l3}
\usepackage{ifthen}
\newcommand{\customtable}[1]{%
\readdef{#1}\filedata
\let\mycolumncount\ncols
%
\ifthenelse{\equal{\mycolumncount}{5}}{%
\begin{tabular}{|*{5}{c|}}
\hline
\csvreader[
head to column names,
late after line=\\\hline,
]{#1}{}{%
\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv
}
\end{tabular}
}{\ifthenelse{\equal{\mycolumncount}{6}}{%
\begin{tabular}{|*{6}{c|}}
\hline
\csvreader[
head to column names,
late after line=\\\hline,
]{#1}{}{%
\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi
}
\end{tabular}
}{}
}
}
\begin{document}
\customtable{file1.csv}
The number of columns is: \mycolumncount.
\customtable{file2.csv}
The number of columns is: \mycolumncount.
\end{document}
仅使用以下工具即可获得相同的结果readarray
,不需要将列数作为\customtable
宏的一部分进行检查:
\begin{filecontents*}{file1.csv}
a,b,c,d,e
1,2,3,4,5
4,5,6,7,8
\end{filecontents*}
\begin{filecontents*}{file2.csv}
a,b,c,d,e,f
1,2,3,4,5,6
4,5,6,7,8,9
\end{filecontents*}
\documentclass{article}
\usepackage{readarray}
\readarraysepchar{,}
\renewcommand\typesetrowsepchar{\\\hline}
\renewcommand\typesetcolsepchar{&}
\newcommand{\customtable}[1]{%
\readdef{#1}\myfile
\readarray*\myfile\myfiledata[-,\ncols]%
\initarray\myabbrevdata[\numexpr\nrows-1\relax,\ncols]%
\mergearray\myfiledata\myabbrevdata[0,1]%
\begin{tabular}{|*{\ncols}{c|}}
\hline
\typesetarray\myabbrevdata\\
\hline
\end{tabular}}
\begin{document}
\customtable{file1.csv}
The number of columns is: \ncols.
\customtable{file2.csv}
The number of columns is: \ncols.
\end{document}