51

Possible Duplicate:
Diagonal lines in table cell

I'd like to include a table of numbers in my document. Those numbers are defined by two parameters n and k, and I'd like to achieve a presentation similar to what Comtet did in "Advanced Combinatorics". More precisely, I'm interested in dividing the top-left cell to include both n and k, as he did in the example below:

alt text

How do I do that?

2 Answers2

64

A simple solution is to use the slashbox package

\documentclass{article}
\usepackage{slashbox}
\begin{document}

\begin{tabular}{c|cc}
 \backslashbox{n}{k} & 0 & 1\\\hline
0 & 1 & 2 \\
\end{tabular}


\end{document}

alt text

If you want to connect certain cells within the table with arrows and so an, it might be easier to use tikz. Here is a somehow evil hack to get a slashbox in a matrix of nodes. I am sure this can be done much better by someone else

\documentclass{article}
\usepackage{slashbox}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

%\begin{tabular}{c|cc}
 %\backslashbox{n}{k} & 0 & 1\\\hline
%0 & 1 & 2 \\
%\end{tabular}

\begin{tikzpicture}
 \matrix (mymatrix) [matrix of nodes,
            nodes in empty cells,
%text depth=.5ex,
text height=1.5ex,
text width=1.5ex
            ]
{
\begin{scope} \tikz\node[overlay] at (-.8ex,-0.4ex){\footnotesize n};\tikz\node[overlay] at (1ex,0.5ex){\footnotesize k}; \end{scope} & 2 & 3 & 8\\\hline
4 & |[fill=blue!10]|5 & 6 & 4\\
7 & 8 & 9 & 0\\
10 & 11 & 12 & |[fill=blue!10]|1\\
};
%vertical and diagonal line
\draw (mymatrix-1-2.north west) -- (mymatrix-4-2.south west);
\draw (mymatrix-1-1.north west) -- (mymatrix-1-1.south east);%n k diagonal line

%connection between nodes
\draw[->] (mymatrix-2-2) to [bend right=25] (mymatrix-4-4.north west);

\end{tikzpicture}


\end{document}

alt text

Werner
  • 603,163
Martin H
  • 18,164
  • Thanks. I like the TikZ version better, although it probably does too much. On the other hand, we proved some formulae for computing parts of the table, and what you provide may come in handy if we decide to highlight those parts. – Anthony Labarre Dec 17 '10 at 11:52
  • I have added a comment in the code. basically I created a line from the north west corner of cell (1,1) to the south east corner of cell (1,1). This approach has a few disadvantages as it is sensitive to column with changes. That is why I was hoping someone has better solution – Martin H Dec 17 '10 at 11:58
  • I guess what confused me was your comment "vertical and horizonal lines"...should be "and diagonal" since the horizontal is created right after the scope – Yossi Farjoun Dec 17 '10 at 11:59
  • true indeed, i changes that – Martin H Dec 17 '10 at 12:45
  • It did not look so good anymore when I started modifying it. One suggestion I have is to replace the first \hline with a path, and then use \coordinate to build the diagonal that starts at the intersection of that path with the path that shows the first column. The diagonal will then "resist" changes. However, that replacement for \hline isn't so straight, but I don't know how to fix that. – Anthony Labarre Dec 17 '10 at 22:48
  • 16
    It seems there is no slashbox in TeXlive 2016 but there is an alternative package called diagbox. – Say OL Oct 17 '16 at 03:16
10
\input eplain
\vbox{\offinterlineskip
\halign{\tabskip1em
  $\phantom{\Big)}#$&\vrule#&&\hfil$#$\hfil\cr
  \sarrowlength=1.5em
  \hidewidth\matrix{\sline(1,-1)\lft{n}\rt{k}}\hidewidth
    && 0 & 1 & 2 & 3 & 4 & 5 & 6 & \cdots \cr
  \noalign{\hrule}
  0 && 0 & 1 & 0 & 0 & 0 & 0 & 0 & \vdots \cr
  1 && 1 & 0 & 0 & 0 & 0 & 0 & 0 & \cr
  2 && 1 & 2 & 1 & 0 & 0 & 0 & 0 \cr
  3 && 1 & 3 & 3 & 1 & 0 & 0 & 0 \cr
  4 && 1 & 4 & 6 & 4 & 1 & 0 & 0 \cr
  5 && 1 & 5 & 10 & 10 & 5 & 1 & 0 \cr
  \vdots && \cdots \cr
}}\bye


It could use some better spacing, but the idea is there.

doncherry
  • 54,637
morbusg
  • 25,490
  • 4
  • 81
  • 162
  • @morbusg: I'm not the downvoter, but I do think that your post doesn't answer the question: The OP explicitely asked for a table. – Hendrik Vogt Dec 20 '10 at 10:59
  • @Hendrik Vogt: under the hood, it is a table ;-) – morbusg Dec 20 '10 at 11:06
  • @morbusg: Well, I see you mean this as a joke, but I still don't see how your answer can be of help to the OP. – Hendrik Vogt Dec 20 '10 at 11:08
  • @Hendrik Vogt: It's not a joke in the sense that bordermatrix expands to "initialized halign" (as I'm sure you know). However, what I meant is that: "If your data can be thought of as a matrix (which seemed to me to possibly be the case), here's a solution", without knowing if that is the case. Sometimes it may be beneficial to suggest other ways to look at your data. I'm not necessarily saying it is beneficial here. – morbusg Dec 20 '10 at 11:26
  • @morbusg: It was clear to me that part of your joke was serious and correct. And clearly, a bordermatrix is the correct data structure for the OP's data. However, he want's to display his data in a table! (And also I think that this is a good idea.) – Hendrik Vogt Dec 20 '10 at 11:30
  • @morbusg: Looks much more like an answer now. The main problem I still see is the inflexible backslash that doesn't look great. – Hendrik Vogt Dec 20 '10 at 15:12
  • 2
    I give up, Mr.~@Vogt: a honest table it is. – morbusg Dec 20 '10 at 17:51
  • @morbusg: +1; the diagonal line is still not optimal, but this looks very much like an answer to the question now. – Hendrik Vogt Dec 20 '10 at 18:12
  • @morbusg +1: table, indeed. – Przemysław Scherwentke Dec 07 '13 at 01:14