2

Once again when porting Latex Table code from one journal to springer format I am getting missing border lines( image attached). Latex code is as follows. Please suggest solution for missing border and auto line breaking to fit page width instead of manual I have done using \\

\usepackage{adjustbox} 
%\usepackage{tabularx}
\usepackage{tabularx,ragged2e}
\documentclass{sn-jnl}
\begin{document}
\begin{table*}[!h]
    %\tiny
    \centering
    \small
    \caption{Result}
    \begin{tabular}{|c|c|c|c|}
        \hline
        \multicolumn{1}{|l|}{\begin{tabular}[c]{@{}l@{}}Users Set\\Size (USS)\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}} Range of Users \\ Individual \end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Index of scalar array \\ for mapping\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Transfer \\ Failure\end{tabular}} \\ \hline
        \multirow{6}{*}{6}                                                                        & \multirow{2}{*}{6}                    & 10                                                                                                       & 23                                                                                           \\ \cline{3-4} 
        &                                       & 4                                                                                                      & \textbf{18.3}                                                                                           \\ \cline{2-4} 
        & \multirow{2}{*}{7}                    & 0                                                                                                      & 13.3                                                                                          \\ \cline{3-4} 
        &                                       & 4                                                                                                       & \textbf{33.3}                                                                                           \\ \cline{2-4} 
        & \multirow{2}{*}{7}                    & 1                                                                                                       & 17                                                                                           \\ \cline{3-4} 
        &                                       & 2                                                                                                       & \textbf{66.6}                                                                                           \\ \hline
    \end{tabular}

\end{table}

\end{document}

enter image description here

  • You should place \documentclass{sn-jnl} before loading any packages in your code. The document class is probably available from here. I'll look into it, but if I use the standard article class and add \usepackage{multirow}, I get a nicely typeset table. – Jasper Habicht Mar 03 '23 at 19:21
  • @SimonDispa Since I provided an answer here, maybe we close the other question? Sorry, I did not see that this is in fact the same question asked before. – Jasper Habicht Mar 03 '23 at 20:02

3 Answers3

5

The problem is that the document class sn-jnl (2019/11/18 v0.1, line 1333) defines \let\cline\cmidrule. However, at that point, \cmidrule is still undefined, because the package booktabs hasn't been loaded at this point. This will result in an error since \cline is undefined. This should be actually be considered a bug in the code of the document class. You can work around this bug by loading the booktabs package using \RequirePackage{booktabs} before using \documentclass (\usepackage won't work before \documentclass).

However, putting this built-in bug aside, the authors of the document class obviously want you to use the booktabs package which does not support vertical lines. The use of the macros provided by this package, among which is also \cmidrule, insert some vertical spacing which breaks vertical rules. The sn-jnl class now sets \cline as alias to \cmidrule, which is why you get these broken borders.

There are two ways to get around this. The first way is to get along whith what the authors of this package probably intended and embrace features the provided by the booktabs package, that is, avoid the use of vertical lines and maybe even go without \multirows:

\documentclass{sn-jnl}
\begin{document}
\begin{table*}[!h]
    \centering
    \small
    \caption{Result}
    \begin{tabular}{ c c c c }
        \toprule
        \begin{tabular}{@{}l@{}} Users Set \\ Size (USS) \end{tabular} & 
        \begin{tabular}{@{}l@{}} Range of Users \\ Individual \end{tabular} & 
        \begin{tabular}{@{}l@{}} Index of scalar array \\ for mapping \end{tabular} &
        \begin{tabular}{@{}l@{}} Transfer \\ Failure \end{tabular} \\ 
        \midrule
        6  & 6  & 10  & 23            \\ \cline{3-4} 
           &    & 4   & \textbf{18.3} \\ \cline{2-4} 
           & 7  & 0   & 13.3          \\ \cline{3-4} 
           &    & 4   & \textbf{33.3} \\ \cline{2-4} 
           & 7  & 1   & 17            \\ \cline{3-4} 
           &    & 2   & \textbf{66.6} \\ 
        \bottomrule
    \end{tabular}
\end{table*}

\end{document}

enter image description here


Another, more hacky way would be to store the original \cline macro before loading the document class and later restore the definition of \cline:

\let\clineorig\cline
\documentclass{sn-jnl}
\usepackage{multirow}

\begin{document}

\begin{table*}[!h]
    \centering
    \small
    \caption{Result}
    \let\cline\clineorig
    \begin{tabular}{|c|c|c|c|}
        \hline
        \begin{tabular}{@{}l@{}} Users Set \\ Size (USS) \end{tabular} & 
        \begin{tabular}{@{}l@{}} Range of Users \\ Individual \end{tabular} & 
        \begin{tabular}{@{}l@{}} Index of scalar array \\ for mapping \end{tabular} & 
        \begin{tabular}{@{}l@{}} Transfer \\ Failure \end{tabular} \\ 
        \hline
        \multirow{6}{*}{6} & \multirow{2}{*}{6} & 10  & 23            \\ \cline{3-4} 
                           &                    & 4   & \textbf{18.3} \\ \cline{2-4} 
                           & \multirow{2}{*}{7} & 0   & 13.3          \\ \cline{3-4} 
                           &                    & 4   & \textbf{33.3} \\ \cline{2-4} 
                           & \multirow{2}{*}{7} & 1   & 17            \\ \cline{3-4} 
                           &                    & 2   & \textbf{66.6} \\ 
    \hline
    \end{tabular}
\end{table*}

\end{document}

