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 can I use \multicolumn in a longtable environment to span all columns except for one? The \LT@cols counter is probably the way to go. However, doing arithmetics in LaTeX seems somewhat unconventional. Related to multicolumn-command which automatically uses all columns of the table?. What's needed is "\LT@cols minus one".

share|improve this question
    
You can use \numexpr\LT@cols-1. Without a MWE, answers too will be trivial ;) –  Harish Kumar Mar 29 at 10:29
    
Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Christian Hupfer Mar 29 at 10:46

1 Answer 1

up vote 7 down vote accepted

You can use \numexpr\LT@cols-1 and define your own multicolumn command like

\makeatletter
\def\mymc#1#2{%
  \multicolumn{\numexpr\LT@cols-1}{#1}{#2}%              %%#1 = alignment, #2 = content
}
\makeatother

and use it like

\mymc{r}{abc}   

Full code:

\documentclass[]{scrbook}

\usepackage{booktabs, longtable}

\makeatletter
\def\mymc#1#2{%
  \multicolumn{\numexpr\LT@cols-1}{#1}{#2}%
}
\makeatother

\begin{document}

\begin{longtable}{p{2cm}p{2cm}p{2cm}ll}

\toprule
A & B & C & D & E\\
\midrule
\endfirsthead

\mbox{table continued from last page}\\
\toprule
A & B & C & D\\

\midrule
\endhead
%\bottomrule
\endfoot
\bottomrule
\endlastfoot

\mymc{r}{abc} & d\\
 \midrule
a & \mymc{c}{bcd}\\
 & b & c & d\\
 & b & c & d\\

\end{longtable}
\end{document}

enter image description here

share|improve this answer
    
Harish's answer have helped me to achieve what I wanted. \numexpr is the answer I was looking for. Thank you guys! –  jhad Mar 29 at 14:53

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.