1

First of all, thank you for trying to help! This is the problem that I have:

I am trying to create a table that will list a bunch of commands and their description. For that, I need to be able to use multicolumn and multirow features. Also, toprule, midrule, and bottomnrule.

The problem is that the table is longer than one page so I need it to continue on the next page. I found about the longtable package but I cannot seem to be able to figure how to incorporate all of the features I need and still make it work.

I will attach my code for the regular table. Can someone please help me convert it to longtable and keep the formatting the same? I would really appreciate it if someone can help me and also explain how it works because I want to learn it for future use. Big thank you!!!


My Code

\documentclass[10pt,english, openany, twoside]{book}

\usepackage[table]{xcolor}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Loading packages that alter the style
\usepackage[]{graphicx}
\usepackage[]{color}
\usepackage{alltt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\setlength{\parskip}{\smallskipamount}
\setlength{\parindent}{0pt}

% Set page margins
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}

% Package used for placeholder text
\usepackage{lipsum}

% Prevents LaTeX from filling out a page to the bottom
\raggedbottom

% Adding both languages
\usepackage[english]{babel}

% All page numbers positioned at the bottom of the page
\usepackage{fancyhdr}

\pagestyle{fancy}

\renewcommand{\headrulewidth}{0.0pt}

\fancyhead[LE,RO]{}
\fancyhead[RE,LO]{}
\fancyhead[CE,CO]{}
\fancyfoot[CE,CO]{\thepage}


% Adds table captions above the table per default
\usepackage{float}
%\floatstyle{plaintop}
%\restylefloat{table}

% Adds space between caption and table
\usepackage{caption}
\captionsetup[table]{position=bottom}

% Adds hyperlinks to references and ToC
\usepackage{hyperref}
\hypersetup{hidelinks,linkcolor = black} % Changes the link color to black and hides the hideous red border that usually is created


%Document Styling% <---------%
%%%%%%%%%%%%%%%%%%

\usepackage{float}
\usepackage{array}
\usepackage{hhline} 
\usepackage{arydshln}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{framed}
\usepackage{mdframed} % This frames the sections. However, a command \begin{mdframed}[options] is needed
\usepackage{multicol} % To change column separation use: \setlength{\columnsep}{-3in}
\usepackage{enumitem} % This package removes the indent on the enumeated list. Allows for the command [Leftmargin=*]

%%%%%%%%%%%%%%%%%%


%Graphics% <---------%
%%%%%%%%%%

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xcolor}

%%%%%%%%%%


%Font% <---------%
%%%%%%

\usepackage[T1]{fontenc}
\usepackage{textcomp} % 
\usepackage{hyperref} 
\usepackage{url}
\urlstyle{same}

%%%%%%


%Mathematics% <---------%
%%%%%%%%%%%%%

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm} 
\usepackage{physics}
\usepackage{siunitx} 

%For Table%
%%%%%%%%%%%

\usepackage{booktabs}

\sisetup{
    round-mode          = places, % Rounds numbers
    round-precision     = 2, % to 2 places
}
\usepackage{colortbl}
\usepackage{xfrac}

\definecolor{Gray}{gray}{0.85}

%dashed line
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}

\definecolor{codegray}{gray}{0.9}

\usepackage{longtable}

\usepackage{booktabs}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Starts the document
\begin{document}
    
    
     \begin{table}[h!]
        \centering
        \begin{tabular}{llll}
            \toprule
            \multicolumn{4}{c}{\textbf{Serial Command List}} \\
            \toprule
            \textbf{Name} & \textbf{Command} & \textbf{Reply} & \textbf{Description} \\
            \midrule
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            %
            \bottomrule
        \end{tabular} 
        \caption{Serial Command List}
        \label{tab:command-list} 
     \end{table}
    

\end{document}

Output

enter image description here

5
  • What did you try so far in oder to convert your table from tabular to longtable? – leandriis Jan 26 at 17:58
  • Please also clean up your preamble and make sure, you load packages only once. – leandriis Jan 26 at 18:18
  • How is your code related to multirow. You mention it in your question but the code you show des not even load the multirow package. Please explain. – leandriis Jan 26 at 18:25
  • 1
    Since you want to combine longtable with dashed horizontal lines from arydshln and also load hyperref, please be aware that the correct load order to packages is \usepackage{longtable}\usepacakge{hyperref}\usepackage{arydshln}. (See also: section "9.1.4 arydshln, longtable" of the hyperref manual.) – leandriis Jan 26 at 18:27
  • I ended up using another tabular environment to create multiple rows. I did not upload any of my tries of using longtable because it was a mess. I would need help converting this to longtable and that was the reason I asked this question. – Vladimir Jan 26 at 19:19
2

The following whould serve as a point to start from. I moved the caption to the beginning of the table. If you want the column headers to repeat on the second and following pages of your table, take a look at the difference od \enfirstthead and \endhead.

As already mentioned in the comments, the load order of packages matters, when you want to combine longtable, hyperref and arydshln, so make sure, these packages are loaded in the mentioned order. Please also clean up wour preamble to make sure you only load packages once. (I already started and commented out some of the duplicates.)

