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

Murmi
  • 53
  • 2
    \Block[fill=[HTML]{548235}]{2-1}{\parbox{3cm}{This cell contains a long value}}? –  May 02 '21 at 06: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 02 '21 at 08:08

2 Answers2

4

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

F. Pantigny
  • 40,250
  • Thank you for your response! parbox solved the problem. I modified Michael Kirker's excel2latexviapython to use nicematrix instead of tabular. https://github.com/Murmele/excel2latexviapython – Murmi Jun 06 '21 at 19:31
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

F. Pantigny
  • 40,250