1

How do I set the multicolumn width in longtable cells? If you run my MWE, you'll see that the first row of my longtable exceeds page limits.

\documentclass[11pt,a4paper,oneside]{report}
\usepackage{setspace}
\usepackage{array}
\usepackage{covington}
\usepackage[danish,english]{babel}
\usepackage{longtable}
\usepackage{caption}
\usepackage{multirow}

\begin{document}


\begin{longtable}{Rp{9cm}}
\caption{Example}\\
\endfirsthead
\caption[]{(continued)}\\
\endhead
\endlastfoot
\multicolumn{2}{l}{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.}\\
\\
\textsc{Lorem}&"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."\\
{\dag} \textsc{Ipsum}&"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."\\
\end{longtable}

\end{document}
2
  • ! Package array Error: Illegal pream-token (R): `c' used. Aug 22, 2012 at 13:25
  • Please test your MWE before posting. Ideally the multicolumn needs to be 9cm plus 2 tabcolsep wider than an R column, but you haven't defined that so we can't say..... Aug 22, 2012 at 13:27

1 Answer 1

4

You are lucky, because package longtable records the width of the columns in the .aux file. That can be used to calculate the width of the first two columns.

Macro \GetLongtableWidths extracts the widths of the next longtable. It is called just before \begin{longtable}. Inside the p specification \wdcal is used. The first argument is a minimal width that is used if the widths are not yet in the .aux file. This width should be smaller or at most equal to the final width. The second argument of \wdcal takes an expression. \wdcol<colnum> is replaced by the width of the column <colnum>. Because the column specification p{...} of \multicolumn does not suppress \tabcolsep (at the left and right of the column), two \tabcolsep needs to be subtracted.

\documentclass[11pt,a4paper,oneside]{report}
\usepackage{setspace}
\usepackage{array}
\usepackage{covington}
\usepackage[danish,english]{babel}
\usepackage{longtable}
\usepackage{caption}
\usepackage{multirow}

\makeatletter
\newcommand*{\GetLongtableWidths}{%
  \begingroup
    \count@=\value{LT@tables}%
    \advance\count@\@ne
    \edef\tmp{LT@\romannumeral\count@}%
    \@ifundefined{\tmp}{%
    }{%
      \count@=\z@
      \def\LT@entry##1##2{%
        \advance\count@\@ne
        \expandafter
        \gdef\csname @wdcol\the\count@\endcsname{##2}%
      }%
      \csname\tmp\endcsname
    }%
  \endgroup
}
\newcommand*{\wdcol}[1]{%
  \@ifundefined{@wdcol\number#1}%
  {0pt}%
  {\csname @wdcol\number#1\endcsname}%
}
\newcommand*{\wdcal}[2]{%
  \ifdim\dimexpr(#2)\relax<0pt %
    \dimexpr(#1)\relax
  \else
    \dimexpr(#2)\relax
  \fi
}
\makeatother

\begin{document}

\GetLongtableWidths
\begin{longtable}{rp{9cm}}
\caption{Example}\\
\endfirsthead
\caption[]{(continued)}\\
\endhead
\endlastfoot
\multicolumn{2}{p{\wdcal{9cm}{\wdcol{1}+\wdcol{2}-2\tabcolsep}}}{Lorem ipsum dolor s
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat  
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id   
est laborum.}\\
\\
\textsc{Lorem}&"Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat  
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id   
est laborum."\\
{\dag} \textsc{Ipsum}&"Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit 
anim id est laborum."\\
\end{longtable}

\end{document}

Result

6
  • Thank you very much! I still have some problems, though, which might be related to my preamble. Your example works very well by itself but it gets messed up when I put it in my document. I am providing here my full preamble: maybe there are conflicts?
    – Cost
    Aug 23, 2012 at 12:16
  • \documentclass[11pt,a4paper,oneside]{report} \pagestyle{headings} \usepackage{setspace} \usepackage{array} \usepackage{covington} \usepackage[danish,english]{babel} \usepackage{natbib} \bibliographystyle{plain} \usepackage{pslatex} \usepackage{float} \usepackage{graphicx} \usepackage{titlesec} \usepackage{amssymb} \usepackage{typearea}
    – Cost
    Aug 23, 2012 at 12:18
  • \usepackage[a4paper]{geometry} \geometry{includefoot=false, heightrounded=true, top=4.5cm, bottom=6cm, left=4.7cm, right=4.7cm} \renewcommand{\bibname}{References} \usepackage[toc,page]{appendix} \usepackage{longtable} \usepackage{caption} \newif\ifappendix % snippet appendice \makeatletter \def\LT@c@ption#1[#2]#3{% \LT@makecaption#1\fnum@table{#3}% \def\@tempa{#2}% \ifx\@tempa\@empty\else {\let\\\space \addcontentsline{\ifappendix toc\else lot\fi}{table}{\protect\numberline{\thetable}{#2}}}% \fi} \makeatother
    – Cost
    Aug 23, 2012 at 12:18
  • \makeatletter % snippet for longtable multicolumn title \newcommand*{\GetLongtableWidths}{% \begingroup \count@=\value{LT@tables}% \advance\count@\@ne \edef\tmp{LT@\romannumeral\count@}% \@ifundefined{\tmp}{% }{% \count@=\z@ \def\LT@entry##1##2{% \advance\count@\@ne \expandafter \gdef\csname @wdcol\the\count@\endcsname{##2}% }% \csname\tmp\endcsname }% \endgroup } \newcommand*{\wdcol}[1]{% \@ifundefined{@wdcol\number#1}% {0pt}% {\csname @wdcol\number#1\endcsname}% }
    – Cost
    Aug 23, 2012 at 12:19
  • \newcommand*{\wdcal}[2]{\ifdim\dimexpr(#2)\relax<0pt\dimexpr(#1)\relax \else \dimexpr(#2)\relax \fi } \makeatother \usepackage[bitstream-charter]{mathdesign} \usepackage[T1]{fontenc} \usepackage{microtype} \usepackage{multirow} \usepackage{morefloats} \usepackage{booktabs} \newcolumntype{R}{>{\raggedleft\arraybackslash}p{2cm}} \usepackage[bottom,norule]{footmisc} \setlength\belowcaptionskip{1\baselineskip} \usepackage{lscape} \usepackage[hidelinks]{hyperref} \usepackage{bookmark} \titlespacing{\section}{0pt}{40pt}{20pt}[0pt] \titlespacing{\subsection}{0pt}{40pt}{20pt}[0pt]
    – Cost
    Aug 23, 2012 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .