0

I made a table using longtable and multicolumn packages. My idea is to spread the table to \textwidth , use n number of columns with the same width and use chunk of the \textwidth for each row. tabularx is not an option because it produces error when using \kill. More info if someone is interested enter link description here

Longtabu is also not an option because is no longer maintained.

What I am missing in the calculations for columns width ? Is there a better way to do this report (with the same layout output) ? Any help is welcomed :)

MWE

\documentclass[paper=a4,fontsize=18pt]{article}

   \usepackage{geometry}
    \geometry{
    % verbose,
    portrait,       % page orientation (landscape or portrait)
    a4paper,        % paper size
    tmargin=5mm,    % page top margin
    bmargin=5mm,    % page bottom margin
    lmargin=5mm,   % page left margin
    rmargin=5mm % page right maring
}

\usepackage[utf8]{inputenc}
\usepackage[defaultsans]{opensans} %font
\usepackage[T1, T2A]{fontenc}
\usepackage[english,bulgarian]{babel}
\usepackage{fancyhdr}
\usepackage{multicol}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{layouts}

\renewcommand{\familydefault}{\sfdefault}   

\definecolor{greyCell}{RGB}{235,235,235}
\newcommand{\smf}{ % small font
    \fontsize{9}{11}\selectfont
}
\begin{document}

