Hello again,
I am trying to change the solution Clemens kindly provided in answer to my first post. There are two changes I need to do: using a
longtable
environment instead of a
tabular
one, and making possible to draw horizontal lines, as done with
\hline
s in the standard environments. The former was easy to do, but in the latter I could not make out how to avoid "Misplaced
\noalign
" errors in the particular situation of
\hline
commands at the beginning of a line.
To generate a
hline
I added a new local command
\myhline
that adds a special item
my@hline
to the list of items. Maybe there is a better name to use here in order to make sure this is not a user item: any suggestion?. This special item is later on dealt with by the items handler as follows: if the current line is empty (the items counter is 1) issue a
\hline
, otherwise reset the items counter to 1 and issue
\\\hline
. The errors I got are for the first case and I really cannot make out what is going on.
There is also an issue concerning the embedding of environments in this environment. The solution I have defines the items list globally and at the beginning of the environment the list is reset to undefined. This clearly does not allow for this environment to be embedded in itself. A proper solution would be to save any existing list and restore it at the end, but I am not confident enough to try coding that.
Here is the current state of the code with changes by me marked with comments starting by
%%M
:
Code: Select all
\documentclass{article}
% always useful when handling tables:
\usepackage{array}
% useful programming macros, e.g. list handling, number comparison...:
\usepackage{etoolbox}
%%M tables may extend to next pages:
\usepackage{longtable}
% make @ a letter so it can be used in command names:
\makeatletter
\newcounter{ncols}
\newcounter{mytab@tmp}
%%M changed the environment name from mytabular to mylongtable
\newenvironment{mylongtable}[1][\arabic{ncols}]{%
% at the beginning of the environment:
%%M in order to have embedded environments (but not mylongtable)
%% the list of items must be set globally and undefined here
\let\mytab@items\un@defined%
% `\tabitem', only defined inside the environment,
% adds the items to a list:
%%M using \listgadd instead of \listadd
\def\tabitem##1{\listgadd{\mytab@items}{##1}}%
%%M '\myhline' adds a special item my@hline to the items list
\def\myhline{\tabitem{my@hline}}%
Up to here I changed the environment name, reset the items list to undefined, replaced
\listadd
by
\listgadd
, and defined
\myhline
that uses
\tabitem
to add a special item to the items list.
There are no other changes in the code for the beginning of the environment. In the code for the end of the environment the items handler was changed as described above, and the
tabular
was replaced by
longtable
:
Code: Select all
% if the number of columns is set to 0 or less use one column:
\ifnumless{#1}{1}
{\def\mytab@ncols{1}}
{\def\mytab@ncols{#1}}%
% initiate temporary counter:
\setcounter{mytab@tmp}{1}%
}{%
% at the end of the environment:
% `\mytab@handler' is the list handler:
%%M process my@hline items (original code in the else part)
\ifstrequal{my@hline}{##2}{%
%%M end current line if not empty and draw line
\ifnumequal{\value{mytab@tmp}}{1}{\hline}{%
\setcounter{mytab@tmp}{1}\\\hline}}{%
% typeset the item:
##2%
% if there are columns left add `&' and step the counter
% else add `\\' and reset the counter:
\ifnumless{\value{mytab@tmp}}{##1}
{\stepcounter{mytab@tmp}&}
{\setcounter{mytab@tmp}{1}\\}%
}%
}%
%%M using longtable instead of tabular
\begin{longtable}{*{\mytab@ncols}{l}}
\forlistloop{\mytab@handler{\mytab@ncols}}{\mytab@items}
\end{longtable}%
}
% make @ other again:
\makeatother
\begin{document}
\setcounter{ncols}{5}
\begin{mylongtable}
%\myhline % yields an ! Misplaced \noalign error
%\myhline % yields an ! Misplaced \noalign error
\tabitem{one}
\tabitem{two}
\tabitem{three}\myhline % this one works (position 3)
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}
\tabitem{eigth}%\myhline % at end of line: yields error
\tabitem{nine}
\tabitem{ten}
\tabitem{eleven}
\end{mylongtable}
\bigskip
\begin{mylongtable}[3]
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\tabitem{one}
\tabitem{two}
\tabitem{three}
\tabitem{four}
\tabitem{five}
\tabitem{six}
\tabitem{seven}\myhline
\end{mylongtable}
\end{document}
The body of the document has a first table with some
\myhline
commands commented out because they cause errors. The second table is a long one. Changing its number of columns will in some cases cause errors because of the
\myhline
commands getting to the beginning of lines.
Thanks in advance for any help on this.
Regards,
Miguel