5

I want the first row to stay normal and then every row after that should be split into 2 columns. I've done it on Microsoft word to show exactly what I want. I tried used multicolumn but couldn't get it to work correctly.

Table

\begin{center}
\begin{tabular}{ |c|c|c|c|c|c|c|c| } 
    \hline
    B    & B 1 & B 2 & B3 & B4 & B5 & B6 & B 7 \\
    \hline
    1   & 12 & 12 & 12 & 12 &12  &12& 12\\ 
    \hline
    2   & 12& 12& 12& 12&12& 12& 12\\ 
    \hline
    3   & & & & & & & \\ 
    \hline
    4   & & & & & & &\\ 
    \hline
    5   & & & & & & &\\ 
    \hline
    6   & & & & & & & \\ 
    \hline
    7   & & & & & & &\\ 
    \hline
\end{tabular}
\end{center}
Zarko
  • 296,517
Syed
  • 53
  • welcome to tex.se! cells you can merge, but not divide. for your cell with x you can obtain with \multicolumn{2}{c}{x}. similar for others. – Zarko Mar 18 '18 at 17:10
  • Thanks Zarko, apologies but its my first time creating a table on latex so still a little confused. I tried doing that but it came up with an error. I've updated the question with what I have so far. – Syed Mar 18 '18 at 17:29
  • 1
    You might take a look at the wikibook for an overview about tables in LaTeX. Also consider using the booktabs package and following the guidelines laid out in the documentation of booktabs for prettier tables. – Skillmon Mar 18 '18 at 17:55
  • 1
    to see your table in context, we need to see complete but small document beginning with\documentclass{...} and ending with \end{document}. about tables beside what suggest @Skillmon, see https://wiert.me/2014/04/03/andre-vatter-google-wie-tabellen-eigentlich-aussehen-sollten-%EF%BB%BF/ – Zarko Mar 18 '18 at 17:58

2 Answers2

6
  • your table code fragment doesn't reproduce showed table. for this it ha defined one column less as needed (you have defined only 8 columns, but showed table has 9)
  • for help to people who like to help you, y\documentclass{...} and ending with `\end{document}, which show your problem. it should have in preamble loaded all to your problem relevant packages (if any needed)
  • as i mentioned in comment, cells cant be split, but can be merged. so solution in general in your case is use command \multicolumn{2}{c}{...} which merge tvo columns and cell's content align in its middle.
  • without knowing content of your table your code snippet can be converted into mwe as follows:

\documentclass{article}

\begin{document}
    \begin{center}
\begin{tabular}{| *{9}{c|} }
    \hline
B    & \multicolumn{2}{c|}{x}
            & \multicolumn{2}{c|}{z}
                    & \multicolumn{2}{c|}{w}
                            & \multicolumn{2}{c|}{t}                \\
    \hline
1   &   12  &   12  &   12  &   12  &   12  &   12  &   12  &   12  \\
    \hline
2   &   12  &   12  &   12  &   12  &   12  &   12  &   12  &   12  \\
    \hline
3   &       &       &       &       &       &       &       &       \\
    \hline
\end{tabular}
    \end{center}
\end{document}

enter image description here

  • however, your table can be written on many diferent way. one of them is usebooktabs packages for horizontal rules where needed, omit vertical riles and for example use modified X columns from package `tabularx:

\documentclass{article}
\usepackage{booktabs, tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
    \begin{center}
\begin{tabularx}{\linewidth}{ c *{8}{C} }
    \toprule
B    & \multicolumn{2}{c}{x}
            & \multicolumn{2}{c}{z}
                    & \multicolumn{2}{c}{w}
                            & \multicolumn{2}{c}{t}                \\
    \midrule
1   &   12  &   12  &   12  &   12  &   12  &   12  &   12  &   12  \\
2   &   12  &   12  &   12  &   12  &   12  &   12  &   12  &   12  \\
3   &       &       &       &       &       &       &       &       \\
    \bottomrule
\end{tabularx}
    \end{center}
\end{document}

enter image description here

Zarko
  • 296,517
  • Ahh yes I see what I was doing wrong. Thanks and for the future tips, I'll be sure too use them next time when asking a question. – Syed Mar 18 '18 at 18:35
  • can we have second layer for labels on top of 12 12 as well? – alper Sep 26 '22 at 16:15
  • @alper, sorry, I do not understand your comment. Do you like to have one more row above tale body? Just insert it. But, I suggest you, ask new question where describe what you like change in above table (BTW, today I would use tabularray package in their writing). – Zarko Sep 26 '22 at 16:35
  • I was lookling something like here: https://tex.stackexchange.com/a/174328/127048 ; like having exta label for each 12 below B x z w t – alper Sep 26 '22 at 17:08
  • 1
    @alper, you can do this on the same way as is done in answer in your link. If you need additional/specific support, at this, than please ask new question that we can help you. – Zarko Sep 26 '22 at 18:04
0
\documentclass[12pt,a4paper]{article}
\usepackage[left=.45in,right=.45in,top=.6in,bottom=.6in,headheight=14.5pt]{geometry}
\usepackage{array,multirow,textcomp}
\usepackage[T1]{fontenc}

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
%\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
%\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

 %    \setlength\extrarowheight{32pt}
\begin{document}

\centering
\begin{tabular}{|c|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}}
    \hline
 x & \multicolumn{2}{c|}{y}& \multicolumn{2}{c|}{z} & \multicolumn{2}{c|}{w}& \multicolumn{2}{c|}{t}\\ \hline
 1 &&&&&&&&\\ \hline
 2 &&&&&&&&\\ \hline
 3  &&&&&&&&\\ \hline
 4  &&&&&&&&\\ \hline
 5  &&&&&&&&\\ \hline
 6  &&&&&&&&\\   \hline     
 7  &&&&&&&&\\ \hline


\end{tabular}

 \end{document} 

enter image description here

Biki Teron
  • 3,275
  • 1
    Why \cline{1-9} instead of a \hline? And why are you using C column specifiers? It doesn't seem necessary here. – Skillmon Mar 18 '18 at 17:51
  • 1
    In the example, you define a C column twice. You as well load the array package twice and included some packages that are not needed (multirow,textcomp) to produce the table. – leandriis Mar 18 '18 at 17:52