6

I have a booktabs (longtable) and I make a column Order. I have to input the number 1, 2, 3, ... by hand. How can I make a column that the numbers are counted automatically? My code is

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{array,longtable,fourier}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
 \usepackage{booktabs}
\begin{document}
\begin{longtable}{*{5}{>{$}c<{$}}}
\endfirsthead  % blank header on first page
\multicolumn{5}{@{}l}{(\emph{array continued from previous page})}
\endhead
\multicolumn{5}{r@{}}{(\emph{array continued on next page})}
\endfoot
\endlastfoot
\begin{tabular}{@{}cllll@{}}
\toprule[1pt]
Order & point $A$  & point $B$  & point $C$  & point $D$  \\ \midrule
1     & $(1,2,-6)$ & $(1,2,-6)$ & $(1,2,-6)$ & $(1,2,-6)$ \\
2     & $(1,2,-6)$ & $(1,2,-6)$ & $(1,2,-6)$ & $(1,2,-6)$ \\
3     & $(1,2,-6)$ & $(1,2,-6)$ & $(1,2,-6)$ & $(1,2,-6)$ \\ \bottomrule
\end{tabular}
\end{longtable}
\end{document}

enter image description here

  • 2
    Your use of tabular inside of longtable utterly defeats the purpose of longtable, which is to allow pagebreaks inside the tabular material. – Mico Mar 05 '18 at 09:28
  • See https://tex.stackexchange.com/q/58138/15925 and https://tex.stackexchange.com/q/21243/15925 – Andrew Swann Mar 05 '18 at 09:31
  • @Mico Instead input, example $\left (\dfrac{1}{2}, \dfrac{3}{4}\right )$, Can I reduce to \left (\dfrac{1}{2}, \dfrac{3}{4}\right ) (obmit $$) but receive the same result? – minhthien_2016 Mar 05 '18 at 09:40
  • @minhthien_2016 - Please see my answer, in which I make the four data columns be in math mode automatically; this should let you insert expressions such as \left( \dfrac{1}{2}, \dfrac{3}{4} \right) in various cells. However, I don't think it's a good idea to use \dfrac in tabular material. Why not use inline-fraction notation, i.e., (1/2, 3/4)? – Mico Mar 05 '18 at 09:55

1 Answers1

4

The following solution automates the automatic insertion of row numbers in the first column in the body of the longtable environment, in a way that allows cross-referencing rows using the basic \label-\ref system. The key part of the solution is the creation of a dedicated column type, called N in the code below.

The solution also gets rid of the tabular environment inside the longtable environment. Having a tabular environment inside a longtable utterly defeats the purpose of using a longtable environment, which is to allow page breaks, as needed, in the interior of a longtable.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage[margin=2cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{fourier} % change font

\usepackage{amsmath,array,longtable,booktabs}
\newcounter{rownum}
\newcolumntype{N}{>{\refstepcounter{rownum}\therownum}l}
\newcolumntype{C}{>{$}c<{$}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macros
\newcommand\ml[1]{\multicolumn{1}{l}{#1}} 

\begin{document}

\begin{longtable}{ N *{4}{C} }
%% headers and footers
\toprule[1pt]
\ml{Order} & \mc{point $A$} & \mc{point $B$} & \mc{point $C$} & \mc{point $D$}  \\
\midrule
\endfirsthead  % blank header on first page

\multicolumn{5}{@{}l}{(\emph{array continued from previous page})}\\
\addlinespace
\ml{Order} & \mc{point $A$} & \mc{point $B$} & \mc{point $C$} & \mc{point $D$}  \\
\midrule
\endhead

\midrule
\multicolumn{5}{r@{}}{(\emph{array continued on next page})}
\endfoot

\bottomrule
\endlastfoot

%% body of longtable
%% Note the generally "empty" first column -- empty except for \label directives
 & (1,2,-6) & (1,2,-6) & (1,2,-6) & (1,2,-6) \\
 & (1,2,-6) & (1,2,-6) & (1,2,-6) & (1,2,-6) \\
 & (1,2,-6) & (1,2,-6) & (1,2,-6) & (1,2,-6) \\
 & (1,2,-6) & (1,2,-6) & (1,2,-6) & (1,2,-6) \\
 \label{row:v} & (1,2,-6) & (1,2,-6) & (1,2,-6) & (1,2,-6) \\
 & (1,2,-6) & (1,2,-6) & (1,2,-6) & (1,2,-6) \\
\end{longtable}

A cross-reference to row \ref{row:v} of the table.

\end{document}
Mico
  • 506,678