enter image description here

I personally would recommend the first solution.


As for the line breaking: It is totally fine to nest tabulars, but you don't need to place them inside \multicolumn macros. I simplified the code in this regard. Another way could be to apply the p{} column type which allows you to break lines, but you would need to tell LaTeX the width of the column (for example p{1cm} for a column width of 1cm).

  • Thanks all Specially @Zarko. In my other table I have 3 rows instead of two in the third column. To draw this I have modified cell{even}{2} = {r=2}{} to cell{odd}{3} = {r=3}{} and also \SetCell[r=6]{c} to \SetCell[r=9]{c} but I am getting errors – user3696623 Mar 04 '23 at 07:12
  • Thank you! I need a horizontal multi-column line as in the example of your first approach. However, it did not work. How about partially using your second approach by adding \let\clineorig\cline line before documentclass as in your example and use \clineorig{3-4} instead of \cline{3-4}? It works but I wonder if there is any drawbacks? – burkay Nov 14 '23 at 01:35
  • @burkay With \let\clineorig\cline you set the command \clineorig to behave exaxtly as \cline does at this point. So you kind of store the functionality of the \cline command before it is redefined. With the later \let\cline\clineorig the redefined \cline command is then restored to the original defintion. So, after this second line, both commands do exactly the same thing. Of course, you can just use \clineorig if you don't want to re-redefine \cline using \let\cline\clineorig. – Jasper Habicht Nov 14 '23 at 08:25
  • @JasperHabicht I started using \clineorig thanks to your answer. I felt like you implied the second approach might have some drawbacks. I was just wondering if that is the case even if we do not re-redefine. Anyway, thank you again. – burkay Nov 19 '23 at 19:04
  • @burkay I see. I don’t think that there are any drawbacks. It you don’t redefine, you keep the original definition without overriding the newer, so you can choose between both which is, as far as I can see, a good solution. – Jasper Habicht Nov 19 '23 at 19:55
  • @JasperHabicht Awesome, thank you :) – burkay Nov 20 '23 at 22:17
5

With tabularray and siunitx packages problems with vertical lines are eliminated and numbers in the last column are aligned at decimal points:

\documentclass{sn-jnl}

\usepackage{xcolor} \usepackage{tabularray} \UseTblrLibrary{siunitx}

\begin{document} \begin{table}[ht] \caption{Result} \sisetup{table-format=2.1, detect-weight, % <-- } \begin{tblr}{hlines, vlines, colspec = {X[c] X[1.2,c] X[1.8,c] X[c, si]}, cell{even}{2} = {r=2}{}, cell{odd[3]}{Z} = {font=\bfseries}, row{1} = {guard} } Users Set Size (USS) & Range of Users Individual & Index of scalar array for mapping & Transfer Failure \ \SetCell[r=6]{c} 6 & 6 & 10& 23 \ & & 4 & 18.3 \ & 7 & 0 & 13.3 \ & & 4 & 33.3 \ & 7 & 1 & 17 \ & & 2 & 66.6 \ \end{tblr} \end{table} \end{document}

enter image description here

Zarko
  • 296,517
3

For information, here what you obtain by using {NiceTabular} instead of {tabular}.

  • I have loaded nicematrix with \usepackage{nicematrix} but nicematrix uses pgf and, in the class sn-jnl, pgf must be loaded before the \documentclass (with \RequirePackage).

  • I have replaced \begin{tabular} and end{tabular} by \begin{NiceTabular} and \end{NiceTabular}.

\RequirePackage{pgf}
\documentclass{sn-jnl}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{|c|c|c|c|} \hline \multicolumn{1}{|l|}{\begin{tabular}[c]{@{}l@{}}Users Set\Size (USS)\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}} Range of Users \ Individual \end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Index of scalar array \ for mapping\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Transfer \ Failure\end{tabular}} \ \hline \multirow{6}{}{6} & \multirow{2}{}{6} & 10 & 23 \ \cline{3-4} & & 4 & \textbf{18.3} \ \cline{2-4} & \multirow{2}{}{7} & 0 & 13.3 \ \cline{3-4} & & 4 & \textbf{33.3} \ \cline{2-4} & \multirow{2}{}{7} & 1 & 17 \ \cline{3-4} & & 2 & \textbf{66.6} \ \hline \end{NiceTabular}

\end{document}

Output of the above code

The rules are not broken because, in the environments of nicematrix, cline is redefined (and that definition locally overwrite the redefinition of \ncline done by sn-jnl).

However, with {NiceTabular} the standard way to construct your table (with the tools of nicematrix) is as follows.

\RequirePackage{pgf}
\documentclass{sn-jnl}
\usepackage{nicematrix}
\usepackage{siunitx}

\begin{document}

\sisetup{table-format = 2.1,detect-weight}

\begin{NiceTabular}{cccS}[hvlines] \Block[l]{}{Users Set\ Size (USS)} & \Block[l]{}{Range of Users\ Individual} & \Block[l]{}{Index of scalar array\ for mapping} & \Block[l]{}{Transfer\ Failure} \ \Block{6-1}{6} & \Block{2-1}{6} & 10 & 23 \ & & 4 & \bfseries 18.3 \ & \Block{2-1}{7} & 0 & 13.3 \ & & 4 & \bfseries 33.3 \ & \Block{2-1}{7} & 1 & 17 \ & & 2 & \bfseries 66.6 \ \end{NiceTabular}

\end{document}

Output of the second code

F. Pantigny
  • 40,250