4

I have some arrays in math mode, such as the one below. I would like a diagonal line in a cell, something like diagbox (diagobx package) or diaghead (makecell package). But it seems that diagbox and diaghead only work in a tabular environment. Of course a way to go consists in using tabular instead of array but this requires to put some $'s in each cell. I'm looking for a less tedious solution.

\documentclass{article}

\usepackage{diagbox}

\begin{document}

$$
\begin{array}{r|cccc}
%\diagbox{x}{y} 
& (0,0) & (0,1) & (1,0) & (1,1) \\ 
  \hline
(0,0) & \phi^{-1}  &  \frac{f_n}{f_{n-1}} - \phi^{-1} & 0  & \frac{f_{n+1}}{f_{n-1}} \\ 
(0,1) & f_{n}/f_{n-1} & 0 & f_{n+1}/f_{n-1} & 0 \\ 
(1,0) & \phi^{-1}  & \phi^{-2}  & 0 & 0 \\ 
(1,1) & 1 & 0 & 0 & 0 \\ 
\end{array} 
$$

\end{document}
1
  • Doesn't M. Al Jumaily's answer sufficient? – user108724 Apr 14 '20 at 13:40
6
+50

Just encapsulate \diagbox{x}{y} inside an \hbox{}, i.e., \hbox{\diagbox{x}{y}}. Also, you can surround the text with $...$ to ensure the text is in mathmode as such: \hbox{\diagbox{$x$}{$y$}}. Off-topic, you should be using \[...\] instead of $$...$$ (have a look here).

Output

\documentclass{article}

\usepackage{diagbox}
\begin{document}
\noindent\ Without \$...\$
\[
    \begin{array}{r|cccc}
        \hbox{\diagbox{x}{y}} & (0,0) & (0,1) & (1,0) & (1,1) \\ \hline
        (0,0) & \phi^{-1}  &  \frac{f_n}{f_{n-1}} - \phi^{-1} & 0  & \frac{f_{n+1}}{f_{n-1}} \\ 
        (0,1) & f_{n}/f_{n-1} & 0 & f_{n+1}/f_{n-1} & 0 \\ 
        (1,0) & \phi^{-1}  & \phi^{-2}  & 0 & 0 \\ 
        (1,1) & 1 & 0 & 0 & 0 \\ 
    \end{array} 
\]
With \$...\$
\[
    \begin{array}{r|cccc}
        \hbox{\diagbox{$x$}{$y$}} & (0,0) & (0,1) & (1,0) & (1,1) \\ \hline
        (0,0) & \phi^{-1}  &  \frac{f_n}{f_{n-1}} - \phi^{-1} & 0  & \frac{f_{n+1}}{f_{n-1}} \\ 
        (0,1) & f_{n}/f_{n-1} & 0 & f_{n+1}/f_{n-1} & 0 \\ 
        (1,0) & \phi^{-1}  & \phi^{-2}  & 0 & 0 \\ 
        (1,1) & 1 & 0 & 0 & 0 \\ 
    \end{array} 
\]

\end{document}
0

A better solution: since diagbox always use \tabcolsep and \arraycolsep may have a different value, we need to do this:

\newcommand\mydiagbox[2]{\hbox{\tabcolsep=\arraycolsep\diagbox{$#1$}{$#2$}}}

Then we can use \mydiagbox{X}{Y} inside array environment.

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.