\def\arraystretch{1}%  vertical padding
\setlength{\tabcolsep}{0.8mm} % for the horizontal padding
\fontsize{10}{13}\selectfont 


          
           \vspace{-4mm}
            \noindent
                
      \LTleft=0pt 
      \LTright=0pt
    \begin{longtable}{@{\extracolsep{\fill}}*{20}{p{0.5\textwidth}} @{}}
    \hline
    \multicolumn{20}{|p{\dimexpr 1\textwidth-2\tabcolsep}|}{ \centering \textbf{ Платежна бележка } }\\ % header
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep}}{ \smf Период } & % ---------------- row 1
    \multicolumn{2}{p{\dimexpr 0.1\textwidth-2\tabcolsep}}{  } & 
    \multicolumn{6}{p{\dimexpr 0.3\textwidth-2\tabcolsep}}{ \smf 2020.07 } & 
    \multicolumn{3}{p{\dimexpr 0.15\textwidth-2\tabcolsep}}{ \smf Календар } & 
    \multicolumn{7}{p{\dimexpr 0.35\textwidth-2\tabcolsep}|}{ \smf 5 Пет дневна работна седмица } \\ 
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep}}{ \smf За лице } & % ---------------- row 2
    \multicolumn{2}{p{\dimexpr 0.1\textwidth-2\tabcolsep}}{ \smf\raggedleft\arraybackslash 146 } & 
    \multicolumn{6}{p{\dimexpr 0.3\textwidth-2\tabcolsep}}{ \smf Атанас Антонов Атанасов } & 
    \multicolumn{3}{p{\dimexpr 0.15\textwidth-2\tabcolsep}}{ \smf Работни дни } & 
    \multicolumn{7}{p{\dimexpr 0.35\textwidth-2\tabcolsep}|}{ \smf 23 } \\ 
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep}}{ \smf Заето в } & % ---------------- row 3
    \multicolumn{2}{p{\dimexpr 0.1\textwidth-2\tabcolsep}}{ \smf\raggedleft\arraybackslash 12 } & 
    \multicolumn{6}{p{\dimexpr 0.3\textwidth-2\tabcolsep}}{ \smf Поддръжка } & 
    \multicolumn{3}{p{\dimexpr 0.15\textwidth-2\tabcolsep}}{ \smf Трудов стаж } & 
    \multicolumn{7}{p{\dimexpr 0.35\textwidth-2\tabcolsep}|}{ \smf 24г. 1м. 4д. към 01.07.2020 } \\ 
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep}}{ \smf Длъжност } & % ---------------- row 4
    \multicolumn{2}{p{\dimexpr 0.1\textwidth-2\tabcolsep}}{ \smf\raggedleft\arraybackslash 24236003 } & 
    \multicolumn{6}{p{\dimexpr 0.3\textwidth-2\tabcolsep}}{ \smf Експерт, организация на труда } & 
    \multicolumn{3}{p{\dimexpr 0.15\textwidth-2\tabcolsep}}{ \smf Осн. заплата } & 
    \multicolumn{7}{p{\dimexpr 0.35\textwidth-2\tabcolsep}|}{ \smf 1,000.00 } \\ \hline
    \multicolumn{10}{|p{\dimexpr 0.5\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \centering \cellcolor{greyCell} \textbf{ Начисления } } &
    \multicolumn{10}{p{\dimexpr 0.5\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \centering \cellcolor{greyCell} \textbf{ Удръжки } } \\ \hline
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \centering \textit{шифър} } &
    \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \centering \textit{Наименование} } &
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \centering \textit{Сума} } &
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \centering \textit{д\%} } &
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \centering \textit{шифър} } &
    \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \centering \textit{Наименование} } &
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \centering \textit{Сума} } &
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \centering \textit{д\%} } \\ \hline 
                                  \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft  } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright От 01.07.2020 г. ОМВ } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 1,000.00 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 0 } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 455 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright Разпл. сметка Уникредит } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 777.20 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 0 } \\ \hline
                                           \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 104 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright Заплата по щат } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 1,000.00 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 23 } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 485 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright Разпл. сметка FiBank } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 75.00 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 0 } \\ \hline
                                           \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 111 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright Допл.проф.опит и стаж } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 144.00 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 14.4 } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 490 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright Банков заем } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 0.00 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 0 } \\ \hline
                                       \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 500 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright Данък общ доход } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 98.63 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 10 } \\ \hline
                                       \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 501 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright ЛОВ ф.\textquotedbl{}Безработица\textquotedbl{} } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 4.58 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 0.4 } \\ \hline
                                       \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 533 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright ЛОВ ф.\textquotedbl{}Пенсии\textquotedbl{} } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 75.28 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 6.58 } \\ \hline
                                       \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 536 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright ЛОВ ф.\textquotedbl{}ОЗМ\textquotedbl{} } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 16.02 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 1.4 } \\ \hline
                                       \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 537 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright ЛОВ ф.\textquotedbl{}ДЗПО УПФ\textquotedbl{} } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 25.17 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 2.2 } \\ \hline
                                       \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 540 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright Здравно осигуряване } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 36.61 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 3.2 } \\ \hline
                                       \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{  } &
                        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 560 } &
        \multicolumn{5}{|p{\dimexpr 0.25\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedright Членски внос Подкрепа } &
        \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 35.50 } &
        \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{ \smf \raggedleft 0 } \\ \hline
                    
    \multicolumn{7}{|p{\dimexpr 0.35\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf  \raggedright Всичко начисления } & % row 1 bottom
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 1,144.00 } & 
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &  
    \multicolumn{7}{|p{\dimexpr 0.35\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf  Всичко удръжки } & 
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft  1,144.00 } & 
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{   }  \\  \hline 
    \multicolumn{7}{|p{\dimexpr 0.35\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf  \raggedright Осигурителен доход } & % row 2 bottom
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 1,144.00 } & 
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &  
    \multicolumn{7}{|p{\dimexpr 0.35\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf  Всичко за осигуреното лице } & 
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft  157.66 } & 
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{   }  \\  \hline 
    \multicolumn{7}{|p{\dimexpr 0.35\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf  \raggedright Облагаем доход } & % row 3 bottom
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 1,144.00 } & 
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &  
    \multicolumn{7}{|p{\dimexpr 0.35\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf  Всичко за осигурителя } & 
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft  224.44 } & 
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{   }  \\  \hline 
    \multicolumn{7}{|p{\dimexpr 0.35\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf  \raggedright Данъчна основа } & % row 4 bottom
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft 986.34 } & 
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}}{ } &  
    \multicolumn{7}{|p{\dimexpr 0.35\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf  Сума за получаване } & 
    \multicolumn{2}{|p{\dimexpr 0.1\textwidth-2\tabcolsep-1\arrayrulewidth}}{ \smf \raggedleft  887.71 } & 
    \multicolumn{1}{|p{\dimexpr 0.05\textwidth-2\tabcolsep-1\arrayrulewidth}|}{   }  \\  \hline 
    \end{longtable}
\end{document}

Result enter image description here

As David Carlisle said, this approach is overcomplicated. The table actually contains 8 columns (not 20) so I post this MWE provided by him.

\documentclass{article}

\usepackage{geometry}
\geometry{
    % verbose,
    portrait,       % page orientation (landscape or portrait)
    a4paper,        % paper size
    tmargin=5mm,    % page top margin
    bmargin=5mm,    % page bottom margin
    lmargin=5mm,   % page left margin
    rmargin=5mm % page right maring
}

% only needed with old latex installations 
  \usepackage[utf8]{inputenc}
    \usepackage[defaultsans]{opensans} %font
    \usepackage[T1, T2A]{fontenc}
    \usepackage{colortbl}
    \usepackage{longtable}

\definecolor{greyCell}{RGB}{235,235,235}

\begin{document}

\setlength{\tabcolsep}{0.8mm} % for the horizontal padding
\fontsize{10}{13}\selectfont 


\setlength\extrarowheight{2pt}% keep hline away from text
\small
\LTleft=0pt
\LTright=0pt
        \begin{longtable}{@{\extracolsep{\fill}}|*{8}{l|}@{}}
        \hline
        \multicolumn{8}{|c|}{\textbf{Header}}\\
        \multicolumn{4}{|l}{%
\begin{tabular}{lrl}
Label& &  2020.07\\
Label& 146 &long variable test\\
Label & 12 & variable variable variable\\
label  & 24568545 & long variable here
\end{tabular}}
&
        \multicolumn{4}{l|}{%
\begin{tabular}{ll}
Label &  long variable \\ 
Label & 23\\
Label& Long variable long variable  \\  
Label & 1,000.12
\end{tabular}}
\\
\rowcolor{greyCell}
\multicolumn{4}{@{}|>{\columncolor{greyCell}[.8mm][2cm]}c@{}}{Label}&\multicolumn{4}{@{}c|@{}}{Label}\\
label&label&label&d\%&label&label&label&d\%\\\hline
104& long variable& 1,000.00&14.4&455&long variable&777.20&0,4\\\hline
111 & long label & 144& 14.4 & 490 & long label & 0.00 & 0\\\hline
&&&&500 &long label &98.63&10\\\hline
\multicolumn{2}{|l|}{long label}&1,44.00 &&\multicolumn{2}{|l|}{long label}&1,44.00& \\\hline
\multicolumn{2}{|l|}{long label}&1,44.00 &&\multicolumn{2}{|l|}{long label}&157.66 &\\\hline
\end{longtable}
\end{document}

Why column 5 text is pushed so far away ? Why the inner tables (under header) don't spread for the full width of the multicolumn ?

11
  • for example in \begin{longtable}{@{\extracolsep{\fill}}*{20}{p{0.5\textwidth}} @{}} you have removed the 2 outer tabcolsep but still have 38\tabcolsep inter column space plus 1\textwidth width of the text content of p columns so you are specifying it to be 38\tabcolsep wider than \textwidth Mar 26 at 7:55
  • We had a conversation about it here tex.stackexchange.com/questions/583583/… The post got delayed so I made a new one. Yes you are right that there are 8 columns but with your example in the post the layout is different. Mar 26 at 8:21
  • Its a mistake from copy pasting. For the small font. What is the proper way to do this table without multicolumn ? Multicolumn gives me an option to place vertical lines whenever I need them. Also with 8 columns I can not make a table which has different number of columns on each row - give that a certain column must be "small" like the д% column. Mar 26 at 8:28
  • 2
    It is hard to suggest how to lay out the table as the image shown clearly only has 8 columns, I spent sone effort on previpus answers coding it up in that way, perhaps I missed some feature, so another question is fine but as this just ignores all previous answers with this complicated array of multicolumns for every entry with no indication of where the additional columns need to be used. I can not guess what answer you want. Mar 26 at 8:35
  • 1
    p{3cm} means a parbox with text lines 3cm wide separted on each side by \tabcolsep space (plus the width of vertical rule on the right if you have rules) so a table that is *{20}{p{3cm}} is 20 times (3cm+2\tabcolsep) wide, similarly if you span two such columns you need to span the text and the inner column gap so you need \multicolumn{2}{p{6cm+2\tabcolsep} the actual calculations are not hard but if you change from 20 columns to 8 all the multicolumn use changes. So you need to decide that first. Mar 26 at 11:10
1

Hm, I would design your table as follows:

  • consider most of comments below your question i.e. your second MWE
  • for table I would use xltabular
  • in columns with numbers I would use S column type
  • form the most used multicolumn cells I would define new commands \mcl and \mcll
  • in editor remove group separators for thousand (their inserting left to siunitx)
\documentclass{article}
\usepackage{geometry}
\geometry{
    a4paper,    % paper size
    margin=20mm % all margins are equal
        }
%\usepackage[utf8]{inputenc}   % only needed with old latex installations
%\usepackage[defaultsans]{opensans} %font
\usepackage[T1, T2A]{fontenc}
\usepackage{longtable, xltabular}
\NewExpandableDocumentCommand\mcll{m}{\multicolumn{2}{|l|}{#1}}
\NewExpandableDocumentCommand\mcl{ m}{\multicolumn{2}{ l|}{#1}}

\usepackage[table]{xcolor}
\definecolor{greyCell}{RGB}{235,235,235}
\usepackage{siunitx}

\begin{document}

\begingroup
\sisetup{group-minimum-digits=4,
         group-separator={,}} 
\setlength\extrarowheight{2pt}% keep hline away from text
\small
    \begin{xltabular}{\linewidth}{ *{2}{|S[table-format=3]
                                        |X
                                        |S[table-format=4.2]
                                        |S[table-format=2.1]}|
                                }
    \hline
\multicolumn{8}{|c|}{\textbf{Header}}   \\
\multicolumn{4}{|l}{
    \begin{tabular}{@{}lrl}
    Label   &           & 2020.07                   \\
    Label   & 146       & long variable test        \\
    Label   & 12        & variable variable variable\\
    Label   & 24568545  & long variable here
    \end{tabular}}
    &   \multicolumn{4}{l|}{
        \begin{tabular}{@{} ll}
        Label   &  long variable                \\
        Label   & 23                            \\
        Label   & Long variable long variable   \\
        Label   & 1,000.12
        \end{tabular}}          \\
    \hline
    \rowcolor{greyCell}
\multicolumn{4}{|c|}{\textbf{Label}}   & \multicolumn{4}{c|}{\textbf{Label}}\\  \hline
{label} & label         & {label}   & {d\%} & {label} & label         & {label} & {d\%} \\  \hline
104     & long variable & 1 000.00  & 14.4  & 455     & long variable & 777.20  & 0,4   \\  \hline
111     & long label    &   144.00  & 14.4  & 490     & long label    & 0.00    & 0     \\  \hline
        &               &           &       & 500     & long label    & 98.63   & 10    \\  \hline
\mcll{long label}        &   144.00  &       & \mcl{long label}  
                                                      & 1 144.00      &   \\  \hline
\mcll{long label}        &   144.00  &       & \mcl{long label} 
                                                      &   157.66      &   \\  \hline
    \end{xltabular}
\endgroup
\end{document}

enter image description here

11
  • Thank you for your help. The layout is exactly what it needs to be. I get some error under This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017) (preloaded format=pdflatex) The error is ! Missing number, treated as zero. Originally I try to stick with longtable only. I did the exact same report with xltabular -> tex.stackexchange.com/questions/583484/… Under This is pdfTeX, Version 3.14159265-2.6-1.40.21 (MiKTeX 20.12) it works like a charm. Mar 26 at 13:22
  • 1
    @LubomirBorisov, in proposed solution, as you can see, this is not needed anymore.
    – Zarko
    Mar 26 at 13:38
  • 1
    @LubomirBorisov, you can instead S column type use any other type. This is up to you, however, other types (except d defined in dcolumn package doesn't offer alignment at decimal points. In xltabular as well in tabularx had to be at least one column of X type or its derivatives.
    – Zarko
    Mar 26 at 14:18
  • 1
    @LubomirBorisov I added \kill to longtable as it was easy and I thought it might be useful in some cases but in 30 years of use I don't think I have ever seen any real use for it. I only suggested it in your earlier example as a work-around for the excessive use of \multicolumn which removed all of the original table specification, and that was a way to get back a row that did not have \multicolumn on every entry. With a more reasonable table specification, as here, \kill is no use at all. Mar 26 at 15:30
  • 1
    @LubomirBorisov, use of m{<width>} type column (defined in array package) or at tabularx, you need redefined X column type as \renewcommand\tabularxcolumn[1]{m{#1}}. I suggest you to make yourself more familiar with writing of tables by reading some introductory text about it. For example en.wikibooks.org/wiki/LaTeX/Tables
    – Zarko
    Apr 7 at 6:20

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.