答案1
我认为以下命令\Table
可以完成这项工作。第一个参数是表中所需的列数,第二个参数是数字列表(以逗号分隔)。
例如:\Table{4}{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21}
\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand {\Table} {mm}
{\clist_set:Nn \l_tmpa_clist {#2}
\int_do_until:nNnn {\clist_count:N \l_tmpa_clist} < {#1+1}
{\int_step_inline:nnnn 1 1 {#1-1}
{\clist_pop:NN \l_tmpa_clist \l_tmpb_tl
\tl_put_right:NV \l_tmpa_tl \l_tmpb_tl
\tl_put_right:Nn \l_tmpa_tl {&}}
\clist_pop:NN \l_tmpa_clist \l_tmpb_tl
\tl_put_right:NV \l_tmpa_tl \l_tmpb_tl
\tl_put_right:Nn \l_tmpa_tl {\\ \hline}}
\int_set:Nn \l_tmpa_int {\clist_count:N \l_tmpa_clist}
\int_compare:nNnT \l_tmpa_int > 0
{\int_step_inline:nnnn 1 1 {\l_tmpa_int - 1}
{\clist_pop:NN \l_tmpa_clist \l_tmpb_tl
\tl_put_right:NV \l_tmpa_tl \l_tmpb_tl
\tl_put_right:Nn \l_tmpa_tl {&}}
\clist_pop:NN \l_tmpa_clist \l_tmpb_tl
\tl_put_right:NV \l_tmpa_tl \l_tmpb_tl
\int_step_inline:nnnn 1 1 {#1 - \l_tmpa_int}
{\tl_put_right:Nn \l_tmpa_tl {&}}
\tl_put_right:Nn \l_tmpa_tl {\\ \hline}}
\begin{tabular}{|*{#1}{c|}}
\hline
\l_tmpa_tl
\end{tabular}}
\ExplSyntaxOff
\begin{document}
\Table{4}{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21}
\end{document}
答案2
无需使用xparse
,有一个可与TikZlistofitems
一起使用的包。\foreach
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{listofitems}
\setsepchar{,}
\newlength{\mysize}
\setlength{\mysize}{1cm}%size of the squares
\begin{document}
\readlist\mylist{You,can,put,here,what,you,want,8,9,10,11,12,13,14,15,16,17,18}
\begin{tikzpicture}
\foreach \i [evaluate=\i as \myrow using {int((\i-1)/5)},
evaluate=\i as \mycol using {mod((\i-1),5)}] in {1,2,...,18}{
\node[draw, rectangle, minimum size=\mysize, text height=.5\mysize, text depth=.2\mysize] at (\mycol*\mysize,-\myrow*\mysize) {\mylist[\i]};}
\end{tikzpicture}
\end{document}
答案3
可以使用表格环境来制作 Latex 中的表格。首先启动环境,然后说明我们希望每列中的数据如何对齐。使用“l”将项目左对齐。然后,您的数据后面跟着“&”以分隔列,并使用两个反斜杠来结束行。最后关闭环境。如果您想为表格添加边框,可以在标题中添加“|”并在行尾添加 \hline。
例如
此代码
\begin{tabular}{|l|l|l|l|l|}
\hline
1 & 2 & 3 & 4 & 5 \\ \hline
6 & 7 & 8 & 9 & 10 \\ \hline
11 & 12 & 13 & 14 & 15 \\ \hline
16 & 17 & 18 & & \\ \hline
\end{tabular}
我经常在网上用这个表格生成器创建更复杂表格的工具。
答案4
您可以决定列数以及列宽(在本例中,所有列宽均相等)。
\documentclass{article}
\usepackage{xparse}
\usepackage{array}
\ExplSyntaxOn
\NewDocumentCommand{\dyntable}{omm}
{% #1 is the cell width, if specified
% #2 is the number of columns
% #3 is the list of elements
\IfNoValueTF { #1 }
{
\arindam_dyntable:nnn { * } { #2 } { #3 }
}
{
\arindam_dyntable:nnn { #1 } { #2 } { #3 }
}
}
\tl_new:N \l__arindam_dyntable_body_tl
\int_new:N \l__arindam_dyntable_count_int
\cs_new_protected:Nn \arindam_dyntable:nnn
{
\tl_clear:N \l__arindam_dyntable_body_tl
\int_zero:N \l__arindam_dyntable_count_int
\clist_map_inline:nn { #3 }
{
\int_incr:N \l__arindam_dyntable_count_int
\tl_put_right:Nn \l__arindam_dyntable_body_tl { ##1 }
\int_compare:nTF
{ \int_mod:nn { \l__arindam_dyntable_count_int } { #2 } = 0 }
{ \tl_put_right:Nn \l__arindam_dyntable_body_tl { \\ \hline } }
{ \tl_put_right:Nn \l__arindam_dyntable_body_tl { & } }
}
\int_compare:nT
{
\int_mod:nn { \l__arindam_dyntable_count_int } { #2 } > 0
}
{ % we need to fill the last line
\prg_replicate:nn
{
#2-1-\int_mod:nn { \l__arindam_dyntable_count_int } { #2 }
}
{ \tl_put_right:Nn \l__arindam_dyntable_body_tl { & } }
\tl_put_right:Nn \l__arindam_dyntable_body_tl { \\ \hline }
}
% print the table
\str_if_eq:nnTF { #1 } { * }
{ \begin{tabular}{|*{#2}{c|}} }
{ \begin{tabular}{|*{#2}{w{c}{#1}|}} }
\hline
\tl_use:N \l__arindam_dyntable_body_tl
\end{tabular}
}
\ExplSyntaxOff
\begin{document}
\dyntable[1.5em]{4}{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
\bigskip
\dyntable{6}{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
\bigskip
\dyntable[3em]{6}{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
\bigskip
\dyntable{4}{
apple, banana, orange, pineapple, cherry, mango, pear,
apricot, peach
}
\bigskip
\dyntable[5em]{4}{
apple, banana, orange, pineapple, cherry, mango, pear,
apricot, peach
}
\end{document}
遍历项目列表,并将每个项目添加到表主体中;当达到列数时,\\
将附加,否则&
。
存在填充最后一行(如果短的话)的问题,可通过添加适当数量的&
和尾随 来完成\\
。
如果没有指定可选参数,我们将传递*
,这最终表示c
列类型。如果在可选参数中指定了长度,我们将使用它w{c}{<length>}
来获取等宽列。