1

I am trying to set the vertical height of a row which uses diagbox to be smaller, but past techniques such as \setarstrut and cellspace appear to not be working thus far. In particular, I am able to almost create the table exactly as sought, but the the row with the diagonal is too tall. How exactly can one set this particular row to be a constant height (e.g. to match the height of the other rows)?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{diagbox}
\usepackage{amsmath}

\begin{document}

\centering
\renewcommand{\arraystretch}{1.25}

\begin{tabular}{ |p{0.8cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}| } \hline \multicolumn{7}{|c|}{Table} \ \hline \backslashbox{$P_1$\kern-1em}{\kern-1em$P_2$} & $10$ & $50$ & $100$ &$500$ &$1000$ &$5000$\ \hline $10$ & 0.0238 & 0.0238 & 0.0087 & 0.0238 & 0.0238 & 0.0238 \ \hline $50$ & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 \ \hline $100$ & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 \ \hline $500$ & 0.0238 & 0.0238 & 0.0065 & 0.0081 & 0.0083 & 0.0129 \ \hline \end{tabular} \end{document}

Table

user202729
  • 7,143
Mathews24
  • 121

3 Answers3

2

The environement {NiceTabular} of nicematrix has a built-in command \diagbox which gives directly the expected output. Moreover, you have a key hvlines which draws all the rules excepted in the blocks.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\centering
\renewcommand{\arraystretch}{1.25}
\begin{NiceTabular}{p{0.8cm}*{6}{p{1.1cm}}}[hvlines]
 \Block{1-7}{Table} \\
 \diagbox{$P_1$}{$P_2$} & $10$ & $50$ & $100$ &$500$ &$1000$ &$5000$\\
 $10$ & 0.0238 & 0.0238  & 0.0087 & 0.0238 & 0.0238 & 0.0238 \\
 $50$ & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 \\
 $100$ & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 \\
 $500$ & 0.0238 & 0.0238 & 0.0065 & 0.0081 & 0.0083 & 0.0129 \\
\end{NiceTabular}
\end{document}

However, you need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

F. Pantigny
  • 40,250
  • Works great! As a follow-up, is there a recommended way to centre the text in each cell? – Mathews24 Oct 16 '20 at 18:45
  • To center the text in each cell, you can use the type c for the columns (the columns will have their natural width). If you want to fix yourself, the width, you can use the type w: w{c}{1cm} for a column of width 1 cm (with content centered). – F. Pantigny Oct 17 '20 at 09:26
1

The \diagbox command can take a height parameter as its input. For example: \diagbox[height=1cm]{$P_1$}{$P_2$}

Ashish
  • 19
0

Replace column type in the first column to c:

\documentclass{article}
\usepackage{diagbox}
\usepackage{siunitx}

\begin{document}

\centering
\renewcommand{\arraystretch}{1.25}

\begin{tabular}{ |c| *{6}{S[table-format=1.4]|} } \hline \multicolumn{7}{|c|}{Table} \ \hline \backslashbox{$P_1$\kern-1em}{\kern-1em$P_2$} & {10} & {50} & {100} & {500} & {1000} & {5000} \ \hline 10 & 0.0238 & 0.0238 & 0.0087 & 0.0238 & 0.0238 & 0.0238 \ \hline 50 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 \ \hline 100 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 \ \hline 500 & 0.0238 & 0.0238 & 0.0065 & 0.0081 & 0.0083 & 0.0129 \ \hline \end{tabular} \end{document}

enter image description here

Addendum: But, your table I would rather write as follows:

\documentclass{article}
\usepackage{booktabs, diagbox}
\usepackage{siunitx}
\usepackage[skip=0.333\lineskip]{caption}

\begin{document}

\begin{table}[ht]
\caption*{Table}
\centering

\begin{tabular}{S[table-format=3.0] {6}{S[table-format=1.4]} } \toprule {\multirow{2.2}{}{$P_1$}} & \multicolumn{6}{c}{$P_2$} \ \cmidrule(l){2-7} & {10} & {50} & {100} & {500} & {1000} & {5000} \ \midrule 10 & 0.0238 & 0.0238 & 0.0087 & 0.0238 & 0.0238 & 0.0238 \ 50 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 \ \addlinespace 100 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 & 0.0238 \ 500 & 0.0238 & 0.0238 & 0.0065 & 0.0081 & 0.0083 & 0.0129 \ \bottomrule \end{tabular} \end{table} \end{document}

enter image description here

Zarko
  • 296,517