0

The following table is set up as can be seen below. I would like to combine duplicated values in column 1-3 by merging the table cells column-wise. However, as you can see the merging of cells is on a different level or spans a different number of cells in each considered column.

    \begin{longtable}[!htbp]{ |m{2cm} m{3.7cm} m{2cm} | m{1.8cm} m{1.8cm} m{1.8cm}| }
 \hline
 \textbf{Rating-level} & \textbf{Rating category} & \textbf{Credit risk} & \textbf{Fitch \newline (original)} & \textbf{S\&P \newline (original)} & \textbf{Moody's \newline (original)} \\
 \hline
    \rowcolor{YellowGreen}IG & Highest-grade & Lowest & AAA & AAA & Aaa \\
    \rowcolor{YellowGreen}IG & High grade & Very low & AA+ & AA+ & Aa1 \\
    \rowcolor{YellowGreen}IG & High grade & Very low & AA & AA & Aa2 \\
    \rowcolor{YellowGreen}IG & High grade & Very low & AA- & AA- & Aa3 \\
    \rowcolor{YellowGreen}IG & Upper medium grade & Low & A+ & A+ & A1 \\
    \rowcolor{YellowGreen}IG & Upper medium grade & Low & A & A & A2 \\
    \rowcolor{YellowGreen}IG & Upper medium grade & Low & A- & A- & A3 \\
    \rowcolor{YellowGreen}IG & Medium grade & Moderate & BBB+ & BBB+ & Baa1 \\
    \rowcolor{YellowGreen}IG & Medium grade & Moderate & BBB & BBB & Baa2 \\
    \rowcolor{YellowGreen}IG & Medium grade & Moderate & BBB- & BBB- & Baa3 \\ 
    \hline
    \rowcolor{Apricot}Non-IG & Speculative grade & Substantial & BB+ & BB+ & Ba1 \\
    \rowcolor{Apricot}Non-IG & Speculative grade & Substantial & BB & BB & Ba2 \\
    \rowcolor{Apricot}Non-IG & Speculative grade & Substantial & BB- & BB- & Ba3 \\
    \rowcolor{Apricot}Non-IG & Speculative grade & High & B+ & B+ & B1 \\
    \rowcolor{Apricot}Non-IG & Speculative grade & High & B & B & B2 \\
    \rowcolor{Apricot}Non-IG & Speculative grade & High & B- & B- & B3 \\
    \rowcolor{Apricot}Non-IG & Highly speculative grade & Very high & CCC+ & CCC+ & Caa1 \\
    \rowcolor{Apricot}Non-IG & Highly speculative grade & Very high & CCC & CCC & Caa2 \\
    \rowcolor{Apricot}Non-IG & Highly speculative grade & Very high & CCC- & CCC- & Caa3 \\
    % \rowcolor{Apricot}Non-IG & & & & & & \\
    \rowcolor{Apricot}Non-IG & Highly speculative grade & Likely in or \newline near default & CC & CC & \\ 
    % \rowcolor{Apricot}Non-IG & & & & & & \\
    \rowcolor{Apricot}Non-IG & Highly speculative grade & Likely in or \newline near default & & & Ca \\
    % \rowcolor{Apricot}Non-IG & & & & & & \\
    \rowcolor{Apricot}Non-IG & Highly speculative grade & Likely in or \newline near default & C & C & \\   
    \hline
    \rowcolor{Red}Default & Default & In default & D & D & C \\
 \hline
\caption{Numeric ratings mapping table} 
% needs to go inside longtable environment
\label{tab:4thlongtable}
\end{longtable}

I tried hours with stacking the multicolumn command on various levels but none gave me the desired output. To clarify, in column 1, I would like to have 3 stacked cells (IG, Non-IG, Def), in column 2 accordingly 7 stacked cells and in column 3 accordingly 9 cells. Somehow every method I tried, completely screwed up the table layout, merging row-wise and column-wise.

Would highly appreciate any pointers!

3
  • Please make your code compilable by adding the docmentclass as well as the relevant packages. Please also add a sketch of the expected output.
    – leandriis
    Commented Mar 14, 2021 at 14:58
  • In order to clarify: Do you want all 3 cells that currently contain the text "upper medium grade" to be merged into a single cell that spans 3 rows, and likewise for all other sets of cells? If that's the case, you should take a look at the \multirow command as opposed to \multicolumn.
    – leandriis
    Commented Mar 14, 2021 at 15:00
  • Currently, your table easily fits onto a single page, but you nevertheless used longtable. Do you want to add more rows to your table later?
    – leandriis
    Commented Mar 14, 2021 at 15:01

2 Answers 2

0

I guess, the following is closer to the expected output:

enter image description here

\documentclass{article}
\usepackage{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{nicematrix}
\NiceMatrixOptions{cell-space-limits = 0.5\tabcolsep}

\begin{document}
\begin{table}
\centering
\caption{Numeric ratings mapping table} 
\label{tab:4thlongtable}
    \begin{NiceTabular}{ llllll }[hvlines, %
                                  code-before={%
                                     \rowcolor{YellowGreen}{2-11}
                                     \rowcolor{Apricot}{12-23}
                                     \rowcolor{Red}{24}}]
 \textbf{\Block{1-1}{Rating-\\ level}} & \textbf{\Block{1-1}{Rating\\ category}} & \textbf{\Block{1-1}{Credit risk}} & \textbf{\Block{1-1}{Fitch \\ (original)}} & \textbf{\Block{1-1}{S\&P \\ (original)}} & \textbf{\Block{1-1}{Moody's \\ (original)}} \\
    \Block[l]{10-1}{IG}     & Highest-grade                                & Lowest                                        & AAA  & AAA  & Aaa \\
                            & \Block[l]{3-1}{High grade}                   & \Block[l]{3-1}{Very low}                      & AA+  & AA+  & Aa1 \\
                            &                                              &                                               & AA   & AA   & Aa2 \\
                            &                                              &                                               & AA-  & AA-  & Aa3 \\
                            & \Block[l]{3-1}{Upper medium\\ grade}         & \Block[l]{3-1}{Low}                           & A+   & A+   & A1 \\
                            &                                              &                                               & A    & A    & A2 \\
                            &                                              &                                               & A-   & A-   & A3 \\
                            & \Block[l]{3-1}{Medium grade}                 & \Block[l]{3-1}{Moderate}                      & BBB+ & BBB+ & Baa1 \\
                            &                                              &                                               & BBB  & BBB  & Baa2 \\
                            &                                              &                                               & BBB- & BBB- & Baa3 \\ 
    \Block[l]{12-1}{Non-IG} & \Block[l]{6-1}{Speculative\\ grade}          & \Block[l]{3-1}{Substantial}                   & BB+  & BB+  & Ba1 \\
                            &                                              &                                               & BB   & BB   & Ba2 \\
                            &                                              &                                               & BB-  & BB-  & Ba3 \\
                            &                                              & \Block[l]{3-1}{High}                          & B+   & B+   & B1 \\
                            &                                              &                                               & B    & B    & B2 \\
                            &                                              &                                               & B-   & B-   & B3 \\
                            & \Block[l]{6-1}{Highly \\speculative\\ grade} & \Block[l]{3-1}{Very high}                     & CCC+ & CCC+ & Caa1 \\
                            &                                              &                                               & CCC  & CCC  & Caa2 \\
                            &                                              &                                               & CCC- & CCC- & Caa3 \\
                            &                                              & \Block[l]{3-1}{Likely in\\ or near\\ default} & CC   & CC   & \\ 
                            &                                              &                                               &      &      & Ca \\
                            &                                              &                                               & C    & C    & \\   
    Default                 & Default                                      & In default                                    & D    & D    & C \\
\end{NiceTabular}
\end{table}
\end{document}
11
  • What means closer? That's exactly what I tried all day yesterday! Spot on! I'll mark the answer as 'accepted' accordingly. I do appreciate your help tremendously. Take care! Commented Mar 14, 2021 at 16:08
  • Looking through the code here, I must admit, I would probably have never arrived at a viable solution. Guess, first I'll have to do my homework and familiarise myself with the package. Thanks again Commented Mar 14, 2021 at 16:14
  • One follow-up question though. So far, the proposed code/solution compiles but outputs a table that is no different from the original one. My online-based shareLaTeX file compiles but reports that \NiceMatrixOptions{cell-space-limits = 0.5\tabcolsep} is unknown for the package. And at the position I place the table in, the \end{NiceTabular}throws an error that reads like LaTeX3 Error: The key 'NiceMatrix/Blcok/l' is unknown and being ignored& LaTeX error: Something's wrong -- perhaps a missing \item. I searched the code but didn't find any missing \. Commented Mar 14, 2021 at 16:23
  • To clarify, I have put the missing packages in my main.tex file in the preamble and some were already loaded. I use the document class report though if that matters in this situation. Otherwise I assume all else being equal to your setup. Does the chosen compile (i.e. XeLaTeX, etc.) matter in this situation? Commented Mar 14, 2021 at 16:30
  • The cell-space-limits that I used in \NiceMatrixOptions was introduced with version 5.9, so most likely you used an older version of the package to compile your code. (current version is 5.12) Either update your packages, or, if you can't, you could try cell-space-top-limit = 0.5\tabcolsep, cell-space-bottom-limit = 0.5\tabcolsep instead. This should work with versions newer than 4.1. The second error message is caused by the optional argument [l] of \Block being unknown. This argument to control the horizontal alignment was introduced in nicematrix version 5.3.
    – leandriis
    Commented Mar 14, 2021 at 17:04
0

Didn't know how to include a picture otherwise, so pls see below what I get with your solution so far. I managed to compile the table in a way that the merging of the cells column-wise looks like in your example, but somehow the column width is out of bounds. I tried manually adjusting them using the initial column types that allow specification of the column width in the table environment (p{2cm} and m{2cm}, but that didn't work.

After looking hours in the pkg documentation, I am not any further at a working solution. I would love your feedback on this follow-up question, if you have an idea. Thanks!

enter image description here

1
  • Did solve it a similar fashion as to what leandriis proposed. Essentially pulled the nicematrix.sty file from the public repo and put it in my project directory on the same level as my main file. That solved the issues. Thanks again! Commented Mar 16, 2021 at 11:41

You must log in to answer this question.

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