How to draw a diagonal slash through an entire matrix in LaTeX?

I want to write the above matrix in Latex. How get i it?

Thanks!

• Welcome to TeX.SE. Which part of the tabular material are you trying to copy? Is it the top-left cell with the diagonal "slash"? If not, what else? Please advise.
– Mico
Jul 1, 2018 at 22:44
• Yes, with the diagonal "slash", so over 15,27 etc.
– Dido
Jul 1, 2018 at 23:10

No TikZ? Hmmm, this looks like a job for picture-mode!

\documentclass{article}

\usepackage{pict2e}% pict2e for an overhaul of picture-mode. Can be commented
\usepackage{xcolor}% xcolor for the gray line

% If you don't want xcolor you can just uncomment this line:
% \makeatletter \let\color\@gobble \makeatother

\def\familydefault{\sfdefault}% sans-serif font

\makeatletter
\def\clap#1{\hb@xt@ \z@ {\hss #1\hss}}
\count@\z@
\@for\el:=#2\do{%
\put(\the\count@,#1){\clap{\el}}%
}%
}
\makeatother

\begin{document}

\setlength{\unitlength}{1cm}
\begin{picture}(5,2.4)(-0.5,0)
\thicklines
\put( 0.5,-0.1){\line(0,1){2.5}}
\put(-0.5, 1.9){\line(1,0){5}}
\put(-0.5, 2.4){\color{gray}\line(2,-1){5}}
\put(-0.5, 2.4){\line(2,-1){1}}
\put(-0.2, 1.95){\tiny\clap{wirklich}}
\put( 0.2, 2.3){\tiny\clap{prog}}
\end{picture}

\end{document}


Alternative: less compact version:

\begin{picture}(5,5)(-0.5,-0.5)
\thicklines
\put( 0.5,-0.5){\line(0,1){5}}
\put(-0.5, 3.5){\line(1,0){5}}
\put(-0.5, 4.5){\color{gray}\line(1,-1){5}}
\put(-0.5, 4.5){\line(1,-1){1}}
\put(-0.2, 3.6){\tiny\clap{wirklich}}
\put( 0.2, 4.4){\tiny\clap{prog}}
\end{picture}


With TikZ. (EDIT: made the separation between the columns and rows a bit larger.)

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newsavebox{\tlbox}
\begin{document}
\savebox\tlbox{\tikz[inner sep=0pt,ampersand replacement=\&]{
\matrix (cheat) [matrix of nodes,nodes={align=center,font=\sffamily\tiny}]
{
\& prog \\
wirklich \&  \\
};
}}
\begin{tikzpicture}
\matrix (magic) [matrix of nodes,nodes={align=center,font=\sffamily,
minimum width=1.4\wd\tlbox,minimum height=1.8\ht\tlbox,inner sep=0pt}]
{
|[inner sep=0pt]|\usebox\tlbox
& 1 & 2 & 3 & 4  \\
1 & 15 & 2 & 2 & 0 \\
2 & 4 & 27 & 2 & 10 \\
3 & 1 & 0 & 3 & 0 \\
4 & 1 & 3 & 0 & 102 \\
};
\draw[thick] (magic-2-1.north west) -- (magic-2-5.north east);
\draw[thick] (magic-1-2.north west) -- (magic-5-2.south west);
\draw[opacity=0.2] (magic-1-1.north west) -- (magic-5-5.south east);
\draw[thick] (magic-1-1.north west) -- (magic-1-1.south east);
\end{tikzpicture}
\end{document}


It is also possible to do without TikZ but my knowledge of these methods is so poor that it requires some finetuning and even then it is far from perfect. Of course, some table experts will find a much better solution.

\documentclass{article}
\usepackage{diagbox}
\usepackage{cancel,xcolor}
\newsavebox{\tabbox}
\begin{document}
\renewcommand{\arraystretch}{1.44}
\setlength{\tabcolsep}{7.5pt} ~\textcolor{gray}{\bcancel{%
\textcolor{black}{\begin{tabular}{c@{}|*{4}{c}}
\diagbox[trim=lr,linewidth=2pt]{\tiny wirklich}{\tiny prog}
& 1 & 2 & 3 & 4  \\
\hline
1 & 15 & 2 & 2 & 0 \\
2 & 4 & 27 & 2 & 10 \\
3 & 1 & 0 & 3 & 0 \\
4 & 1 & 3 & 0 & 102
\end{tabular}}}}
\end{document}


• Thank you very much for your suggestion. Is it possible without TikZ?
– Dido
Jul 2, 2018 at 0:11
• @Dido The answer is probably yes, I added something that may or may not be a starting point.
– user121799
Jul 2, 2018 at 2:02