3

I'm trying to create a table with NiceTabular, because I had some coloring problems with multicolumn/multirow.

The table looks already good except, that no linebreaks are done in \Block cells. In normal cells the line break works as expected. With multirow I had the same problem, but there I could set again the width as multirow parameter and then linebreak was done, but with NiceTabular I'm not able to do:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{nicematrix} % nicematrix.sty must be installed

\begin{document}
\begin{NiceTabular}[colortbl-like]{|p{1cm}|p{2cm}|Wc{3cm}|p{4cm}|p{4cm}|} 
    \hline 
    \Block[fill=[HTML]{FFFF00}]{2-1}{Table Title}    &
    \Block[fill=[HTML]{FFFF00}]{2-1}{Column 1}   &  Column   &   \Block[fill=[HTML]{FFFF00}]    {2-2}{Column 3} &  \\ 
    \cline{3-3}       
    &     &     2    &       &   \\ 
    \hline 
    Row 1 & Value 1 & Value 2    & Value 3 & Value 4 \\ 
    \hline 
    \Block[fill=[HTML]{780373}]{2-1}{Row 2}  &  
    Long text outside of Block &     
    \Block[fill=[HTML]{548235}]{2-1}{This cell contains a long value}    &   \Block[fill=[HTML]{0070C0}]{2-2}{Value 8} &  \\ 
    \cline{2-2}       
    & Value 6 &  & & \\ 
    \hline 
\end{NiceTabular}   
\end{document}

The table should be create automatically from excel at the end, so adding a linebreak just in the text does not solve the problem. How do I get a linebreak in a NiceTabular \Block?

enter image description here

2
  • 2
    \Block[fill=[HTML]{548235}]{2-1}{\parbox{3cm}{This cell contains a long value}}?
    – user241086
    May 2 at 6:31
  • 1
    See also F. Pantigny's comment: "If you want automatic break lines in the \Block, you have to put a \parbox in the block (with the right width: maybe this will automated in a further version of nicematrix).
    – leandriis
    May 2 at 8:08
3

With a \parbox.

\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{nicematrix}
\usepackage{rotating}

\begin{document}

\begin{sidewaystable}
\begin{NiceTabular}[hvlines,colortbl-like]{p{1cm}p{2cm}Wc{3cm}p{4cm}p{4cm}} 
    \Block[fill=[HTML]{FFFF00}]{2-1}{Table Title}    &
    \Block[fill=[HTML]{FFFF00}]{2-1}{Column 1}   &  Column   &   \Block[fill=[HTML]{FFFF00}]    {2-2}{Column 3} &  \\ 
    &     &     2    &       &   \\ 
    Row 1 & Value 1 & Value 2    & Value 3 & Value 4 \\ 
    \Block[fill=[HTML]{780373}]{2-1}{Row 2}  &  
    Long text outside of Block &     
    \Block[fill=[HTML]{548235}]{2-1}{\parbox{3cm}{This cell contains a long value}}    &   \Block[fill=[HTML]{0070C0}]{2-2}{Value 8} &  \\ 
    & Value 6 &  & & \\ 
\end{NiceTabular}   
\end{sidewaystable}

\end{document}

Remark: In your case, the key hvlines draws all the required rules.

As usual with nicematrix, you need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

1
1

With the latest version of nicematrix (v. 6.0 of 2021-08-10), the width of the columns with a fixed width (columns of type p, b, m and X) is used for the mono-column blocks of that column. So, in your case, you have directly the expected result.

\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{nicematrix}
\usepackage{rotating}

\begin{document}

\begin{sidewaystable}
\begin{NiceTabular}[hvlines,colortbl-like]{p{1cm}p{2cm}Wc{3cm}p{4cm}p{4cm}} 
    \Block[fill=[HTML]{FFFF00}]{2-1}{Table Title}    &
    \Block[fill=[HTML]{FFFF00}]{2-1}{Column 1}   &  Column   &   \Block[fill=[HTML]{FFFF00}]{2-2}{Column 3} &  \\ 
    &     &     2    &       &   \\ 
    Row 1 & Value 1 & Value 2    & Value 3 & Value 4 \\ 
    \Block[fill=[HTML]{780373}]{2-1}{Row 2}  &  
    Long text outside of Block &     
    \Block[fill=[HTML]{548235}]{2-1}{This cell contains a long value}    &   \Block[fill=[HTML]{0070C0}]{2-2}{Value 8} &  \\ 
    & Value 6 &  & & \\ 
\end{NiceTabular}   
\end{sidewaystable}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

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.