# Two longtables side by side

I'm trying to add two tables side by side they need to be split, cannot be multi column like a & b & & & & c & d

\begin{longtable}{cc}
\begin{longtable}
\hline
a & b\\
\end{longtable} &

\begin{longtable}
\hline
c & b\\
\end{longtable}
\end{longtable}


This approach however gives me TeX capacity exceeded.

Follow-up I am using Jinja2 template package for Python to dynamically add content to my latex file and then compile that latex file using latex package for Python.

Anyhow in python i do not know when I have reached the end of the current page so if I use a tabular I go off the page. Longtable showed promise as it automatically continues down to second page with a smooth transition.

Is there an alternative way to approach this ?

• Why do you think multi column does not work? – jarauh Oct 28 '15 at 20:05
• I did not say it does not work. I said I cannot use it because I am adding data dynamically using Python & Jinja2 templating language. So i need tables separated. – Sterling Duchess Oct 28 '15 at 20:12
• Then: Why can't Python & Jinja2 cope with this situation? Could you explain this in more detail? – jarauh Oct 28 '15 at 20:19
• Because it over-complicates the code. I have 3 arrays that contain data I would have to rework my entire data structure and IO workflow to have 1 loop build the table like this instead of just having 3 simple loops. – Sterling Duchess Oct 28 '15 at 20:21
• Do you have a MWE? – Peter Ebelsberger Nov 2 '15 at 22:09

If I understand well what you need: you need to place data of one table in compact form in the source file, then to place data of the second table and then to print these tables one next to second, they have different height and they must be breakable to pages. This can be done by the following code:

\newbox\tleftbox \newbox\trightbox

\def\tableleft{\setbox\tleftbox=\vbox\bgroup\tablelr}
\def\tableright{\setbox\trightbox=\vbox\bgroup\tablelr}

\def\tablelr#1{\let\\=\cr
\halign{\vrule\strut\hbox to2em{\hss##\unskip\hss}&%
\hbox to2em{\strut\hss##\unskip\hss}\vrule\cr
\noalign{\penalty0\hrule\nobreak}#1\crcr
\noalign{\hrule\kern-.4pt\kern-\dp\strutbox}}
\egroup
}
\def\printtables{\medskip\bgroup
\splittopskip=0pt plus5pt \offinterlineskip
\dimen0=\wd\tleftbox \dimen1=\wd\trightbox
\setbox0=\vsplit\tleftbox to0pt \setbox1=\vsplit\trightbox to0pt
\printtablesA \egroup
\kern\dp\strutbox \medskip
}
\def\printtablesA{
\setbox0=\vsplit\tleftbox to16pt \setbox1=\vsplit\trightbox to16pt
\ifvoid0 \ifvoid1 \let\printtablesA=\relax \fi\fi
\ifx\printtablesA\relax \else \hbox{\tline0\kern15pt\tline1}\fi
\printtablesA
}
\def\tline#1{\ifvoid#1\kern\dimen#1 \else \vbox{\unvbox#1}\fi}

\vsize=3cm \raggedbottom % Just for testing

\tableleft{
a & b \\
c & d \\
e & f \\
g & h \\
i & j \\
k & l \\
m & n \\
o & p \\
a & b \\
c & d \\
e & f \\
g & h \\
i & j \\
k & l \\
m & n \\
o & p \\
}
\tableright{
q & r \\
s & t \\
u & v \\
w & x \\
y & z \\
q & r \\
s & t \\
u & v \\
w & x \\
y & z \\
}
\printtables

\end


In order to test this, you can run pdftex file or you can try this with LaTeX after LaTeX commands are added (\documentclass etc.). You needn't any special package.

Explanation The tables are saved to the \tleftbox and \trightbox first. If you need more tables, you may declare a new box \tnextbox (for example) and you can save next table here (or you can use another more descriptive name convention). Then the macro \printtables does an initial setting and runs \printtablesA repeatedly. This macro gets the first line from \tleftbox and \trightbox and moves them to the \box0 and \box1 using \vsplit primitive. You can add analogical process for next table by \setbox2=\vsplit\tnextbox to16pt for example. If all boxes are empty:

\ifvoid0 \ifvoid1 \ifvoid2 \let\printtablesA=\relax \fi\fi\fi


then the loop ends. The line constructed by \box0, \box1 is printed to the output using \hbox and \tline macro. For three tables it looks like

\hbox{\tline0\kern15pt\tline1\kern15pt\tline2}


The horizontal space between tables is specified here (15pt). The output is filled by such \hboxes and the list of these \hboxes is breakable into pages, of course.

• Helped me. If you could add bit explanation of adding more than two if possible and what's going on I will accept as the answer and grant the bounty. – Sterling Duchess Nov 6 '15 at 7:45
• \hline breaks the tables vertical alignment the one with less items gets pushed down. Is it possible to have this table have all cells bordered ? – Sterling Duchess Nov 9 '15 at 8:23

While Python cannot know when the table has reached the bottom of the page, it can make a very good guess. If all your entries have the same size more or less, you can just count the number of rows in your table and start a new table every n rows, where n depends on the geometry of your page, your font size, the entries to the table, etc.

Depending on the content of your table, it might also help the reader, if you always put the same number of rows on each page. Then, if you look for a specific row, you can estimate on which page you have to look.

• I want to consider Python solution as the last option. – Sterling Duchess Nov 4 '15 at 0:06

My suggestion is:

Take the following usepackages

longtable, datatool. Designing a table which can handle your task seems to be possible. There are a lot of questions unanswered.

What if table one is much longer than table 2 ? What if table one is much shorter than table 2 ? How do you want to handle this cases?

Maybe I have a solution for you, but currently I have to guess.

\documentclass[10pt]{article}
\usepackage{graphicx,fancyhdr,import,hyperref}
\usepackage{longtable}
\usepackage{datatool}

\begin{document}

\DTLsetseparator{,}
\begin{longtable}{|c|c|cccc|c|c|}\hline
\DTLforeach{ex}{\a=a, \b=b,\c=c,\d=d}%
{%
\DTLiffirstrow{}%
{%
\\%
}%
\protect\a&%
\protect\b&&&&&\protect\c&\protect\d%
}%
\end{longtable}

\end{document}


• Tables should just be top aligned and then extend down I think [t] It does not matter if one extends half the first page and other extends 3 pages and leaves lots of white space. – Sterling Duchess Nov 4 '15 at 0:07