2

I am trying to implement a code using tabularx and multicolumn along with head, but I keep on getting an error. The code is as follows:

\begindocumnet{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{ragged2e}
\newcolumntype{L}{ >{\RaggedRight}X }
\usepackage{siunitx}
\usepackage{makecell}
\renewcommand\theadfont{\normalfont\bfseries}
\setlength\parindent{0pt}
\newcolumntype{Y}{ >{\Centering}X}
\usepackage[justification=centering]{caption}

\noindent 

\newcolumntype{Y}{ >{\Centering}X} 
\begin{tabularx}{\textwidth}{@{} lYYY @{}}  

\toprule 


\thead[lc]{Category}   & \multicolumn{2}{c}{\thead{Forecasting Horizon}}& 

& \multicolumn{2}{c}{\thead{1 Period}} & {\multicolumn{2}{c}{1 Period}} & 

\multicolumn{2}{c}{\thead{3 Period}}  \\


&  \thead{TSMOM (1,1)} & \thead{TSMOM (3,3)} 

& \thead{TSMOM (1,1)} & \thead{TSMOM (3,3)} & \thead{TSMOM (1,1)} 

& \thead{TSMOM (3,3)} \\

\midrule  

Test-Statistic & 0 & 1.00 & 1.00 \\ \addlinespace

P-value & 0 & 0.59 & 1.00 \\ \addlinespace  

\bottomrule  

\end{tabularx}

To provide an idea, I basically want to make a chart with the following content

enter image description here

except I would like to have the layout in the following format :

enter image description here

However, I get the following error such as: "extra alignment tab has been change to \cr" could someone please help advise me where I am making a mistake with the code ? Thank you

1

You have mismatch in MWE (one multicolum is probably accidentally repeated). Corrected (and slightly improved) code is:

\documentclass{article}
    \usepackage{booktabs,makecell,multirow,tabularx}
\renewcommand\theadfont{\normalfont\bfseries}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
\newcolumntype{C}{>{\Centering\arraybackslash}X}

\newcommand\mc[1]{\multicolumn{2}{c}{#1}}
    \usepackage[justification=centering]{caption}
    \usepackage{ragged2e}
    \usepackage{siunitx}

    \usepackage[margin=20mm]{geometry}
\setlength\parindent{0pt}

    \begin{document}
\begin{tabularx}{\textwidth}{@{} l *{6}{C} @{}}
    \toprule
\thead{Forecasting Horizon}
    &   \mc{\thead{1 Period}}
        &   \mc{\thead{2 Period}}
            &   \mc{\thead{3 Period}}               \\
    \cmidrule(rl){2-3}\cmidrule(rl){4-5}\cmidrule(rl){6-7}
\thead{strategies}
    &  \thead{TSMOM\\ (1,1)}
        &   \thead{TSMOM\\ (3,3)}
            &   \thead{TSMOM\\ (1,1)}
                &   \thead{TSMOM\\ (3,3)}
                    &   \thead{TSMOM\\ (1,1)}
                        &   \thead{TSMOM\\ (3,3)}   \\
    \midrule
Test-Statistic
    & \mc{0} & \mc{1.00} & \mc{1.00}                \\ 
    %\addlinespace
P-value & \mc{0} & \mc{0.59} & \mc{1.00}            \\
    \bottomrule
\end{tabularx}
    \end{document}

enter image description here

  • Thank you very much ! Would you mind explaining whether it is necessary for me to use the geometry package for "\usepackage[margin=20mm]{geometry}" ? I seem to be getting an error saying that there is an "option clash for package geometry" (I am using lyx but am not sure if this is specific to lyx). Any how, could I get rid of this package because when I wipe out the preamble, I seem to be getting an error that says "undefined control sequence". Sorry for nothing you again but I tried looking via other sources without success and was hoping that you could give me some advice – PutsandCalls Feb 25 '16 at 7:33
  • @TheStudent, you didn't provide any information about your document, so I use geometry for determining page layout. My solution works also without it. Unfortunately, i newer use Lyx, so I haven't any clue, how/where the page layout is defined in it. – Zarko Feb 25 '16 at 7:38
  • Ah, I had found out where I made an error and yes, you are correct about the code not working without the geometry package. Thank you very much for your help again :) – PutsandCalls Feb 25 '16 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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