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.

I wanted to create a table of three columns . the first has several rows and each of the second and third is divided into two columns. I do not know what could be wrong.

\document class{article}  
\newcommand{\splitcell}\[1\]{%
\begin{tabular}{@{}c@{}}\strut#1\strut\end{tabular}%
}

\begin{document}

\begin{table}\[htp\]
\centering

\caption{A caption}

\begin{tabular}{
    S\[table-format=2.0\] % Column 1
    S\[table-format=1.0\] % Column 2
    S\[table-format=1.6\] % Column 3
    S\[table-format=1.4\] % Column 4
    S\[table-format=1.3\] % Column 5
}

\toprule
{Metal type/ Metal thickness $t$(\si{\nano\meter})} & \multicolumn{2}{c}{Akbari} & \multicolumn{2}{c}{Naema} \\
\cmidrule{2-3} \cmidrule{4-5}
 & & {$Re(n_{eff})$}  {\splitcell{\\$\alpha$\\(\si{dB/\mu m})}} &   {$Re(n_{eff})$}  {\splitcell{\\$\alpha$\\(\si{dB/\mu m})}}\\
\midrule
Al/100nm & 3.619 & 1.041 & 3.607517& 1.04979 \\
\midrule
Au/100nm & 3.772 & 1.228 &3.762532 & 1.29734 \\
\midrule
Al/20nm & 3.72 & 2 & 3.65266 & 2.00962\\
\midrule
Au/20nm & 4.2 & 5 & 4.001033 & 3.25059\\
\midrule
Al/10nm & 5.12 & 23.5 & 3.865893 & 5.12678\\
\midrule
Au/10nm &   NA & 33.5 & 4.809568 & 9.15694 \\

\bottomrule

\end{tabular}
\end{table}
\end{document}

The table is here

share|improve this question
    
Well... first of all you need to understand more about tables and eventually learn more about LaTeX. Finally you'll need to write well your questions. You need to explain what's wrong, the bug or error when you compile or instead describe clearly what's your handicap or what you do not understand or don't know to get the expected result. I saw your picture and I don't find the complication of this table even if you do understand how to use multicolumn. –  Aradnix Aug 15 at 5:41
1  
Can you please repost the latex. Currently it does not compile. Some of the issues are \document class (no space) and [ and ] both appear as \[ and \], respectively. You are also missing some required packages and there are probably other problems. Please make sure that it compiles without errors. –  Andrew Aug 15 at 5:41
    
Please read the link above in my first comment and analyse the answer of the question I answered yesterday. There is the solution to your question, you'll need to understand the multicolumn environment syntax. –  Aradnix Aug 15 at 5:57
2  
@theEditors: Please do not edit the MWE of a question. This could hide the actual problems of the OP. Adressing the errors in comment (like above) or fixing the whole thing in an answer would be a better way. Thanks. –  LaRiFaRi Aug 15 at 6:04
1  
@LaRiFaRi - I've rolled back the two most recent rounds of revision, so that people can evaluate the original code more easily. –  Mico Aug 15 at 8:58

2 Answers 2

up vote 2 down vote accepted

Another solution, using xcolor, colortbl, hhline and makecell:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[TS1,T1]{fontenc}
\usepackage{lmodern}
\usepackage[table, x11names, svgnames]{xcolor}
\usepackage{colortbl}
\colorlet{myblue}{SteelBlue3}
\usepackage{amsmath}
\DeclareMathOperator\re{\textsf{Re}}
\usepackage{array, hhline, caption}
\newcommand\myhline{\hhline{>{\arrayrulecolor{myblue}}->{\arrayrulecolor{Cornsilk1}}|----}}
\usepackage{siunitx}

\usepackage{makecell}
\renewcommand\cellalign{lc}
\renewcommand\theadfont{\cellcolor{myblue}\color{Cornsilk1}\bfseries\boldmath}
\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\def\nm{\si{\,\nm}}

