Graphics, Figures & TablesAutomatic table column wrapping

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
spkmn
Posts: 4
Joined: Wed Jan 28, 2009 3:31 pm

Automatic table column wrapping

Post by spkmn »

All,

Let's say that I have a table with 40 rows x 2 columns and the columns are narrow. Is there any package that would look at the table and determine that it could do 2 sets of 20 rows x 2 columns. In effect, I would want it to do:

Col-A Col-B | Col-A Col-B
vala1 valb1 | vala21 valb21
vala2 valb2 | vala22 valb22
... ... | ... ...
vala20 valb20 | vala40 valb40


Thanks
James

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

marco_d
Posts: 57
Joined: Tue Jan 20, 2009 7:49 pm

Re: Automatic table column wrapping

Post by marco_d »

Hello,

the only one you can use is supertabular. When you are using it you can use the twocolumn-option.

regards
Marco
i am German. I can not use difficult words. :-)
spkmn
Posts: 4
Joined: Wed Jan 28, 2009 3:31 pm

Re: Automatic table column wrapping

Post by spkmn »

Hi... I gave this a try two different ways.

I tried \twocolumn before and \onecolumn after the table, but it didn't have any effect. Just to test, I set center before the table. The table was set down the middle of the page.

I also tried it using \begin{multicols}{2} but the table is set down the left column of the page, with the right column blank, and the the left column on the next page. I can tell that the table is centered in the column.

Does anyone have any ideas?

Thanks in advance,
James
marco_d
Posts: 57
Joined: Tue Jan 20, 2009 7:49 pm

Automatic table column wrapping

Post by marco_d »

Hello,

here an example that works:

Code: Select all

\documentclass[a4paper,12pt]{scrreprt}
\usepackage{array,supertabular,ragged2e,multicol}


\begin{document}
\twocolumn
\tablefirsthead{%
  \hline  Col-A &  Col-B \\\hline}
\tablehead{%
  \hline
  \multicolumn{2}{|l|}{\small\sl continued from previous page}\\\hline
  Col-A &  Col-B \\\hline}
\tabletail{%
  \hline\multicolumn{2}{|r|}{\small\sl continued on next page}\\\hline}
\tablelasttail{\hline}
\bottomcaption{This table is split}
\begin{supertabular}{|c|c|}
20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 
20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 
20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 
20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline
20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 
20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 
20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 
20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline 20 & 22 \\\hline
\end{supertabular}

\end{document}
regards
Marco
i am German. I can not use difficult words. :-)
kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Re: Automatic table column wrapping

Post by kaiserkarl13 »

The example provided only works if the table is the first thing on a page. Otherwise, supertabular realizes it has way too many rows to typeset on one page and starts a new one (even though that's unnecessary due to the second column!).

It also doesn't work inside the multicol package's multicols environment (you get two tables on two pages, with nothing in the second column).

I'm trying to do the same thing as the initial poster---that is, create a table that is created (and numbered) as a single table but is typeset as two super-columns, each containing its own version of the original table.

My original thought was to have two running counters (one for the right column and one for the left), with the start of the right column counter defined like so:
\@ifundefined{r@lastlstruct}{}{\setcounter{rstructure}{\ref{lastlstruct}}}%

However, this breaks if I use the calc package, AND requires me to manually create the columns (which makes modifying the table very difficult).

Any ideas?
kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Automatic table column wrapping

Post by kaiserkarl13 »

I wanted to see if anyone has a solution to the posted problem, as I'm still trying to figure this out.

The desired function is to generate a table that is half the width of the printed table, with the second half of the table printed in the second column. In short, typing something like

Code: Select all

\begin{splittabular}{l l l l}
 a & a & a & a \\
 b & b & b & b \\
 c & c & c & c \\
 A & A & A & A \\
 B & B & B & B \\
 C & C & C & C \\
\end{splittabular}
results in the table

a a a a A A A A
b b b b B B B B
c c c c C C C C

(assuming, of course, that the bottom of the page was only three lines away from the start of the table...).
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Automatic table column wrapping

Post by phi »

Here is a custom implementation, but perhaps there is a package that already implements this:

Code: Select all

\documentclass{article}

\usepackage{environ}
\usepackage{etoolbox}

\DeclareListParser{\ProcessTableRows}{\\}
\newcount\RowCount
\newcount\RowIndex

\NewEnviron{splittabular}[1]{%
    \RowCount=0 %
    \def\do##1{\advance\RowCount by 1 }%
    \expandafter\ProcessTableRows\expandafter{\BODY}%
    \divide\RowCount by 2 %
    \RowIndex=0 %
    \def\do##1{%
        \ifnum\RowIndex<\RowCount
        \csdef{Row-\number\RowIndex}{##1}%
        \else
        \eappto\AllRows{\csexpandonce{Row-\number\numexpr\RowIndex-\RowCount}\unexpanded{&##1\tabularnewline}}%
        \fi
        \advance\RowIndex by 1 %
    }%
    \expandafter\ProcessTableRows\expandafter{\BODY}%
    \tabular{#1#1}%
    \AllRows
    \endtabular
}


\begin{document}

\begin{splittabular}{l l l l}
a & a & a & a \\
b & b & b & b \\
c & c & c & c \\
A & A & A & A \\
B & B & B & B \\
C & C & C & C \\
\end{splittabular}

\end{document}
kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Re: Automatic table column wrapping

Post by kaiserkarl13 »

phi, thank you for posting this---extraordinary helpful. I managed to tweak it to place vertical lines down the center, too.

I'm going to see if I can make a variant on this that allows running lists of automatically numbered table cells, e.g.,

1 blah blah 4 blah blah
2 blah blah 5 blah blah
3 blah blah 6 blah blah

Entered as:
\countem & blah & blah \\
\countem & blah & blah \\
\countem & blah & blah \\
\countem & blah & blah \\
\countem & blah & blah \\
\countem & blah & blah \\

I'll also have to do some work to get \hline and/or \cline to work. Perhaps this isn't possible, but now that I know there are some interesting add-ons that may make it at least hypothetically possible I have to try.

Thanks again!
Post Reply