3

I'm trying to create a table using tabularx environment and I need to use multiple columns to reproduce the original Word template table. On the first picture is how the table should look and below is my first try to reproduce it supposed layout

\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[czech]{babel}
\usepackage[margin=2cm]{geometry}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
\sf
\noindent
\bgroup
\def\arraystretch{1.5}
\begin{center}
    \begin{tabularx}{\textwidth}{||C|C|C|C||}
        \hline \hline
        \multicolumn{4}{||c||}{{\large Ústav technologie stavebních dílců, FAST, VUT v Brně}}\\ [2mm]
        
        \multicolumn{4}{||c||}{{\huge \bfseries BJA006 Technologie betonu - laboratoře}}\\[2mm]
        
        \hline
        \large {\bfseries Číslo protokolu:} &\multicolumn{2}{|c|}{\large {\bfseries Název protokolu:}}  & \large {\bfseries Studijní skupina:} \\[2mm]
        
        \LARGE 1&\multicolumn{2}{|c|}{\LARGE Technologické zkoušky kameniva}  & \LARGE B3M1 \\[2mm]
        
        \hline
        
        \multicolumn{2}{||c|}{{\large \bfseries Vypracovala:}}  & \large {\bfseries Datum:} & \large {\bfseries Kontrolovala:}\\[2mm]
        
        \multicolumn{2}{||c|}{\LARGE XXX BBB} & \LARGE 10.10.2023 & \LARGE XXX BBB \\[2mm]
         
        \hline \hline
    \end{tabularx}
\end{center}
\egroup

\end{document}  

This code has resulted in this: First try

As you can see, vertical lines are not aligned as they should be.
So I added empty line at the end of the table and got this:

Second try

So I tried using multicolumns also for singlecolumns and I got the original table. But using multicolumns is too complicated. So any suggestions where am I making a mistake? I don't belive there is such a bug in tex. The final code is here:

\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[czech]{babel}
\usepackage[margin=2cm]{geometry}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
\sf
\noindent
\bgroup
\def\arraystretch{1.5}
\begin{center}
    \begin{tabularx}{\textwidth}{CCCC}
        \hline \hline
        \multicolumn{4}{||c||}{{\large Ústav technologie stavebních dílců, FAST, VUT v Brně}}\\ [2mm]
        
        \multicolumn{4}{||c||}{{\huge \bfseries BJA006 Technologie betonu - laboratoře}}\\[2mm]
        
        \hline
        \multicolumn{1}{||c}{\large {\bfseries Číslo protokolu:}}   &\multicolumn{2}{|c|}{\large {\bfseries Název protokolu:}}  & \multicolumn{1}{c||}{\large {\bfseries Studijní skupina:}} \\[2mm]
        
        \multicolumn{1}{||c}{\LARGE 1}&\multicolumn{2}{|c|}{\LARGE Technologické zkoušky kameniva}  & \multicolumn{1}{c||}{\LARGE B3M1} \\[2mm]
        
        \hline
        
        \multicolumn{2}{||c|}{{\large \bfseries Vypracovala:}}  &\multicolumn{1}{c|}{\large {\bfseries Datum:}} & \multicolumn{1}{c||}{\large {\bfseries Kontrolovala:}}\\[2mm]
        
        \multicolumn{2}{||c|}{\LARGE XXX BBB} & \multicolumn{1}{c|}{\LARGE 10.10.2023} & \multicolumn{1}{c||}{\LARGE XXX BBB} \\[2mm]
         
        \hline \hline
        &&&\\
    \end{tabularx}
\end{center}
\egroup

\end{document}
2
  • in texlive 2023 I get this from the version with the blank line. If you have an older tex system try \omit&\omit&\omit&\omit\\ as the last line Commented Oct 9, 2023 at 19:49
  • Or you can use TikZ because you get to control every length.
    – Symbol 1
    Commented Oct 9, 2023 at 20:32

3 Answers 3

6

The issue is that while you define the table to have four columns with automatic, i.e., residually determined, widths, you currently don't provide enough information for LaTeX to be able to determine some of the combined column widths -- those with column type c. Specifically, the calculation of the width of the two bottom-left cell cannot be performed properly. To fix this, you should provide an explicit width calculation yourself, e.g., by replacing

\multicolumn{2}{||c|}{{\large \bfseries Vypracovala:}}

