1

I have a table with 20 equals columns. What I need to do is to use portions of those 20 columns with multicolumn grouping. The table needs to be spread across the whole textwidth.

%--LATEX-COMPILER:lualatex:LATEX-COMPILER       % Specify which compiler to be used
%LATEX-COMPILE-COUNT:1:LATEX-COMPILE-COUNT      % How many iterations to make the compiler
\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{booktabs}
\usepackage{tabularx}
\usepackage{longtable}
\usepackage{layouts}
\usepackage{xltabular}
\usepackage{layouts}


\newcommand{\smf}{ % small font
    \fontsize{9}{11}\selectfont
}


 
\begin{document}


     \noindent
     \begin{xltabular}{\textwidth}{|*{20}{>{\centering\arraybackslash\hsize=0.88\hsize}X|}}
\hline
 \multicolumn{20}{|c|}{ \centering \textbf{ test } } \\  
       \multicolumn{2}{|l}{ \smf test} & 
        \multicolumn{2}{l}{  } & 
         \multicolumn{6}{l}{ \smf test } & 
           \multicolumn{3}{l}{ \smf test} &  
             \multicolumn{7}{l|}{ \smf test }   \\ \hline 
             1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1 \\ \hline
\end{xltabular}



     \noindent
\begin{xltabular}{\textwidth}{|*{20}{>{\centering\arraybackslash\hsize=0.88\hsize}X|}}
\hline
 \multicolumn{20}{|c|}{ \centering \textbf{ test } } \\  
       \multicolumn{2}{|l}{ \smf test} & 
        \multicolumn{2}{l}{  } & 
         \multicolumn{6}{l}{ \smf test } & 
           \multicolumn{3}{l}{ \smf test} &  
             \multicolumn{7}{l|}{ \smf test }   \\ \hline 
           
\end{xltabular}

\end{document}

Table 1 produces what I need. Table 2 does not spread at all. I used to use tabu which had a command \tabuphantomline which solved the problem but since its no longer maintained I decided to swtich to some other table. Thank you in advance !

EDIT

For some reason killing a row gives me the following error ! Missing } inserted.

This happens only when xltabular is used in combination with multicolumn, column of type X and a the command \kill. This happens in environment pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017), works fine under pdfTeX, Version 3.14159265-2.6-1.40.21 (MiKTeX 20.12)

This works fine

\begin{xltabular}{\textwidth}{|X|X|X|} % vertical rules for illustrative purposes only
   % \multicolumn{1}{|c|}{very long content } & setting the widths & of each column \\ \kill
  actual & content & is short \\
  killed & row & discarded 
\end{xltabular}

This produces the error

\begin{xltabular}{\textwidth}{|X|X|X|} % vertical rules for illustrative purposes only
    \multicolumn{1}{|c|}{very long content } & setting the widths & of each column \\ \kill
  actual & content & is short \\
  killed & row & discarded 
\end{xltabular}
2
  • 1
    Will there be other contents in your table or just the single row consisting of multiple multicolumn commands? Couldn't you just calculate the required widths yourself?
    – leandriis
    Feb 8, 2021 at 10:09
  • There will be other rows but all of them will be used with multicolumn again. Why I need to calculate something that is supposed to be calculated in the first place ? The table at its core has 20 equal columns. If I use multicolumn{10} I should receive 50% of the table width or I am missing something. And how I can calculate it ? Feb 8, 2021 at 10:26

1 Answer 1

2

Due to the behaviour of the underlying \halign primitive tabularx has essentially no knowledge that the X specifications were there or even that the original table had 20 columns. TeX optimises away all column boundaries that are spanned in every row.

However or longtable you could use a \kill row to add a row and remove it again so \halign sees all the columns.

enter image description here

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


% no \newcommand{\smf}{ % small font
\newcommand{\smf}{% small font
    \fontsize{9}{11}\selectfont
}


 
\begin{document}


% no     \noindent
     \begin{xltabular}{\textwidth}{|*{20}{>{\centering\arraybackslash\hsize=0.88\hsize}X|}}
\hline
 \multicolumn{20}{|c|}{ \centering \textbf{ test } } \\  
       \multicolumn{2}{|l}{ \smf test} & 
        \multicolumn{2}{l}{  } & 
         \multicolumn{6}{l}{ \smf test } & 
           \multicolumn{3}{l}{ \smf test} &  
             \multicolumn{7}{l|}{ \smf test }   \\ \hline 
             1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1 \\ \hline
\end{xltabular}



     % no\noindent
\begin{xltabular}{\textwidth}{|*{20}{>{\centering\arraybackslash\hsize=0.88\hsize}X|}}
\hline
 \multicolumn{20}{|c|}{ \centering \textbf{ test } } \\  
       \multicolumn{2}{|l}{ \smf test} & 
        \multicolumn{2}{l}{  } & 
         \multicolumn{6}{l}{ \smf test } & 
           \multicolumn{3}{l}{ \smf test} &  
             \multicolumn{7}{l|}{ \smf test }   \\ \hline 
           
\end{xltabular}


     \begin{xltabular}{\textwidth}{|*{20}{>{\centering\arraybackslash\hsize=0.88\hsize}X|}}
\hline
 \multicolumn{20}{|c|}{ \centering \textbf{ test } } \\  
       \multicolumn{2}{|l}{ \smf test} & 
        \multicolumn{2}{l}{  } & 
         \multicolumn{6}{l}{ \smf test } & 
           \multicolumn{3}{l}{ \smf test} &  
             \multicolumn{7}{l|}{ \smf test }   \\ \hline 
             1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1&1 \kill
\end{xltabular}


\end{document}
3
  • Thank you ! I was looking exactly for this. Feb 8, 2021 at 12:10
  • Can you help me a bit, I edited my original post. Feb 15, 2021 at 10:38
  • 1
    @LubomirBorisov sorry the site doesn't really work well if you change the question weeks after an answer posted. Better to ask a new question. But xltabular isn't my package and if it works in texlive 2020 but not texlive 2017, just updating the text system may be easiest, but post a new question with a complete example. Feb 15, 2021 at 11:12

You must log in to answer this question.

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