1

I would like to write text into multiple columns using the nicematrix and booktabs packages.

However, there is the unwanted spacing, circled in the picture.

Is there any way to get rid of it so that the three columns are vertically aligned?

Solutions using other ways to do this are welcome, but they must:

  • Be able to show the equation with adequate spacing above and below, as the booktabs package does automatically
  • Have the vertical lines
\documentclass[12pt]{article}

\usepackage{graphicx}

\usepackage{float}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}

\begin{table}[H]
    \centering
    \begin{NiceTabular}{|p{3.9em}| p{14em} |p{10em} | p{10em}|}
        \toprule
        \textbf{Test} & \textbf{Test} & \textbf{Test} & \textbf{Test} \\
        \midrule
        H&H&H&$\left(\frac{1}{1+\frac{\alpha}{\beta}}\right)^{10}$\\
        \midrule
        Test
        &
        Test Test Test TestTestTest TestTest Test TestTest TestTest TestTestTest TestTestTestTestTest TestTestTest
        &
        \Block{1-2}{
            \begin{minipage}{22em}
                Test Test Test TestTestTest TestTest Test TestTest TestTest TestTestTest TestTestTestTestTest TestTestTest
        \end{minipage}}
    &
    \\
    \bottomrule
    \end{NiceTabular}
\end{table}

\end{document}

enter image description here

1
  • Welcome To TeX.SE!
    – Zarko
    Mar 26 at 22:14

3 Answers 3

2

enter image description here

nicematrix package provides the option T for blocks to align columns. \Block does not create space if it has two or more rows/columns. You still need to determine the space for your text manually by specifying columns widths and number of \tabcolsep in minipage environment

\documentclass{article}

\usepackage{calc}
\newlength{\widthWidest}

\usepackage{nicematrix}
\usepackage{booktabs}

\NewDocumentCommand{\testText}{}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobor- tis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante.}

\NiceMatrixOptions{rules/color=[gray]{0.75}, hvlines}

\begin{document}
    
    \setlength{\widthWidest}{\widthof{\textbf{Header 1}}}%
    \begin{NiceTabular}{p{\widthWidest} p{10em} p{10em} p{10em}}
        
        \toprule
        
        \RowStyle[bold]{} Header 1 & Header 2 & Header 3 & Header 4
        \\
        \midrule
        
        Test
        &
        \Block[T, l]{1-1}{
    
            \smash{\rlap{\rule{\dimexpr 30em + 4\tabcolsep \relax}{0.1pt}}}%
            \testText
            
        }
        &
        \Block[T]{1-2}{
            
            \begin{minipage}{\dimexpr 20em + 2\tabcolsep \relax}
                
                \raggedright
                
                \testText
                
            \end{minipage}
            
        }
        &
        \\
        
        \bottomrule
        
    \end{NiceTabular}
    
\end{document}
2
  • Thanks, is there a way to make the text fully justified (i.e. not raggedright)? Also, is there a way to vertically center the texts?
    – cmb2200
    Mar 28 at 0:07
  • @cmb2200. Load ragged2e package and use \justifying inside \Block. When you remove the key T, you get vertical centering of the contents Mar 28 at 12:13
1

That's a fairly simple table. You wouldn't need to load nicematrix in this case. \multicolumn is enough to sort it out.

Regarding the rules, if you want to use those defined in booktabs, still have vertical lines, and avoid small gaps, one way is to remove spaces around the rules and stretch the table by increasing a factor of arraystretch.

Also the equation has a larger height and would stick to top and bottom lines by default. This is usually sorted out by adding a strut, e.g. \xmathstrut from mathtools package to stretch this particular cell individually.

Because of the size of your table, the code uses sidewaystable which renders its content vertically (see rotating documentation for more info).

Here's the table:

enter image description here

and the code:

\documentclass[12pt]{article}
\usepackage{array}       % for \newcolumntype and \extrarowheight
\usepackage{mathtools}   % for \xmathstrut
\usepackage{rotating}    % for sidewaystable
\usepackage{booktabs}

\newcolumntype{P}[1]{p{#1}}   % >{\raggedright\arraybackslash}
\newcommand\header[1]{\textbf{#1}}

\usepackage[nopar]{kantlipsum}


\begin{document}
\begin{sidewaystable}
    %%% Stretch table
    \renewcommand\arraystretch{1.75}
    \setlength\extrarowheight{-1.5pt}
    %%% Remove spaces around rules
    \setlength\aboverulesep{0pt}
    \setlength\belowrulesep{0pt}
    %%%
    \centering
    \begin{tabular}{|
            P{3.9em}|
            P{14em}|
            *2{P{10em}|}
        }
        \toprule
        \header{Test}
        & \header{Test} & \header{Test} & \header{Test} \\
        \midrule
        H & H & H & $\xmathstrut{0.75}
                     {0.75} \left(
                                \frac{1}{1+\frac{\alpha}{\beta}}
                            \right)^{10}$ \\
        \midrule
        Test
        & \kant[2][2]
        & \multicolumn{2}{P{\dimexpr20em+2\tabcolsep}|}{\kant[1][1]} \\
        \bottomrule
    \end{tabular}
\end{sidewaystable}
\end{document}
1

I'm not very familiar with nicematrix, but your table is simple to write by use of the tabularray package:

\documentclass[12pt]{article}
\usepackage{lipsum}

\usepackage{tabularray}
\UseTblrLibrary{amsmath, booktabs}

\begin{document}

\begin{table}[ht]
    \centering
    \begin{tblr}{colspec = {l X[1.2, j] *{2}{X[0.8, j]}},
                 row{1}  = {font=\bfseries},
                 rowsep  = 3pt,
                 hspan   = minimal
                 }
    \toprule
Test    &   Test    &   Test    &   Test    \\
    \midrule
H       &   H       &   H       &   $\left(\dfrac{1}{1+\frac{\alpha}{\beta}}\right)^{10}$\\
    \midrule
Test    & \lipsum[66]
                    &   \SetCell[c=2]{j}    \lipsum[11]
                                &           \\
    \bottomrule
    \end{tblr}
\end{table}

\end{document}

After only one compilation you will get the final result:

enter image description here

In the case, that you like that table has vertical lines, just add in table preamble vlines option:

    \begin{tblr}{colspec = {l X[1.2, j] *{2}{X[0.8, j]}},
                 row{1}  = {font=\bfseries},
                 rowsep  = 3pt,
                 hspan   = minimal,
                 vlines
                 }

and table will be:

enter image description here

You must log in to answer this question.

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