with

\multicolumn{2}{||>{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth\relax}C|}{%
   \large\bfseries Vypracovala:}

or, more succinctly, by replacing c in the second argument of the applicable \multicolumn instruction with

>{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth\relax}C

What's going on? Note that while the default width of a C-type column is given by the parameter \hsize, we're resetting the width to a bit more than 2\hsize -- specifically, 2\hsize+2\tabcolsep+1\arrayrulewidth.


Addendum: If you absolutely must draw double-vertical and double-horizontal lines around the table, I suggest you do so with the help of the hhline package and its \hhline macro. And, since you're using the babel package with the option czech, I further suggest you run the instruction \shorthandoff{-} -- just inside the center environment -- in order to enable \hhline with single horizontal rules.

enter image description here

\documentclass[a4paper, 11pt]{article}
\usepackage[T1]{fontenc} % <-- new
%\usepackage[utf8]{inputenc} % that's the default nowadays
\usepackage[czech]{babel}
\usepackage[margin=2cm]{geometry}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage{hhline} % cf. https://www.ctan.org/pkg/hhline

\begin{document}

\begin{center}

\shorthandoff{-} % cf. https://tex.stackexchange.com/a/105935/5001

\def\arraystretch{1.5}
\sffamily

\begin{tabularx}{\textwidth}{|| C|C|C|C ||}
%\hline \hline
\hhline{|t:====:t|} % <-- new

\multicolumn{4}{||c||}{{\large Ústav technologie stavebních dílců, FAST, VUT v Brně}} \\ [2mm]
\multicolumn{4}{||c||}{{\huge\bfseries BJA006 Technologie betonu - laboratoře}} \\[2mm]
        
%\hline
\hhline{||----||} % <-- new

  \large\bfseries Číslo protokolu 
& \multicolumn{2}{|c|}{\large\bfseries Název protokolu}  & \large\bfseries Studijní skupina \\[2mm]
        
  \LARGE 1
& \multicolumn{2}{|c|}{\LARGE Technologické zkoušky kameniva}  
& \LARGE B3M1 \\[2mm]
        
%\hline
\hhline{||----||} % <-- new
        
\multicolumn{2}{||>{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth\relax}C|}{\large\bfseries Vypracovala}  
& \large\bfseries Datum 
& \large\bfseries Kontrolovala \\[2mm]
        
\multicolumn{2}{||c|}{\LARGE XXX BBB} 
& \LARGE 10.10.2023 
& \LARGE XXX BBB \\[2mm]
         
%\hline\hline
\hhline{|b:====:b|}  % <-- new

\end{tabularx}

\end{center}

\end{document}
5
  • Thank you, it's more complicated than I thought. I thought that the width of columns was calculated from all lines and that it should be the same across all the lines. I don't like tabularx much, is there a way to do it in tabular environment or begin{table}?
    – Jan K
    Commented Oct 11, 2023 at 11:41
  • @JanK - The issue at hand is not specific to tabularx. By design, a \multicolumn{}{}{} directive overrides the specs of the underlying column(s). E.g., \begin{tabular}{p{3cm}} \multicolumn{1}{l}{abc} \end{tabular} does not produce a single-column table with a usable width of 3cm; instead, it produces a single-column table that's as wide as the string "abc". The deeper issue, then, is that your table has a lot of \multicolumn directives which, taken together, prevent LaTeX from applying the information about the widths of the underlying columns to the width of the bottom-left cells.
    – Mico
    Commented Oct 11, 2023 at 12:31
  • Thanks, I now understand where the problem is. Is there a way to automatically include >{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth\relax} part of the multicolumn code? So I could just write \multicolumn{2}{||>C|} with any number of merged columns and don't need to think about resizing?
    – Jan K
    Commented Oct 11, 2023 at 19:02
  • @JanK - I'm not sure what you mean by "automatic" inclusion; AFAICT, there's nothing "automatic" about the row width calculation, viz., \hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth. I suppose that after \newcolumntype{C}{>{\centering\arraybackslash}X}, you could execute \newcolumntype{D}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth\relax}C}. This would let you generate the cell as \multicolumn{2}{||D|}{\large\bfseries Vypracovala} in a nice, uncluttered fashion. Is that maybe what you're after?
    – Mico
    Commented Oct 11, 2023 at 19:14
  • @JanK - If at all possible, I'd like to recommend that you redesign the table so that in at least one of the rows, there's just one cell in the second column, without any \multicolumn connections. This change would give LaTeX just enough information about the widths of all 4 underlying columns and thereby make it unnecessary to perform explicit calculations of the combined width of the first two columns.
    – Mico
    Commented Oct 11, 2023 at 19:18