\documentclass[10pt,english, openany, twoside]{book}

\usepackage[table]{xcolor}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Loading packages that alter the style
\usepackage[]{graphicx}
%\usepackage[]{color} %%% not needed. You already load xcolor.
\usepackage{alltt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\setlength{\parskip}{\smallskipamount}
\setlength{\parindent}{0pt}

% Set page margins
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}

% Package used for placeholder text
\usepackage{lipsum}

% Prevents LaTeX from filling out a page to the bottom
\raggedbottom

% Adding both languages
\usepackage[english]{babel}

% All page numbers positioned at the bottom of the page
\usepackage{fancyhdr}

\pagestyle{fancy}

\renewcommand{\headrulewidth}{0.0pt}

\fancyhead[LE,RO]{}
\fancyhead[RE,LO]{}
\fancyhead[CE,CO]{}
\fancyfoot[CE,CO]{\thepage}


% Adds table captions above the table per default
\usepackage{float}
%\floatstyle{plaintop}
%\restylefloat{table}

% Adds space between caption and table
\usepackage{caption}
\captionsetup[table]{position=bottom}


%Document Styling% <---------%
%%%%%%%%%%%%%%%%%%

\usepackage{float}
\usepackage{array}
\usepackage{hhline} 
%\usepackage{caption} % already loaded
\usepackage{subcaption}
\usepackage{framed}
\usepackage{mdframed} % This frames the sections. However, a command \begin{mdframed}[options] is needed
\usepackage{multicol} % To change column separation use: \setlength{\columnsep}{-3in}
\usepackage{enumitem} % This package removes the indent on the enumeated list. Allows for the command [Leftmargin=*]

%%%%%%%%%%%%%%%%%%


%Graphics% <---------%
%%%%%%%%%%

\usepackage{graphicx} % already loaded
\usepackage{tikz}
\usetikzlibrary{calc}
%\usepackage{xcolor} % already loaded.

%%%%%%%%%%


%Font% <---------%
%%%%%%

%\usepackage[T1]{fontenc} % already loaded before
\usepackage{textcomp} % 
\usepackage{hyperref}
\hypersetup{hidelinks,linkcolor = black} 
\usepackage{url}
\urlstyle{same}

%%%%%%


%Mathematics% <---------%
%%%%%%%%%%%%%

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm} 
\usepackage{physics}
\usepackage{siunitx} 

%For Table%
%%%%%%%%%%%

\usepackage{booktabs}

\sisetup{
    round-mode          = places, % Rounds numbers
    round-precision     = 2, % to 2 places
}
%\usepackage{colortbl} % already loaded by \usepackage[table]{xcolor}
\usepackage{xfrac}

\definecolor{Gray}{gray}{0.85}

\usepackage{longtable} % make sure to load longtable before arydshln!

%dashed line
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}

\definecolor{codegray}{gray}{0.9}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Starts the document
\begin{document}
    

        \begin{longtable}{llll}
                \caption{Serial Command List}
        \label{tab:command-list} \\
            \toprule
            \multicolumn{4}{c}{\textbf{Serial Command List}} \\
            \toprule
            \textbf{Name} & \textbf{Command} & \textbf{Reply} & \textbf{Description} \\
            \midrule
            \endfirsthead
            \bottomrule
            \endlastfoot
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \begin{tabular}{@{}r@{\;}l@{}} 
                                                            n = & power percentage\\
                                                                & \textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                                                            e.g.& \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                                                            r = & ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            \hdashline
            %
            Set Power & \texttt{\$P,n}  & \texttt{\$Pr} & \noindent\begin{tabular}{@{}l} 
                n = power percentage\\
                \phantom{n = }\textbf{Note}: Up to 3 digits and 2 decimal places. \\\\
                
                e.g. \colorbox{codegray}{\texttt{\$P,57.5}} sets power to 57.5\% \\\\
                
                r = ERROR CODE (0 if no error)
            \end{tabular} \\
            %
        \end{longtable} 
    

\end{document}
5
  • Thank you so much! This was exactly what I needed!! – Vladimir Jan 26 at 20:27
  • A question: How do I move the caption back to the bottom? I have tried it in a few ways but LaTeX is treating it as another row and it keeps putting a line under it. Could you help? – Vladimir Jan 26 at 20:28
  • 1
    @Vladimir: A table's caption is usually placed above the caption which is why I moved it. Placing the captions after the table would be especially unsual here, since your table spans multiple pages. If you however insist, you can use the following sode: – leandriis Jan 26 at 21:02
  • 1
    \begin{longtable}{llll} \toprule \multicolumn{4}{c}{\textbf{Serial Command List}} \\ \toprule \textbf{Name} & \textbf{Command} & \textbf{Reply} & \textbf{Description} \\ \midrule \endfirsthead \bottomrule \caption{Serial Command List} \label{tab:command-list} \\ \endlastfoot – leandriis Jan 26 at 21:02
  • Thank you truly for all your help and the advice! – Vladimir Jan 26 at 21:09

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.