多行多列的长表

多行多列的长表

首先,感谢您的帮助!这是我遇到的问题:

我正在尝试创建一个表格,其中将列出一系列命令及其说明。为此,我需要能够使用多列和多行功能。此外,还有 toprule、midrule 和 bottomnrule。

问题是表格长度超过一页,所以我需要它继续到下一页。我找到了 longtable 包,但我似乎无法弄清楚如何整合我需要的所有功能并让它正常工作。

我将附上常规表格的代码。有人可以帮我将它转换为 longtable 并保持格式不变吗?如果有人能帮助我并解释它的工作原理,我将不胜感激,因为我想学习它以备将来使用。非常感谢!


我的代码

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

输出

在此处输入图片描述

答案1

以下应该作为起点。我将标题移到了表格的开头。如果您希望列标题在表格的第二页及后续页面上重复,请查看 od\enfirstthead和 的区别\endhead

正如评论中提到的,当您想要组合longtablehyperref和时,包的加载顺序很重要,arydshln因此请确保按上述顺序加载这些包。还请清理我们的前言,以确保您只加载一次包。(我已经开始并注释掉一些重复项。)

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

相关内容