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 5 '18 at 9:28
  • @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
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}
|improve this answer|||||
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.