4

Here is a solution with {NiceTabular} of nicematrix.

\documentclass[a4paper, 11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[czech]{babel}
\usepackage[margin=2cm]{geometry}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{center}
\shorthandoff{-} % cf. https://tex.stackexchange.com/a/105935/5001
\renewcommand{\arraystretch}{1.5}
\sffamily

\begin{NiceTabular}{*{4}{X[c]}}[vlines]
\Hline
\Block{1-4}{\large Ústav technologie stavebních dílců, FAST, VUT v Brně} &&& \\[2mm]
\Block{1-4}{\huge\bfseries BJA006 Technologie betonu - laboratoře} \\[2mm]
\Hline
\RowStyle{\large\bfseries} Číslo protokolu & \Block{1-2}{Název protokolu} && Studijní skupina \\[2mm]
\RowStyle{\LARGE} 1 & \Block{1-2}{Technologické zkoušky kameniva} && B3M1 \\[2mm]
\Hline
\RowStyle{\large\bfseries} \Block{1-2}{Vypracovala} && Datum & Kontrolovala \\[2mm]
\RowStyle{\LARGE} \Block{1-2}{XXX BBB} && 10.10.2023 & XXX BBB \\[2mm]
\Hline
\CodeAfter
  \tikz \draw  ([xshift=-2pt,yshift=2pt]1-|1) rectangle ([xshift=2pt,yshift=-2pt]last-|last) ; 
\end{NiceTabular}

\end{center}

\end{document}

Output of the above code

Since I have not used \multicolumn (which erases the specs of the columns as explained by Mico), the columns have directly the expected width (roughly \linewidth/4).

In order to place the contents of merged cells, I have used the command \Block provided by nicematrix. That command uses TikZ to write its argument in the PDF after the construction of the main array. For the computation of the correct position, the command \Block relies upon the TikZ nodes that nicematrix has previously put in the array. Since the array has been constructed with the correct widths, the TikZ nodes are at the right place and nicematrix is able to put the contents of the blocks (and the rules) at the expected position...

2
  • +1. Could you maybe post an addendum to explain to the OP, in words, what the NiceTabular machinery does that makes it unnecessary to calculate the width of the first two columns explicitly? I.e., what information does NiceTabular draw on that doesn't appear to be availabe to the "standard" tabular/array setup?
    – Mico
    Commented Oct 11, 2023 at 19:23
  • 1
    @Mico. Good suggestion. Done... Commented Oct 12, 2023 at 8:57
2

Solution by use of the tabularray package (for completeness 😉):

\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage[czech]{babel}
\usepackage{tabularray}

\begin{document}
\begin{center}
    \begin{tblr}{hline{1,Z} = 2pt, hline{2,4,5} = solid,
                 vline{1,Z} = 2pt, vline{2,3,4} = solid,
                 colspec = {*{4}{X[c, font=\large\sffamily]}},
                 cell{1}{1}   = {c=4}{},
                 cell{2,3}{2} = {c=2}{},
                 cell{4,5}{1} = {c=2}{},
                 row{even[2]} = {font=\large\sffamily\bfseries},
                 row{odd[2]}  = {font=\sffamily\LARGE},
                 rowsep = 7pt, 
                 hspan  = minimal
                 }
{Ústav technologie stavebních dílců, FAST, VUT v Brně\\  
\huge \bfseries 
BJA006 Technologie betonu - laboratoře}
    &   &   &                       \\
Číslo protokolu: 
    &   Název protokolu:
        &   &   Studijní skupina:   \\  
1   &   Technologické zkoušky kameniva
        &   &   B3M1                \\
Vypracovala:
    &   &   Datum:
            &   Kontrolovala:       \\
XXX BBB
    &   &   10.10.2023 
            &   XXX BBB             \\
    \end{tblr}
\end{center}

\end{document}

enter image description here

As you see, I took a liberty and wee bit redesign your table ...

You must log in to answer this question.

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