# Vertical line missing in a cell missing

I try to draw a diagonal line in the first cell in the table below, using Tikz. But the vertical border at its left is missing. How could I fix it? The code is:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{makecell}

\newcolumntype{x}[1]{>{\rule[-2.5mm]{0pt}{5.5mm}%
\centering\arraybackslash}p{#1}}
\newcommand\diag[4]{%
\multicolumn{1}{p{#2}|}{\hskip-\tabcolsep
$\vcenter{\begin{tikzpicture}[baseline=0,anchor=south west,inner sep=#1] \path[use as bounding box] (0,0) rectangle (#2+2\tabcolsep,\baselineskip); \node[minimum width={#2+2\tabcolsep-\pgflinewidth}, minimum height=\baselineskip+\extrarowheight-\pgflinewidth] (box) {}; \draw[line cap=round] (box.north west) -- (box.south east); \node[anchor=south west] at (box.south west) {#3}; \node[anchor=north east] at (box.north east) {#4}; \end{tikzpicture}}$\hskip-\tabcolsep}}

\begin{document}

\setlength{\extrarowheight}{0.2cm}
\begin{tabular}{|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|}\hline
\diag{.1em}{.5cm}{$X$}{$Y$}&12&18&50&16\\ \hline
&&&&20\\ \hline
&&&&30\\ \hline
&&&&45\\ \hline
15&12&18&50&32\\ \hline
\end{tabular}

\end{document}


and the output is:

EDIT: I used the code

\documentclass{article}

\usepackage{makecell}
\newcolumntype{x}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

\begin{table}[tbp]
\begin{center}
\setcellgapes{5pt} \makegapedcells \setlength\extrarowheight{1pt}
\begin{tabular}{|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|}\hline
\diaghead(-1,1){XX}{$X$}{$Y$}   & 0 & 1 & 2 & 3\\ \hline
0 & $\frac{1}{20}$ & $\frac{2}{20}$ & $\frac{1}{20}$ & $\frac{1}{20}$\\ \hline
1 & $\frac{2}{20}$ & $\frac{3}{20}$ & $0$&$\frac{1}{20}$\\ \hline
2 & $\frac{3}{20}$ & $0$ & $\frac{4}{20}$ & $\frac{2}{20}$\\ \hline
\end{tabular}
\end{center}
\end{table}

\end{document}


How can I adjust the diagonal line to link properly the corners in the first cell?

• \newcommand\diag[4]{% \multicolumn{1}{|p{#2}|}{... should help. Notice the additional | right before p{#2}. – leandriis May 30 at 18:53
• @leandriis Thank you so very much! – Cris May 30 at 18:55

With {NiceTabular} of nicematrix (≥ 4.0), you can draw the tabular as you have done and you have a command \diagbox (available only in the environments of nicematrix) which draws what is asked in the question. Moreover, you can simplify the code by using the key hvlines which draws all the rules.

\documentclass{article}
\usepackage{nicematrix}

\newcolumntype{x}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

\setlength{\extrarowheight}{0.2cm}
\begin{NiceTabular}{*{5}{x{0.5cm}}}[hvlines]
\diagbox{$X$}{$Y$}&12&18&50&16\\
&&&&20\\
&&&&30\\
&&&&45\\
15&12&18&50&32
\end{NiceTabular}

\end{document}


If you want to center vertically the contents of the cells, all the techniques of {tabular} of arrayare available. Maybe the easier is to add a strut with a \rule:

\documentclass{article}
\usepackage{nicematrix}

\newcolumntype{x}[1]{>{\rule[-2.5mm]{0pt}{5.5mm}%
\centering\arraybackslash}p{#1}}

\begin{document}

\setlength{\extrarowheight}{2mm}
\begin{NiceTabular}{*{5}{x{0.5cm}}}[hvlines]
\diagbox{$X$}{$Y$}&12&18&50&16\\
&&&&20\\
&&&&30\\
&&&&45\\
15&12&18&50&32
\end{NiceTabular}

\end{document}


• Thanks for the answer. With your solution, how could the text in the cells be centered vertically? – Cris May 30 at 20:39
• All the usual technics are available. I have modified my answer to present a method. – F. Pantigny May 30 at 21:04
• Amazing! I must have missed NiceTabular when glancing through the manual.... – user194703 May 30 at 21:13
• @Schrödinger's cat: NiceTabular is very recent: in v4.0, 2020/05/08 and in fact, in that version, the command \diagbox was present but not documented. It is documented in v4.1, 2020/05/27. The documentation of that version has been changed to insist more on NiceTabular. – F. Pantigny May 30 at 21:23
• Thank you for the answer. I used the command you suggested in the first solution by @leandriis, since I have some problems when trying to install nicematrix.sty. I don't know how to adjust now the diagonal line in the first cell to link properly the corners. The output is in the edited question. – Cris May 31 at 5:04

With \multicolumn{1}... you can not only overwrite the horizontal alignment of a cell but also remove/add vertical lines on either side of that cell.

In your original definition of the \diag command, you used \multicolumn{1}{p{#2}|} which adds a vertical line at the right of the cell. Change this to \multicolumn{1}{|p{#2}|} with an additional | right before the p.... in order to also get the vertical line on the eft of the cell.

• It works well. Thank you for the answer! – Cris May 30 at 19:36
• Is there any solution to center vertically the text in the cells? – Cris May 30 at 20:28

Since you load makecell I would use its macro\diaghead:

This package offers macro based on possibilities of picture environment.

\diaghead(<H ratio,V ratioi>){<Text set for column


where () sets the ratios like in \line command (digits from 1 up to 6). This argument is optional, the default ratio (\line direction) defined (5,-2).

\documentclass{article}
\usepackage{makecell}
\newcolumntype{x}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{table}[ht]
\setcellgapes{5pt}
\makegapedcells
\begin{tabular}{|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|}\hline
\diaghead(-1,1){XX}{$Y$}{$X$}   & 12 & 18 & 50 & 16 \\ \hline
&    &    &    & 20 \\ \hline
&    &    &    & 30 \\ \hline
&    &    &    & 45 \\ \hline
15  &12  & 18 & 50 & 32 \\ \hline
\end{tabular}
\end{table}
\end{document}


• Thanks a lot ! It is all right. – Cris May 31 at 9:38
• @Cris, content of cells are centered to see this instead numbers insert letters "abg". Since numbers haven't depth as have letters g,q,y, it seems that the space above them is smaller than below. This depth you can compensate with adding \setlength\extrarowheight{1pt} after makegapedcellgape in table code. – Zarko May 31 at 9:51
• I still have a problem with the diagonal line in the first cell. I think this is because the number ol columns and of the rows are different. I used your code and the new output is in the question. I didn't work too much with tables. – Cris May 31 at 10:05
• @Cris, you are right, extrarowheight is not the solution. With my answer is not possible better vertical centering as already achieved. – Zarko May 31 at 10:37