0

I want to make a table that spreads to \textwidth. I tried several ways (none of them working properly Tabularx (xltabular) with only multicolumns not spreading properly, xltabular produces error in combination with multicolumn and \kill)

What I want to know is what I need to change (and learn) in order to make such tables.

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{fancyhdr}
    \usepackage{multicol}
    \usepackage{colortbl}
    \usepackage{booktabs}
    \usepackage{tabularx}
    \usepackage{longtable}
    \usepackage{layouts}
    \usepackage{xltabular}


%\pagestyle{fancy}

\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 


        \noindent
\noindent
        \begin{xltabular}{\textwidth}{|*{20}{c|}}
        \hline
        \multicolumn{20}{|c|}{ \textbf{ Header } } \\
        \multicolumn{2}{|l}{ \smf Label} & 
        \multicolumn{2}{l}{  } & 
        \multicolumn{6}{l}{ \smf 2020.07 } & 
        \multicolumn{3}{l}{ \smf Label} &  
        \multicolumn{7}{l|}{ \smf long variable }   \\ 
        \multicolumn{2}{|l}{ \smf Label} & 
        \multicolumn{2}{r}{ \smf 146 } & %  number cell
        \multicolumn{6}{l}{ \smf long variable test } &  % cell with person name
        \multicolumn{3}{l}{ \smf Label} &  
        \multicolumn{7}{l|}{ \smf 23 }   \\  %  number cell
        \multicolumn{2}{|l}{ \smf Label } & 
        \multicolumn{2}{r}{ \smf  12 } & %  number cell
        \multicolumn{6}{l}{ \smf variable variable variable } & % can be long 
        \multicolumn{3}{l}{ \smf Label} &  
        \multicolumn{7}{l|}{ \smf Long variable long variable }   \\  
        \multicolumn{2}{|l}{ \smf Label} & 
        \multicolumn{2}{r}{ \smf 24568545 } & %  number cell
        \multicolumn{6}{l}{ \smf long variable here } & 
        \multicolumn{3}{l}{ \smf Label } &  
        \multicolumn{7}{l|}{ \smf 1,000.12 } \\  \hline   
        \multicolumn{10}{|c}{ \cellcolor{greyCell} \smf \textbf{Label} } &
        \multicolumn{10}{|c|}{ \cellcolor{greyCell} \smf  \textbf{Label}   } \\ \hline
        \multicolumn{2}{|c|}{ \smf \textit{label}} & 
        \multicolumn{5}{c|}{ \smf \textit{label} } & 
        \multicolumn{2}{c|}{ \smf \textit{label} } & 
         \smf \textit{d\%} &  
        \multicolumn{2}{c|}{ \smf \textit{label}} & 
        \multicolumn{5}{c|}{ \smf \textit{label} } & 
        \multicolumn{2}{c|}{ \smf \textit{label} } & 
         \smf \textit{d\%}   \\  \hline 
            \multicolumn{2}{|r|}{ \smf  104 } & %  number cell
            \multicolumn{5}{l|}{ \smf long variable } & 
            \multicolumn{2}{r|}{ \smf 1,000.00 } & %  number cell
             \smf 14.4  &  

            \multicolumn{2}{r|}{ \smf 455 } & %  number cell
            \multicolumn{5}{l|}{ \smf long variable } & 
            \multicolumn{2}{r|}{ \smf 777.20 } & 
             \smf 0,4   \\  \hline   


        \multicolumn{7}{|l|}{ \smf long label } & % first row bottom
        \multicolumn{2}{r|}{ \smf   1,144.00 } & %  number cell
         &  
        \multicolumn{7}{l|}{ \smf  long label } & 
        \multicolumn{2}{r|}{ \smf  1,234.12 } & %  number cell
          \\  \hline 
        \multicolumn{7}{|l|}{ \smf long label } & % second row bottom
        \multicolumn{2}{r|}{ \smf  1,232.12 } & 
         &  
        \multicolumn{7}{l|}{ \smf  very long label } & 
        \multicolumn{2}{r|}{ \smf  1,999.12 } & 
          \\  \hline 
        \multicolumn{7}{|l|}{ \smf  long label} & % third row bottom
        \multicolumn{2}{r|}{ \smf  1,888.32 } & 
         &  
        \multicolumn{7}{l|}{ \smf  long label } & 
        \multicolumn{2}{r|}{ \smf  1,878.99 } & 
          \\  \hline 
        \multicolumn{7}{|l|}{ \smf  long label } & % last row bottom
        \multicolumn{2}{r|}{ \smf    1,878.99 } & 
         &  
        \multicolumn{7}{l|}{ \smf  long label } & 
        \multicolumn{2}{r|}{ \smf  1,878.99 } & 
          \\  \hline 
        
