7

enter image description here

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

Thanks!

  • 1
    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 '18 at 22:44
  • Yes, with the diagonal "slash", so over 15,27 etc. – Dido Jul 1 '18 at 23:10
10

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

enter image description here

\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}}
\def\addrow#1#2{%
  \count@\z@
  \@for\el:=#2\do{%
    \put(\the\count@,#1){\clap{\el}}%
    \advance\count@\@ne
  }%
}
\makeatother

\begin{document}

\setlength{\unitlength}{1cm}
\begin{picture}(5,2.4)(-0.5,0)
  \thicklines
  \addrow{0.0}{4,1 ,3 ,0 ,102}
  \addrow{0.5}{3,1 ,0 ,3 ,0}
  \addrow{1.0}{2,4 ,27,10,2}
  \addrow{1.5}{1,15,2 ,2 ,0}
  \addrow{2.0}{ ,1 ,2 ,3 ,4}
  \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:

enter image description here

\begin{picture}(5,5)(-0.5,-0.5)
  \thicklines
  \addrow{0}{4,1 ,3 ,0 ,102}
  \addrow{1}{3,1 ,0 ,3 ,0}
  \addrow{2}{2,4 ,27,10,2}
  \addrow{3}{1,15,2 ,2 ,0}
  \addrow{4}{ ,1 ,2 ,3 ,4}
  \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}
5

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}

enter image description here

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}

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.