# How can I make a column that the number is counted automatically?

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<{$}}}
\multicolumn{5}{@{}l}{(\emph{array continued from previous page})}
\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} • Your use of tabular inside of longtable utterly defeats the purpose of longtable, which is to allow pagebreaks inside the tabular material. – Mico Mar 5 '18 at 9:28
• – Andrew Swann Mar 5 '18 at 9: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 5 '18 at 9: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 5 '18 at 9:55

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. \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{\multicolumn{1}{c}{#1}} % handy shortcut macros
\newcommand\ml{\multicolumn{1}{l}{#1}}

\begin{document}

\begin{longtable}{ N *{4}{C} }
\toprule[1pt]
\ml{Order} & \mc{point $A$} & \mc{point $B$} & \mc{point $C$} & \mc{point $D$}  \\
\midrule

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

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

• Can I align the numbers in column Order is center? – minhthien_2016 Mar 5 '18 at 9:56
• @minhthien_2016 - Absolutely. Just change \newcolumntype{N}{>{\refstepcounter{rownum}\therownum}l} to \newcolumntype{N}{>{\refstepcounter{rownum}\therownum}c}, i.e., change the last l to c. – Mico Mar 5 '18 at 9:57
• How can I align left every columns? – minhthien_2016 Apr 1 '18 at 12:18
• @minhthien_2016 - Have you tried changing the underlying column types from c to l? – Mico Apr 1 '18 at 13:18
• I am trying to find where is underlying column? – minhthien_2016 Apr 1 '18 at 14:59