[Tex/LaTex] merging cells with tabularx

multicolumntabularx

I'am writing my thesis. I want to create a table.

I'm using the tabularx environment
My code:

\begin{table}[htbp]
  \centering
  \caption{Add caption}
    \begin{tabularx}{\textwidth}{*{3}{>{\centering\arraybackslash}X}}
    \toprule
    \textbf{Aditivo} & {\textbf{Quantidade (g)} \\
    \cmidrule{2-3}
     & Carga Mineral (\%) \\
     & 20    & 30 \\
     \midrule
    Fibra & 1,266 & 252 \\
    GCC & 0,316 & 52 \\
    Amido & 0,016 & 25 \\
    ASA & 0,002 & 252 \\
    Percol & 0,00032 & 25 \\
    \bottomrule
    \end{tabularx}
  \label{tab_formacao_folhas}%
\end{table}%ckquote

enter image description here

But I want merging the text "Quantidade (g)" and "Carga Mineral" in second and third column. With tabularx , I can't use multicolumn.

Best Answer

Please always post complete documents.

The comment about not being able to use \multicolumn with tabularx is wrong as shown in the first table below, but tabularx is really for adjusting the widths of paragraphs in p columns. Using it for numeric columns doesn't really produce a good result, it just spaces the table out, making it harder to read, and if you use \centering the numeric data isn't aligned. I'd use a setting more like the second table where the data aligns on the ,

enter image description here

\documentclass{article}

\usepackage{dcolumn,tabularx,booktabs}

\begin{document}

\begin{table}[htbp]
%  \centering does nothing if table is full width
  \caption{Add caption}
    \begin{tabularx}{\textwidth}{*{3}{>{\centering\arraybackslash}X}}
    \toprule
    \textbf{Aditivo} & \multicolumn{2}{c}{\textbf{Quantidade (g)}}\\
    \cmidrule{2-3}
     & \multicolumn{2}{c}{Carga Mineral (\%)} \\
     & 20    & 30 \\
     \midrule
    Fibra & 1,266 & 252 \\
    GCC & 0,316 & 52 \\
    Amido & 0,016 & 25 \\
    ASA & 0,002 & 252 \\
    Percol & 0,00032 & 25 \\
    \bottomrule
    \end{tabularx}
  \label{tab_formacao_folhas}%
\end{table}
\begin{table}[htbp]
  \centering
  \caption{Add caption}
    \begin{tabular}{cD{,}{,}{3.7}D{,}{,}{5.1}}
    \toprule
    \textbf{Aditivo} & \multicolumn{2}{c}{\textbf{Quantidade (g)}}\\
    \cmidrule{2-3}
     & \multicolumn{2}{c}{Carga Mineral (\%)} \\
     & \multicolumn{1}{c}{20}    &  \multicolumn{1}{c}{30} \\
     \midrule
    Fibra & 1,266 & 252 \\
    GCC & 0,316 & 52 \\
    Amido & 0,016 & 25 \\
    ASA & 0,002 & 252 \\
    Percol & 0,00032 & 25 \\
    \bottomrule
    \end{tabular}
  \label{tab_formacao_folhas2}%
\end{table}

\end{document}
Related Question