自动制表:根据列表创建表格

自动制表:根据列表创建表格

我刚刚发现了一些命令\autorows,并且\autocols在回忆录类中可以将列表包装到表中。例如

\autorows{c}{5}{l}{one, two, three, four,
     five, six, seven, eight, nine, ten,
     eleven, twelve, thirteen }

将排版如下的表格:

one    two    three    four  five
six    seven  eight    nine  ten
eleven twelve thirteen

是否有一个包可以做到这一点,因为我不想从我正在使用的类切换到memoir.cls

答案1

从技术上讲,你可以\autorowsmemoir.dtx并将其放在你的序言中(或将其收集到样式文件中):

在此处输入图片描述

\documentclass{article}

\makeatletter
\def\ctabsetlines{%
  \let\hline\m@mhline
  \let\@BTnormal\m@m@BTnormal}
\newdimen\@mincolumnwidth
\newskip\ctableftskip \ctableftskip=\fill
\newskip\ctabrightskip \ctabrightskip=\fill
\newdimen\TX@col@width
\newcount\TX@cols
\newcount\@linestogo           % lines remaining to be processed
\newcount\@cellstogo           % cells remaining in column or row
\newcount\abovecolumnspenalty
  \abovecolumnspenalty=10000
\newtoks\crtok
  \crtok = {\cr}%

\newcommand{\autorows}[5][0pt]{\par\begingroup
 \ctabsetlines
% Set the table position
  \ctableftskip\fill
  \ctabrightskip\fill
  \if l#2 
    \ctableftskip\z@
  \else
    \if r#2
      \ctabrightskip\z@
    \fi
  \fi
% Set the column position style
  \let\c@lleftskip\hfil
  \let\c@lrightskip\hfil
  \if l#4 
    \let\c@lleftskip\relax
  \else
    \if r#4
      \let\c@lrightskip\relax
    \fi
  \fi
% Count the number of entries and the minimum width (max entry width)
% for the columns.
  \TX@cols=#3\relax
  \@curtab=#3\relax
  \@cellstogo = \TX@cols
  \@mincolumnwidth\z@
  \@linestogo\z@
  \@for\@tempa:=#5\do{%
    \advance\@linestogo\@ne
    \settowidth{\@tempdima}{\@tempa}
    \ifdim\@tempdima>\@mincolumnwidth
      \@mincolumnwidth=\@tempdima
    \fi}%
  \advance\@mincolumnwidth\tabcolsep
% Specify what is to be done after every entry
  \def\@endcolumnactions{%
    \global\advance\@linestogo\m@ne
    \global\advance\@cellstogo\m@ne
    \ifnum\@cellstogo<\@ne
      \global\@cellstogo=\TX@cols
      \the\crtok
    \else 
      & 
    \fi}%
% Calculate the width of the columns
  \ifdim #1>\z@
    \TX@col@width=#1
  \else
    \TX@col@width=\hsize
  \fi
  \divide\TX@col@width \TX@cols
  \ifdim #1=\z@
    \TX@col@width=\@mincolumnwidth
  \fi
  \penalty\abovecolumnspenalty
  \noindent % usually not a paragraph
  \vskip -\z@ % don't know why we need this, but looks bad without it
% Create most of the preamble by looping to add \cs{@cellsincolumn}-1 slots,
% then the last one which is different.
  \def\@preamble{}%
  \begingroup
    \let\@sharp\relax
    \ifnum\TX@cols>\@ne
      \loop
        \ifdim #1<\z@
          \g@addto@macro{\@preamble}{%
            \strut\c@lleftskip\@sharp\c@lrightskip &}%
        \else
          \g@addto@macro{\@preamble}{%
            \hb@xt@ \TX@col@width{%
                    \strut\c@lleftskip\@sharp\c@lrightskip} &}%
        \fi
        \advance\TX@cols\m@ne
      \ifnum\TX@cols>\@ne
      \repeat
    \fi
    \ifdim #1<\z@
      \g@addto@macro{\@preamble}{%
        \strut\c@lleftskip\@sharp\c@lrightskip}%
    \else
      \g@addto@macro{\@preamble}{%
        \hb@xt@ \TX@col@width{\strut\c@lleftskip\@sharp\c@lrightskip}}%
    \fi
  \endgroup
  \let\@sharp ##
% Start the \verb?\halign?
  \tabskip\ctableftskip
  \halign to \hsize \bgroup
    \tabskip\z@
    \@preamble
    \tabskip\ctabrightskip\cr 
% Add all the entries then finish off.
    \@for\@tempa:=#5\do{%
      \@tempa\unskip\space\@endcolumnactions}%
    \the\crtok \egroup \endgroup \par}
\makeatother

\begin{document}

\autorows{c}{5}{l}{
     one, two, three, four,
     five, six, seven, eight, nine, ten,
     eleven, twelve, thirteen}

\end{document}

\makeatletter...之间的所有内容\makeatother都是您所需要的。可以进行类似的提取\autocols(留作练习)。

请注意,逗号分隔列表中的空格很重要。

答案2

一个内部不使用表格的简单实现:

在此处输入图片描述

\documentclass{article}

\makeatletter
\newcommand\autorows[3]{%
\begin{flushleft}
\@for\tmp:=#3\do{\makebox[\dimexpr\textwidth/#1][#2]{\ignorespaces\tmp\unskip}\linebreak[0]}%
\end{flushleft}}
\makeatother

\begin{document}

\autorows{5}{l}{one, two, three, four,
     five, six, seven, eight, nine, ten,
     eleven, twelve, thirteen }

\end{document}

相关内容