I'm using longtable where I want vertical bars separating cells, although not for the table headers. I have 8 columns (but show only 3 here as example) and want to re-use the same lengths on the main longtable as well as all multicolumn specifiers.
In this example, I only use one heading in one table, but with multiple headings (first page, last page, the other pages), I'll have to specify the same column lengths over and over again.
So what I have:
\documentclass[a4paper,10pt]{article}
\usepackage{longtable}
\usepackage{array}
\usepackage{ifthen}
\usepackage{xifthen}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}b{#1}}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}b{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}b{#1}}
\providecommand*\tablehead[2][]{\scriptsize\textbf{#2\ifthenelse{\isempty{#1}}{}{\textsuperscript#1}}}
\begin{document}
\begin{longtable}[l]{|@{}L{0.2\textwidth}@{}|@{}C{0.5\textwidth}@{}|@{}R{0.3\textwidth}@{}|}
\multicolumn{1}{@{}L{0.2\textwidth}@{}}{\tablehead[1]{First head}} &
\multicolumn{1}{@{}C{0.5\textwidth}}{\tablehead[2]{Second head}} &
\multicolumn{1}{@{}R{0.3\textwidth}}{\tablehead[3]{Last}} \\
\hline
\endfirsthead
\hline
\endlastfoot
1 & 2 & 3 \\
4 & 5 & 6 \\
\end{longtable}
\end{document}
I was thinking of defining the column widths once only in my command \mcw which defines the columns widths for column 1, 2 and 3. So, if I want to try lengths I'm comfortable with, I can change in one place only. My suggestion (which doesn't compile) is:
\documentclass[a4paper,10pt]{article}
\usepackage{longtable}
\usepackage{array}
\usepackage{ifthen}
\usepackage{xifthen}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}b{#1}}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}b{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}b{#1}}
\providecommand*\tablehead[2][]{\scriptsize\textbf{#2\ifthenelse{\isempty{#1}}{}{\textsuperscript#1}}}
\newcommand*{\mcw}[1]{% My column width
\ifthenelse{#1=1}{0.2\textwidth}{%
\ifthenelse{#1=2}{0.5\textwidth}{%
\ifthenelse{#1=3}{0.3\textwidth}{%
}}}}
\begin{document}
\begin{longtable}[l]{|@{}L{\mcw{1}}@{}|@{}C{\mcw{2}}@{}|@{}R{\mcw{3}}@{}|}
\multicolumn{1}{@{}L{\mcw{1}}@{}}{\tablehead[1]{First head}} &
\multicolumn{1}{@{}C{\mcw{2}}}{\tablehead[2]{Second head}} &
\multicolumn{1}{@{}R{\mcw{3}}}{\tablehead[3]{Last}} \\
\hline
\endfirsthead
\hline
\endlastfoot
1 & 2 & 3 \\
4 & 5 & 6 \\
\end{longtable}
\end{document}
What am I doing wrong? Can't a newcommand (mcw in this case) "return" a length and pass it as an argument into another command (L, C and R in this case)?