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
  • I've closed this as a duplicate of a newer questions: the linked one has a better overall answer, I feel. (Tikz gives an exact solution, which slashbox does not.)
    – Joseph Wright
    Sep 1, 2011 at 19:33
  • @JosephWright Would it make sense to merge these questions?
    – doncherry
    Apr 25, 2013 at 4:26

2 Answers 2

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

6
  • 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. Dec 17, 2010 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, 2010 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 Dec 17, 2010 at 11:59
  • true indeed, i changes that
    – Martin H
    Dec 17, 2010 at 12:45
  • 15
    It seems there is no slashbox in TeXlive 2016 but there is an alternative package called diagbox.
    – Say OL
    Oct 17, 2016 at 3: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.

9
  • @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. Dec 20, 2010 at 10:59
  • @Hendrik Vogt: under the hood, it is a table ;-)
    – morbusg
    Dec 20, 2010 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. Dec 20, 2010 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, 2010 at 11:26
  • 2
    I give up, Mr.~@Vogt: a honest table it is.
    – morbusg
    Dec 20, 2010 at 17:51

Not the answer you're looking for? Browse other questions tagged .