Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
! Package array Error: Illegal pream-token (R): `c' used. –  David Carlisle Aug 22 '12 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..... –  David Carlisle Aug 22 '12 at 13:27
add comment

1 Answer

up vote 4 down vote accepted

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

share|improve this answer
    
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? –  Costanza Aug 23 '12 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} –  Costanza Aug 23 '12 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 –  Costanza Aug 23 '12 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}% } –  Costanza Aug 23 '12 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] –  Costanza Aug 23 '12 at 12:20
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.