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}
  • Sorry, I meant Welcome to TeX.SX! In the \multicolumn{5}{c}{multicolum2}, use \multicolumn{5}{c@{}}{multicolum2} as in the table header. – Phelype Oleinik Aug 29 '18 at 12:04
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)

  • 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 '18 at 13:23

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.