TransWikia.com

Longtable With Multirow and Multicolumn

TeX - LaTeX Asked on February 12, 2021

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}
setlengthdashlinedash{0.2pt}
setlengthdashlinegap{1.5pt}
setlengtharrayrulewidth{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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

One Answer

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}
setlengthdashlinedash{0.2pt}
setlengthdashlinegap{1.5pt}
setlengtharrayrulewidth{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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} & noindentbegin{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}

Correct answer by leandriis on February 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP