2

I am trying to create a table. However, I am facing problems with rules and formatting. Please have a look at the latex code.

\documentclass{article}

\usepackage{tabularx,booktabs}
\newcommand{\gmc}\[2\]{\multicolumn{#1}{@{}#2@{}}}
\newcolumntype{s}{>{\hsize=.5\hsize}X}

\begin{document}

    \begin{tabularx}{\textwidth}{|X|s|s|s|}

        \hline
        Mean cure time in cells & \gmc{3}{c}{Cell frequency} \\\toprule\toprule
                        & Male     & Female  & Total     \\ \midrule
        Drugtakers         & 4 (80)       & 7 (20) & 4.6         \\ \midrule
        Non Drugtakers     & 3 (20)       & 6  (80)  & 5.4     \\ \midrule
    \end{tabularx}

\end{document}

enter image description here

  • I just +1 mico's answer as it explains tabularx use but there is never really any point to using tabularx if all the columns are X or p as you know in advance what the column widths need to be so you may as well just use tabular and p columns. tabularx really only makes sense if some of the columns are l r or c – David Carlisle Nov 13 '14 at 19:56
3

I think there are two issues in your code. First, if you want to have three columns of type s, defined via \newcolumntype{s}{>{\hsize=.5\hsize}X}, you should set the type of the first column not to X but to Z, where Z should be defined via \newcolumntype{Z}{>{\hsize=2.5\hsize}X}. Note that the sum of the \hsize fractions has to equal the number of columns of (possibly modified) X. (Incidentally, it might make sense to add a \centering instruction to the definition of the s column type.)

Second, since you're using the booktabs package and its macros to obtain well-spaced horizontal rules, you shouldn't use any vertical rules in the table.

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs}
\newcolumntype{s}{>{\hsize= .5\hsize}X}
\newcolumntype{Z}{>{\hsize=2.5\hsize}X}  
\begin{document}
     \noindent
     \begin{tabularx}{\textwidth}{Zsss} % Check: 2.5 + 3 * 0.5 = 4
     \toprule
     Mean cure time in cells & \multicolumn{3}{c}{Cell frequency}\\
     \midrule
                        & Male     & Female   & Total   \\
        Drugtakers      & 4 (80)   & 7 (20)   & 4.6     \\
        Non Drugtakers  & 3 (20)   & 6  (80)  & 5.4  \\
    \bottomrule
    \end{tabularx}
\end{document}

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.