\begin{document}
\rowcolors*{3}{SlateGray3!60}{Gainsboro!50}%[\cline{2-5}]
\begin{table}[!htp]
\centering
\sisetup{table-number-alignment=center,detect-all}
\caption{A caption}
\sffamily\arrayrulecolor{Cornsilk1}\setlength\arrayrulewidth{1.5pt}
\begin{tabular}%
   { >{\cellcolor{myblue}\color{Cornsilk1}\bfseries\boldmath}Cl |% Column 1
    S[table-format=1.2] |% Column 2
    S[table-format=2.3] |% Column 3
    S[table-format=1.6] |% Column 4
    S[table-format=1.5] % Column 5
}
\rowcolor{myblue}& \multicolumn{2}{>{\bfseries\color{Cornsilk1}}c}{Akbari} & \multicolumn{2}{>{\bfseries\color{Cornsilk1}}c}{Naema} \\
\myhline%
\rowcolor{myblue}\makecell{Metal type/Metal \\ thickness\enspace$t$(\si{\nano\meter})}& {\thead{$\re(n_{\mathbf{eff}})$}} & {\thead{$\alpha$\\(\si{dB/\micro\meter})}} & {\thead{$\re(n_{\mathrm{eff}})$}} & {\thead{$\alpha$\\(\si{dB/\micro\meter})}}\\
\myhline%
Al/100\nm & 3.619 & 1.041 & 3.607517& 1.04979 \\
Au/100\nm & 3.772 & 1.228 &3.762532 & 1.29734 \\
Al/20\nm & 3.72 & 2 & 3.65266 & 2.00962\\
Au/20\nm & 4.2 & 5 & 4.001033 & 3.25059\\
Al/10\nm & 5.12 & 23.5 & 3.865893 & 5.12678\\
Au/10\nm & {NA} & 33.5 & 4.809568 & 9.15694
\end{tabular}
\end{table}

\end{document} 

enter image description here

share|improve this answer
    
thanks Bernard, I really liked your way of coloring the table the way I want but unfortunately, I had problem separating the part of code that must be in the preamble from the part that must be with the table. –  Naema Aug 15 at 19:00
1  
I'm afraid I don't see what problem you have: the preamble part and the body part are already separated. Perhaps there are incompatibilities betwwen the rest of your preamble ang this code? Btw, note that your definition of splitcell is replaced with loading the makecell package, that has, among other features, the same functionality. –  Bernard Aug 15 at 19:09

Your code has quite a few problems, any one of which makes the MWE uncompilable. As far as I can tell, none of them are related to the fact you mention in the title of the posting, which is that the table groups two pairs of columns using \multicolumn.

  • The first instruction should be \documentclass, not \document class

  • The packages siunitx and booktabs need to be loaded

  • When square brackets are called for, type [ and ], not \[ and \]. E.g., don't write \begin{table}\[htp\]; instead, write \begin{table}[htp]. Same goes for for the specification of the S columns.

  • In the \si expressions, use \micro\meter, not \mu m

  • Don't forget to insert & alignment dividers between some of the header cells. (The tabular environment has five columns, not three.)

Not outright errors, but important nonetheless:

  • The first column can use type c; no need for anything more fancy

  • When setting the value of table-format, strive to match the number of digits and decimals to the actual properties of the data inputs

  • I suggest you add the [t] placement specifier to the tabular environment contained in the splitcell macro

  • The very first \\ line break instruction in both calls to \splitcell seems unnecessary.

  • When you have an occasional non-numeric input, such as NA, in an S column type, be sure to center it by encasing it in curly braces. E.g., write {NA}, not just NA.

enter image description here

\documentclass{article}  
\usepackage{siunitx,booktabs}
\newcommand{\splitcell}[1]{%
  \begin{tabular}[t]{@{}c@{}}\strut#1\strut\end{tabular}}

\begin{document}

\begin{table}[htp]
\centering

\caption{A table}

\medskip
\begin{tabular}{
    c                   % Column 1
    S[table-format=1.3] % Column 2
    S[table-format=2.3] % Column 3
    S[table-format=1.6] % Column 4
    S[table-format=1.5] % Column 5
}

\toprule
Metal type\slash Metal thickness $t$ (\si{\nano\meter})
& \multicolumn{2}{c}{Akbari} 
& \multicolumn{2}{c}{Naema} \\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
 &  
{$Re(n_{\textit{eff}})$}  & 
{\splitcell{$\alpha$\\(\si{dB/\micro\meter})}} &   
{$Re(n_{\textit{eff}})$} & 
{\splitcell{$\alpha$\\(\si{dB/\micro\meter})}}\\
\midrule
Al/100nm & 3.619 & 1.041 & 3.607517& 1.04979 \\
\midrule
Au/100nm & 3.772 & 1.228 &3.762532 & 1.29734 \\
\midrule
Al/20nm & 3.72 & 2 & 3.65266 & 2.00962\\
\midrule
Au/20nm & 4.2 & 5 & 4.001033 & 3.25059\\
\midrule
Al/10nm & 5.12 & 23.5 & 3.865893 & 5.12678\\
\midrule
Au/10nm &   {NA} & 33.5 & 4.809568 & 9.15694 \\
\bottomrule

\end{tabular}
\end{table}
\end{document}
share|improve this answer
    
thanks a lot Mico, and thanks to everyone commented so far. –  Naema Aug 15 at 18:58

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.