\end{xltabular}

\end{document}

How it should really look (I did this myself but its not working under environment pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017)) enter image description here

-----EDIT-----

I did a new MWE using only longtable and multicolumn. The idea is the same to use a n number of columns and take portions of them. This time each row is using p type columns with calculated width.

\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}

This time I get a lot of Overfull \hbox and Underfull \vbox . The result is almost identical with the first image. Is there anything I could do to optimise the code? Maybe I am missing something in the calculations ? A different approach is also welcomed (which produces the same result).

7
  • 2
    well the first thing you need to understand is that it doesn't make any sense at all to use xtabular without a X-column. Feb 16 at 9:13
  • You are right. I know this. David Carlisle said that I can not use column of type X and then multicolumn{2}{|l|} . I need to use multicolumn{2}{|X|} where things like multicolumn{2}{|X|}{ \centering TEST } actually don't work.The example I provided produces equal result with longtable also. Feb 16 at 9:47
  • No you don't need an X type. you only should decide how wide the columns with the numbers should be and then calculate the width of the rest. then you can use the p or w type. Feb 16 at 9:50
  • By calculation you mean for each row to do something like \textwidth - (sum of numeric columns width) = space left for variable columns ? And then use mutlicolumn{2}{|p{lets say 30mm}|}{variable} . Is this correct ? Also when I have multicolumn does it actually matters how many columns I put in there when I point out its a p column with its own width ? Ofc I need to fullfill the required columns of the table preamble for the row. Feb 16 at 10:09
  • 1
    You have posted multiple copies of this now and i really can not guess the intention of the multicolumn You show an image of a table with 8 columns so why do you declare 20 columns in the table (and still after multiple times do that with no X) then have spanning columns in every cell? Feb 16 at 11:14
3

Something more like this (which does not use tabularx/xltabular at all) and has far fewer \multicolumn

enter image description here




% article class does not have these options
%\documentclass[paper=a4,fontsize=18pt]{article}

\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}

% not used in this document
%    \usepackage{fancyhdr}
%    \usepackage{multicol}
%    \usepackage{booktabs}
%    \usepackage{layouts}


    \usepackage{colortbl}

% not needed as xltabular loads them
%    \usepackage{tabularx}
%    \usepackage{longtable}

    \usepackage{xltabular}


%\pagestyle{fancy}

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


% this is exactly the definition of the standard \small
%\newcommand{\smf}{% small font
%    \fontsize{9}{11}\selectfont
%}

\begin{document}

% this is the default so no need to specify
% \def\arraystretch{1}%  vertical padding

\setlength{\tabcolsep}{0.8mm} % for the horizontal padding

% this is the standard font with slightly larger baseline space
% it works but is an unusual way to set baselineskip
\fontsize{10}{13}\selectfont 


% double \noindent and should not be used befoe longtable/xltabular
%        \noindent
% \noindent

\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}
        
\end{xltabular}

\end{document}
3
  • 1
    generally stretching a table out to full width is not something I'd recommend: it just makes the table harder to read. Feb 16 at 12:59
  • Thank you for your response and sorry for he big delay (I was busy) ! What is the proper way to make the 4th and 8th columns less wider. This was the reason I was using X columns, because I could take chunks of the width. Mar 23 at 7:35
  • The example you provided is closer to what I need to do but the columns width is not correct. I will update the original question with a modified tabular. Mar 25 at 7:37

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.