1

How can I solve the misalignment in the following table?

enter image description here

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\usepackage{color, colortbl}
\definecolor{gray_header}{gray}{0.6}
\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{@{}>{\raggedright}X| L L L L| L L L L L@{}}
\rowcolor{gray_header}
tab1 & \multicolumn{4}{c}{multicolumn1} & \multicolumn{5}{c}{multicolum2} \\
\rowcolor{gray_header}
 &  & & & & \multicolumn{3}{c}{mulmticolum3} & a  & b \\ \hline
1 & 2& 3& 4& 5& 6& 7& 8& 9& 10\\\hline
\end{tabularx}
\end{table}
\end{document}
1
  • Sorry, I meant Welcome to TeX.SX! In the \multicolumn{5}{c}{multicolum2}, use \multicolumn{5}{c@{}}{multicolum2} as in the table header. Aug 29, 2018 at 12:04

3 Answers 3

1

i suspect that your problem is spilling a color of the colored rows out of table. this is caused by @{} at begin and end of column specification. if you will remove them, the table become fine, i.e. colored rows will not spill out of table anymore.

for limiting colored rows to table width you should add \columncolor{white}.... to the first and last table's columns as follows:

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\usepackage[table]{xcolor}
\definecolor{gray_header}{gray}{0.6}

\begin{document}
    \begin{table}
\begin{tabularx}{\textwidth}{@{}
                            >{\columncolor{white}[0pt][\tabcolsep]}L|
                            *{4}{L} | *{4}{L}
                             >{\columncolor{white}[\tabcolsep][0pt]}L
                            @{}}
\rowcolor{gray_header}
tab1 & \multicolumn{4}{c@{}}{multicolumn1} & \multicolumn{5}{c}{multicolum2} \\
\rowcolor{gray_header}
 &  & & & & \multicolumn{3}{c}{mulmticolum3} & a  & b \\
 \hline
1 & 2& 3& 4& 5& 6& 7& 8& 9& 10\\
\hline
\end{tabularx}
    \end{table}
\end{document}

which gives

enter image description here

(red lines indicate text borders)

in comparison to your solution now whole table is inside text area (width), i.e. and colored area in the first two rows doesn't spill out of table (and text area).

0

Thanks to Phelype Oleinik, I was able to solve the issue by adapting the code to:

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\usepackage{color, colortbl}
\definecolor{gray_header}{gray}{0.6}
\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{@{}>{\raggedright}X| L L L L| L L L L L@{}}
\rowcolor{gray_header}
tab1 & \multicolumn{4}{c@{}}{multicolumn1} & \multicolumn{5}{c@{}}{multicolum2} \\
\rowcolor{gray_header}
 &  & & & & \multicolumn{3}{c}{mulmticolum3} & a  & b \\
 \hline
1 & 2& 3& 4& 5& 6& 7& 8& 9& 10\\
\hline
\end{tabularx}
\end{table}
\end{document}

enter image description here

(red lines indicate text border)

1
  • i took a liberty and ad picture of table generated with you code in answer. let me say: i doubt that you like to have a table, where colored rows are wide than rest of tables (see added image).
    – Zarko
    Sep 28, 2018 at 13:23
0

With {NiceTabularX} of nicematrix (≥ 6.0 of 2021-08-10).

\documentclass{article}
\usepackage{xcolor}
\definecolor{gray_header}{gray}{0.6}

\usepackage{nicematrix}
\newcolumntype{L}{X[l]}

\begin{document}
\begin{table}
\begin{NiceTabularX}{\textwidth}{@{}L|LLLL|LLLLL@{}}[colortbl-like]
\rowcolor{gray_header}
tab1 & \Block{1-4}{multicolumn1} &&&& \Block{1-5}{multicolum2} \\
\rowcolor{gray_header}
& & & & & \Block{1-3}{multicolum3} &&& a  & b \\
\hline
1 & 2& 3& 4& 5& 6& 7& 8& 9& 10\\
\hline
\end{NiceTabularX}
\end{table}
\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .