8

I just found out about the commands \autorows and \autocols in the memoir class that will wrap a list into a table. For example

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

will typeset a table that looks like this :

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

Is there a package that can do that, as I would rather not switch from the class I am using to memoir.cls ?

4

Technically you can just grab everything related to \autorows from memoir.dtx and place it in your preamble (or collect it in a style file):

enter image description here

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

Everything between \makeatletter ... \makeatother is what you need. A similar extraction can be made for \autocols (and is left as an exercise).

Note that spaces in the comma-separated list is important.

3

A simple implementation not using a tabular internally:

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.