2

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
  • 1
    You're defining the column width in the p type columns, and the overall width of the three columns is more than 145 pt wider than your text width. – Skillmon Oct 22 '20 at 6:32
  • Wouldn't a list like environment such as itemize or description be better suited for this kind of information? – leandriis Oct 22 '20 at 7:31
1

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}

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

enter image description here

enter image description here

1
  • Thank you very much, this is very helpful :), how can I mark it as the correct answer? – Mai Oct 22 '20 at 7:51
1

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|>{\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}
4
  • I tried this but it does not work perfectly as needed, thanks for your effort – Mai Oct 22 '20 at 7:56
  • @Mai: What exactly does not work? Do you get any error messages upon compiling or does the code just not result in the desired output? – leandriis Oct 22 '20 at 8:00
  • I am sorry, I didn't understand at the first time that you introduced two tables, and both work fine, thank you. – Mai Oct 22 '20 at 8:16
  • your answer is correct too – Mai Oct 22 '20 at 8:18

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.