长表有多列宽度问题

长表有多列宽度问题

我正在尝试将 longtable 包与 multirow 包一起使用,如下例所示,但是长表宽度超出了页面宽度,有人可以帮忙吗?

\documentclass{article}
\usepackage{longtable}
\usepackage{multirow}
\title{A longtable example}

\begin{document}

\begin{center} 

 \begin{longtable}{|p{1cm}|p{3cm}|p{12cm}|}
 \caption{List Of Quality Metrics Used} \label{tab:qm} \\

  \hline \multicolumn{1}{|c|}{\textbf{Ref.}} & \multicolumn{2}{c|}{\textbf{Metrics Used}}  \\ \hline 
   \endfirsthead

  \multicolumn{3}{c}%
   {{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
  \hline \multicolumn{1}{|c|}{\textbf{Ref.}} & \multicolumn{2}{c|}{\textbf{Metrics Used}}  \\ \hline 
  \endhead

   \hline \multicolumn{3}{|r|}{{Continued on next page}} \\ \hline
   \endfoot

    \hline \hline
    \endlastfoot

    ref1 & \multicolumn{2}{l|}{ LOC-WORKED-ON, MAX-LOC-WORKED-ON, AVG-LOC-WORKED-ON, MAX-COMMITS, 
    AVG-COMMITS, MAX-LOC-ADDED, AVG-LOC-ADDED, MAX-LOC-DELETED, AVG-LOC-DELETED, MAX-LOC-WORKED, AVG- 
    LOC-WORKED,MAX-CODECHURN, AVG-CODECHURN, MAX-CHANGESET, AVG-CHANGESET, MAX-TIME-DIFF,MIN-TIME- 
    DIFF,AVG-TIME-DIFF, SINGLE-COMMITS} \\ \hline
    ref2 & \multicolumn{2}{l|}{ WMC, DIT, NOC, CBO, RFC, LCOM, Ca, Ce, NPM, LCOM3, LOC, DAM,MOA, MFA, 
    CAM, IC, CBM, AMC, Max-CC, Avg-CC.} \\ \hline
    \multirow{2}{*}{ref3}  & Dataset PC3 \& PC4: & LOC-BLANK
     BRACNH-COUNT,
     CALL-PAIRS,
     LOC-CODE-AND-COMMENTS,
     LOC-COMMENTS,
     CONDITION-COUNT,
     CYCLOMATIC-COMPLEXITY,
     CYCLIMATIC-DENSITY,
     DECISION-COUNT,
     DECISION-DENSITY,
     EDGE-COUNT,
     ESSENTIAL-COMPLEXITY,
     ESSENTIAL-DENSITY,
     LOC-EXECUTABLE,
     PARAMETER-COUNT,
     HALSTEAD-CONTENT,
     HALSREAD-DOFFICULTY,
     HALSTEAD-EFFORT,
     HALSTEAD-ERROR-EST,
     HALSTEAD-LENGTH,
     HALSTEAD LEVEL,
     HALSTEAD-VOLUME,
     MAINTENANCE-SEVERITY,
     MODIFIED-CONDITION-COUNT,
     MULTIPLE-CONDITION-COUNT,
     NODE-COUNT,
     NORMALIZED-CYCLOMATIC-COMPLEXITY,
     NUM-OPERANDS, 
     NUM-OPERATORS,
     NUM-UNIQUE-OPERANDS,
     NUM-UNIQYE-OPERATORS,
     NUMBER-OF-LINES,
     PERCENT-COMMENTS,
     LOC-TOTAL  \\ \cline{2-3}
                & Dataset KC2 & LOC-EXECUTABLE,
     CYCLOMATIC-COMPLEXITY,
     ESSENTIAL-COMPLEXITY,
     DESIGN-COMPLEXITY,
     NUM-OPERATORS-AND-OPERANDS,
     HALSTEAD-VOLUME,
     HALSTEAD-DIFFICULTY,
     HALSTEAD-INTEELIGENCE,
     HALSTEAD-EFFORT,
     HALSTEAD-NUM-DELIVERED-BUGS,
     HALSTEAD-PROG-TIME,
     HALSTEAD-CODE-LINE-COUNT,
     HALSTEAD-CODE-AND-COMMENT-LINE-COUNT,
     NUM-UNIQUE-OPERATORS,
     NUM-UNIQUE-OPERANDS,
     NUM-OPERATORS,
     NUM-OPERANDS,
     BRANCH-COUNT \\ \hline                            

     \end{longtable}

     \end{center}
     \end{document}

在此处输入图片描述

答案1

下面的操作将以下列方式改变您的表格:

  • 仅使用可用的\linewidth
  • 删除(在这个例子中它是无用的,因为内容很可能不会保留在同一页面上,并且对于-type 列,\multirow对齐不会自动起作用)p
  • 在包含大量内容的单元格中使用\raggedright(为此array加载了包并L定义了类型列)
  • 用于自动计算可用宽度(您可以更改和\dimexpr的值来获得所需的结果)\myfirstcol\mysecondcol
  • L在第一行和第二行的连接单元格中也使用列(用于\dimexpr计算宽度)
  • 我删除了center周围的longtable,反正那也没有必要,因为它使longtable表格居中(如果你没有手动指定另一种对齐方式)并且center只会导致这里出现不必要的垂直空间(而且表格的宽度与文本块允许的宽度一样宽,所以无论如何它总是居中的)
  • 用于\caption格式化连续标题(如果使用空的可选参数,longtable则不会将此标题添加到表格列表中,如我的 MWE 中所示),我还更改了后续标题的文本。
  • 我曾经\extrarowheight将单元格间隔得更大一些(这样与规则相结合可以获得更好的外观\hline\cline
\documentclass{article}
\usepackage{longtable}
\usepackage{array}
\title{A longtable example}

\newlength\myfirstcol
\newlength\mysecondcol
\newlength\mythirdcol
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{document}
\listoftables

\begingroup
\setlength\myfirstcol{8mm}
\setlength\mysecondcol{2cm}
\setlength\mythirdcol
  {\dimexpr\linewidth-\myfirstcol-\mysecondcol-6\tabcolsep-4\arrayrulewidth}
\setlength\extrarowheight{2pt}
 \begin{longtable}{|p{\myfirstcol}|L{\mysecondcol}|L{\mythirdcol}|}
 \caption{List Of Quality Metrics Used} \label{tab:qm} \\

  \hline \multicolumn{1}{|c|}{\textbf{Ref.}} & \multicolumn{2}{c|}{\textbf{Metrics Used}}  \\ \hline 
   \endfirsthead

   \caption[]{List Of Quality Metrics Used (continued)} \\
  \hline \multicolumn{1}{|c|}{\textbf{Ref.}} & \multicolumn{2}{c|}{\textbf{Metrics Used}}  \\ \hline 
  \endhead

   \hline \multicolumn{3}{|r|}{{Continued on next page}} \\ \hline
   \endfoot

    \hline \hline
    \endlastfoot

    ref1 &
    \multicolumn{2}{L{\dimexpr\mythirdcol+\mysecondcol+2\tabcolsep+\arrayrulewidth}|}{ LOC-WORKED-ON, MAX-LOC-WORKED-ON, AVG-LOC-WORKED-ON, MAX-COMMITS, 
    AVG-COMMITS, MAX-LOC-ADDED, AVG-LOC-ADDED, MAX-LOC-DELETED, AVG-LOC-DELETED, MAX-LOC-WORKED, AVG- 
    LOC-WORKED,MAX-CODECHURN, AVG-CODECHURN, MAX-CHANGESET, AVG-CHANGESET, MAX-TIME-DIFF,MIN-TIME- 
    DIFF,AVG-TIME-DIFF, SINGLE-COMMITS} \\ \hline
    ref2 &
    \multicolumn{2}{L{\dimexpr\mythirdcol+\mysecondcol+2\tabcolsep+\arrayrulewidth}|}{ WMC, DIT, NOC, CBO, RFC, LCOM, Ca, Ce, NPM, LCOM3, LOC, DAM,MOA, MFA, 
    CAM, IC, CBM, AMC, Max-CC, Avg-CC.} \\ \hline
    ref3  & Dataset PC3 \& PC4: & LOC-BLANK
     BRACNH-COUNT,
     CALL-PAIRS,
     LOC-CODE-AND-COMMENTS,
     LOC-COMMENTS,
     CONDITION-COUNT,
     CYCLOMATIC-COMPLEXITY,
     CYCLIMATIC-DENSITY,
     DECISION-COUNT,
     DECISION-DENSITY,
     EDGE-COUNT,
     ESSENTIAL-COMPLEXITY,
     ESSENTIAL-DENSITY,
     LOC-EXECUTABLE,
     PARAMETER-COUNT,
     HALSTEAD-CONTENT,
     HALSREAD-DOFFICULTY,
     HALSTEAD-EFFORT,
     HALSTEAD-ERROR-EST,
     HALSTEAD-LENGTH,
     HALSTEAD LEVEL,
     HALSTEAD-VOLUME,
     MAINTENANCE-SEVERITY,
     MODIFIED-CONDITION-COUNT,
     MULTIPLE-CONDITION-COUNT,
     NODE-COUNT,
     NORMALIZED-CYCLOMATIC-COMPLEXITY,
     NUM-OPERANDS, 
     NUM-OPERATORS,
     NUM-UNIQUE-OPERANDS,
     NUM-UNIQYE-OPERATORS,
     NUMBER-OF-LINES,
     PERCENT-COMMENTS,
     LOC-TOTAL  \\ \cline{2-3}
                & Dataset KC2 & LOC-EXECUTABLE,
     CYCLOMATIC-COMPLEXITY,
     ESSENTIAL-COMPLEXITY,
     DESIGN-COMPLEXITY,
     NUM-OPERATORS-AND-OPERANDS,
     HALSTEAD-VOLUME,
     HALSTEAD-DIFFICULTY,
     HALSTEAD-INTEELIGENCE,
     HALSTEAD-EFFORT,
     HALSTEAD-NUM-DELIVERED-BUGS,
     HALSTEAD-PROG-TIME,
     HALSTEAD-CODE-LINE-COUNT,
     HALSTEAD-CODE-AND-COMMENT-LINE-COUNT,
     NUM-UNIQUE-OPERATORS,
     NUM-UNIQUE-OPERANDS,
     NUM-OPERATORS,
     NUM-OPERANDS,
     BRANCH-COUNT \\ \hline                            
\end{longtable}
\endgroup

\end{document}

在此处输入图片描述

在此处输入图片描述

答案2

我建议采用以下两种重新设计方法之一。采用这种方法,您的表格可以放在一页上:

\documentclass{article}
\usepackage{xltabular}
\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{3pt}
\setlength{\cellspacebottomlimit}{\cellspacetoplimit}
\addparagraphcolumntypes{X}


\usepackage{booktabs}
\usepackage{enumitem}
\newlist{tabdescr}{description}{1}
\setlist[tabdescr]{before=\begin{minipage}[t]{\hsize}\raggedright,
                   after=\end{minipage}}


\begin{document}
\begin{table}
\caption{List Of Quality Metrics Used} \label{tab:qm}
 \begin{tabularx}{\textwidth}{|0l|>{\raggedright\arraybackslash}0{X}|}

  \hline \multicolumn{1}{|0c|}{\textbf{Ref.}} & \multicolumn{1}{0c|}{\textbf{Metrics Used}}  \\ \hline 

    ref1 &
    LOC-WORKED-ON, MAX-LOC-WORKED-ON, AVG-LOC-WORKED-ON, MAX-COMMITS, 
    AVG-COMMITS, MAX-LOC-ADDED, AVG-LOC-ADDED, MAX-LOC-DELETED, AVG-LOC-DELETED, MAX-LOC-WORKED, AVG- 
    LOC-WORKED,MAX-CODECHURN, AVG-CODECHURN, MAX-CHANGESET, AVG-CHANGESET, MAX-TIME-DIFF,MIN-TIME- 
    DIFF,AVG-TIME-DIFF, SINGLE-COMMITS \\ \hline
    ref2 &
     WMC, DIT, NOC, CBO, RFC, LCOM, Ca, Ce, NPM, LCOM3, LOC, DAM,MOA, MFA, 
    CAM, IC, CBM, AMC, Max-CC, Avg-CC\\ \hline
    ref3  & \textit{Dataset PC3 \& PC4:}  LOC-BLANK
     BRACNH-COUNT,
     CALL-PAIRS,
     LOC-CODE-AND-COMMENTS,
     LOC-COMMENTS,
     CONDITION-COUNT,
     CYCLOMATIC-COMPLEXITY,
     CYCLIMATIC-DENSITY,
     DECISION-COUNT,
     DECISION-DENSITY,
     EDGE-COUNT,
     ESSENTIAL-COMPLEXITY,
     ESSENTIAL-DENSITY,
     LOC-EXECUTABLE,
     PARAMETER-COUNT,
     HALSTEAD-CONTENT,
     HALSREAD-DOFFICULTY,
     HALSTEAD-EFFORT,
     HALSTEAD-ERROR-EST,
     HALSTEAD-LENGTH,
     HALSTEAD LEVEL,
     HALSTEAD-VOLUME,
     MAINTENANCE-SEVERITY,
     MODIFIED-CONDITION-COUNT,
     MULTIPLE-CONDITION-COUNT,
     NODE-COUNT,
     NORMALIZED-CYCLOMATIC-COMPLEXITY,
     NUM-OPERANDS, 
     NUM-OPERATORS,
     NUM-UNIQUE-OPERANDS,
     NUM-UNIQYE-OPERATORS,
     NUMBER-OF-LINES,
     PERCENT-COMMENTS,
     LOC-TOTAL  \\ \cline{2-2}
                & \textit{Dataset KC2} LOC-EXECUTABLE,
     CYCLOMATIC-COMPLEXITY,
     ESSENTIAL-COMPLEXITY,
     DESIGN-COMPLEXITY,
     NUM-OPERATORS-AND-OPERANDS,
     HALSTEAD-VOLUME,
     HALSTEAD-DIFFICULTY,
     HALSTEAD-INTEELIGENCE,
     HALSTEAD-EFFORT,
     HALSTEAD-NUM-DELIVERED-BUGS,
     HALSTEAD-PROG-TIME,
     HALSTEAD-CODE-LINE-COUNT,
     HALSTEAD-CODE-AND-COMMENT-LINE-COUNT,
     NUM-UNIQUE-OPERATORS,
     NUM-UNIQUE-OPERANDS,
     NUM-OPERATORS,
     NUM-OPERANDS,
     BRANCH-COUNT \\ \hline                            
\end{tabularx}
\end{table}


\begin{table}
\caption{List Of Quality Metrics Used} \label{tab:qm}
 \begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}X}

  \toprule
  Ref. & Metrics Used \\ 
  \midrule 

    ref1 &
    LOC-WORKED-ON, MAX-LOC-WORKED-ON, AVG-LOC-WORKED-ON, MAX-COMMITS, 
    AVG-COMMITS, MAX-LOC-ADDED, AVG-LOC-ADDED, MAX-LOC-DELETED, AVG-LOC-DELETED, MAX-LOC-WORKED, AVG- 
    LOC-WORKED,MAX-CODECHURN, AVG-CODECHURN, MAX-CHANGESET, AVG-CHANGESET, MAX-TIME-DIFF,MIN-TIME- 
    DIFF,AVG-TIME-DIFF, SINGLE-COMMITS \\ 
    \midrule
    ref2 &
     WMC, DIT, NOC, CBO, RFC, LCOM, Ca, Ce, NPM, LCOM3, LOC, DAM,MOA, MFA, 
    CAM, IC, CBM, AMC, Max-CC, Avg-CC\\
    \midrule
    ref3  & \begin{tabdescr}\item{Dataset PC3 \& PC4:}  LOC-BLANK
     BRACNH-COUNT,
     CALL-PAIRS,
     LOC-CODE-AND-COMMENTS,
     LOC-COMMENTS,
     CONDITION-COUNT,
     CYCLOMATIC-COMPLEXITY,
     CYCLIMATIC-DENSITY,
     DECISION-COUNT,
     DECISION-DENSITY,
     EDGE-COUNT,
     ESSENTIAL-COMPLEXITY,
     ESSENTIAL-DENSITY,
     LOC-EXECUTABLE,
     PARAMETER-COUNT,
     HALSTEAD-CONTENT,
     HALSREAD-DOFFICULTY,
     HALSTEAD-EFFORT,
     HALSTEAD-ERROR-EST,
     HALSTEAD-LENGTH,
     HALSTEAD LEVEL,
     HALSTEAD-VOLUME,
     MAINTENANCE-SEVERITY,
     MODIFIED-CONDITION-COUNT,
     MULTIPLE-CONDITION-COUNT,
     NODE-COUNT,
     NORMALIZED-CYCLOMATIC-COMPLEXITY,
     NUM-OPERANDS, 
     NUM-OPERATORS,
     NUM-UNIQUE-OPERANDS,
     NUM-UNIQYE-OPERATORS,
     NUMBER-OF-LINES,
     PERCENT-COMMENTS,
     LOC-TOTAL  \end{tabdescr} \\ \cmidrule{2-2}
                & \begin{tabdescr}\item{Dataset KC2} LOC-EXECUTABLE,
     CYCLOMATIC-COMPLEXITY,
     ESSENTIAL-COMPLEXITY,
     DESIGN-COMPLEXITY,
     NUM-OPERATORS-AND-OPERANDS,
     HALSTEAD-VOLUME,
     HALSTEAD-DIFFICULTY,
     HALSTEAD-INTEELIGENCE,
     HALSTEAD-EFFORT,
     HALSTEAD-NUM-DELIVERED-BUGS,
     HALSTEAD-PROG-TIME,
     HALSTEAD-CODE-LINE-COUNT,
     HALSTEAD-CODE-AND-COMMENT-LINE-COUNT,
     NUM-UNIQUE-OPERATORS,
     NUM-UNIQUE-OPERANDS,
     NUM-OPERATORS,
     NUM-OPERANDS,
     BRANCH-COUNT \end{tabdescr} \\ 
    \bottomrule                            
\end{tabularx}
\end{table}


\end{document}

相关内容