TransWikia.com

Long Table with multicolumn width problem

TeX - LaTeX Asked on March 11, 2021

I am trying to use longtable package with multirow package, as shown in the example below, but the long table width exceeded the page width, could anyone help with this?

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}

enter image description here

2 Answers

The following changes your table in the following ways:

  • use only the available linewidth
  • remove multirow (it's useless in this example as the contents won't stay on the same page most likely and the alignment doesn't work automatically anyway for p-type columns)
  • use raggedright in the cells with much content (for that the array package is loaded and the L type column defined)
  • use dimexpr to calculate the available width somewhat automatically (you can change the values of myfirstcol and mysecondcol to get the result you want)
  • use L columns also in the joined cells of the first and second row (with dimexpr to calculate the width)
  • I removed center around longtable, that isn't necessary anyways, as longtable centres the tables (if you don't manually specify another alignment) and center only results in unwanted vertical space here (also the table is as wide as the text block allows, so it is always centred anyway)
  • use caption to format the continued caption as well (if you use an empty optional argument, longtable won't add this caption to the list of tables, shown in my MWE), I also altered the text of that follow-up caption.
  • I used extrarowheight to space out the cells a bit more (this gives better looks combined with hline and cline rules).
documentclass{article}
usepackage{longtable}
usepackage{array}
title{A longtable example}

newlengthmyfirstcol
newlengthmysecondcol
newlengthmythirdcol
newcolumntype{L}[1]{>{raggedrightarraybackslash}p{#1}}

begin{document}
listoftables

begingroup
setlengthmyfirstcol{8mm}
setlengthmysecondcol{2cm}
setlengthmythirdcol
  {dimexprlinewidth-myfirstcol-mysecondcol-6tabcolsep-4arrayrulewidth}
setlengthextrarowheight{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{dimexprmythirdcol+mysecondcol+2tabcolsep+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{dimexprmythirdcol+mysecondcol+2tabcolsep+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}

enter image description here

enter image description here

Correct answer by Skillmon on March 11, 2021

I suggest one of the two following redesigns. With this approach, your table fits onto a single page:

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|>{raggedrightarraybackslash}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>{raggedrightarraybackslash}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}

Answered by leandriis on March 11, 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