14

I have a grid/table/matrix with separate labels for the rows and the columns. However, I also want to label these labels themselves (by indicating the "type" of the labels, such as SI units, etc.)

Here is a MWE, with three variants (check the entry abc/def in the upper left position, describing the row labels abc and the column labels def):

\documentclass{article}
\begin{document}
\begin{table}
\caption{Forward slash.}
\[\begin{array}{c|ccccc}
abc/def & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}

\begin{table}
\caption{Vertical bar.}
\[\begin{array}{c|ccccc}
abc|def & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}

\begin{table}
\caption{Backslash.}
\[\begin{array}{c|ccccc}
abc\backslash def & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}

\end{document}

enter image description here

However, this MWE suffers from (at least) the following two problems: the first column (labelling the rows) is too wide, and the centered labels are far from the core of the array itself (right-aligning could help, but... that introduces other problems). Second, the spacing around abc/def looks incorrect.

The questions is, basically, how should I typeset this array nicely? What should I put to the upper left position, in place of abc/def? I do not wish to put abc and def into two distinct (multi)columns. Maybe stacking abc above (or under?) def is a start, but then I would still need some kind of visual separator (taking the role of the \|/ symbols) within that cell. I should probably also clarify that both labels abc and def are in practice very short, typically I would write x/y or n/m or n/k, so single-character mathematical variables, and not lengthy texts.


Related: How to create table with labels (first row and first column)

1

6 Answers 6

10

I'm going to suggest "none of the three possibilities". Instead, consider using a classic "tableau" setup, with a clear hierarchical structure in the header. Such a setup helps avoid creating the "cramped" look that's almost inevitable with any one of the three possibilities you've offered.

enter image description here

\documentclass{article}
\usepackage{booktabs}% for \toprule, \midrule, \bottomrule, and \cmidrule macros
\usepackage{amsmath} % for \text macro
\begin{document}
\begin{table}
\caption{Still another approach}
\[
\begin{array}{@{}l*{5}{c}@{}}
\toprule
\text{abc} & \multicolumn{5}{c@{}}{\text{def}}\\
    \cmidrule(l){2-6}
    & 1 & 2 & 3 & 4 & 5\\
\midrule
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\bottomrule
\end{array}
\]
\end{table}
\end{document}
3
  • Thank you for your quick answer. I personally would probably center abc vertically to reduce the large white ocean right below it, but others might disagree with this. I also know that using vertical lines in table in general discouraged, and this proposal avoids them nicely.
    – Matsmath
    Nov 16, 2016 at 22:06
  • 2
    @Matsmath - You're welcome. The idea behind placing the "abc" and "def" labels on the same row is to indicate that they're hierarchically equal. The material in the second header row is then easily seen as (a) being secondary and (b) belonging solely to "def".
    – Mico
    Nov 16, 2016 at 22:13
  • To whoever downvoted this answer (which I wrote more than 2 years ago) earlier today: Care to express what my answer provoked your displeasure? Don't be shy.
    – Mico
    Dec 3, 2018 at 8:17
11

Here are two options:

enter image description here

\documentclass{article}

\usepackage{mathtools,eqparbox}

\newcommand{\indices}[2]{{% \indices{<rows>}{<columns>}
  \begin{array}{@{}r@{}}
    \scriptstyle #2~\smash{\eqmakebox[ind]{$\scriptstyle\rightarrow$}} \\[-\jot]  
    \scriptstyle #1~\smash{\eqmakebox[ind]{$\scriptstyle\downarrow$}}
  \end{array}}}

\begin{document}

\[
  \begin{array}{c|ccccc}
    \indices{\text{abc}}{\text{def}}
    & 1 & 2 & 3 & 4 & 5 \\
    \hline
    1 & a & b & c & d & e \\
    2 & f & g & h & i & j \\
    3 & k & l & m & n & o
  \end{array}
\]

\[
  \begin{array}{cc|ccccc}
    &\multicolumn{1}{c}{} & \multicolumn{5}{c}{\text{def}} \\
    && 1 & 2 & 3 & 4 & 5 \\
    \cline{2-7}
    & 1 & a & b & c & d & e \\
    \smash{\rotatebox[origin=c]{90}{\text{abc}}} & 2 & f & g & h & i & j \\
    & 3 & k & l & m & n & o
  \end{array}
\]

\end{document}

If you're interested in a matrix-like command, there are some examples at Where is the \matrix command?. This includes using \bordermatrix, kbordermatrix and blkarray, all of which allows you to place indices to identify the rows/columns.

0
11

It is also possible to use TikZ directly inside the tabular, needing no savebox at all to get the lenghts:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{table}
\caption{Diagonal Split}
\[\begin{array}{c|ccccc}
\tikz{\node[below left, inner sep=1pt] (def) {def};%
      \node[above right,inner sep=1pt] (abc) {abc};%
      \draw (def.north west|-abc.north west) -- (def.south east-|abc.south east);}
 & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}
\end{document}

enter image description here

Then, inside the \tikz command you have the TikZ power to do whatever... Here is a version with repositioned nodes, a very thin shortened division line and fonts in \footnotesize.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{table}
\caption{Diagonal Split}
\[\begin{array}{c|ccccc}
\tikz[diag text/.style={inner sep=0pt, font=\footnotesize},
      shorten/.style={shorten <=#1,shorten >=#1}]{%
        \node[below left, diag text] (def) {def};
        \node[above right=2pt and -2pt, diag text] (abc) {abc};
        \draw[shorten=4pt, very thin] (def.north west|-abc.north west) -- (def.south east-|abc.south east);}
 & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}
\end{document}

enter image description here

0
9

A tabular is used to create a minimal box containing the two labels. Putting it into a savebox gives you the dimensions. Finally, TikZ is used to draw a diagonal line from the upper left to lower right.

\documentclass{article}
\usepackage{tikz}
\newsavebox{\tempbox}
\begin{document}

\savebox{\tempbox}{\begin{tabular}{@{}r@{}l@{\space}}
&abc\\def
\end{tabular}}

\begin{table}
\caption{Diagonal Split}
\[\begin{array}{c|ccccc}
\tikz[overlay]{\draw (0pt,\ht\tempbox) -- (\wd\tempbox,-\dp\tempbox);}%
\usebox{\tempbox}\hspace{\dimexpr 1pt-\tabcolsep}
 & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}
\end{document}

demo

3
  • 1
    Do I lose antialiasing with this method?
    – Matsmath
    Nov 24, 2016 at 4:08
  • 1
    If you go straight to PDF (pdflatex), no. Otherwise, it depends on resolution of the medium. Nov 24, 2016 at 4:13
  • You could use diagbox package for this kind of cell.
    – Leo Liu
    Nov 28, 2016 at 11:35
8
+50

Sometimes I use diagonal lines or black boxes:

\documentclass{article}
\usepackage{fp}
\usepackage{graphicx}

\newbox\MytempboxA
\newbox\MytempboxB
\newcommand\myTempA{}
\newcommand\myTempB{}
\newcommand\myTempC{}
\newcommand\myTempD{}

\begin{document}

\begin{table}%
\caption{Diagonal line approach}%
\[%
\setbox\MytempboxA\hbox{\mbox{abc}}%
\setbox\MytempboxB\hbox{\mbox{def}}%
\begin{array}{@{}|r|*{5}{c}|}
\hline
\multicolumn{1}{|l}{%
  \edef\myTempA{%
    \number\numexpr\dimexpr\wd\MytempboxA+2\arraycolsep\relax\relax
  }%
  \edef\myTempB{%
    \number
    \numexpr
      \dimexpr\dp\csname @arstrutbox\endcsname+%
              \ht\csname @arstrutbox\endcsname+%
              \arrayrulewidth
      \relax
    \relax
  }%
  \FPpow\myTempC\myTempA{2}%
  \FPpow\myTempD\myTempB{2}%
  \FPadd\myTempC\myTempC\myTempD
  \FProot\myTempC\myTempC{2}% length of diagonal line in sp
  \FPdiv\myTempD\myTempB\myTempA
  \FParctan\myTempD\myTempD% angle of diagonal line in rad
  \smash{%
    \kern-\arraycolsep
    \rlap{%
      \lower
        \dimexpr
          \dp\csname @arstrutbox\endcsname+\arrayrulewidth
        \relax
        \hbox{%
          \rotatebox[units=-6.283185,origin=br]{\myTempD}{%
            \rule{\myTempC sp}{\arrayrulewidth}%
          }%
        }%
    }%
  }%
}&\multicolumn{5}{c|}{\copy\MytempboxB}\\%
\cline{2-6}%
\copy\MytempboxA& 1 & 2 & 3 & 4 & 5\\%
\hline
1 & a & b & c & d & e\\%
2 & f & g & h & i & j\\%
3 & k & l & m & n & o\\%
\hline
\end{array}%
\]%
\end{table}

\begin{table}
\caption{Black box approach}%
\setbox\MytempboxA\hbox{\mbox{abc}}%
\setbox\MytempboxB\hbox{\mbox{def}}%
\[%
\begin{array}{@{}|r|*{5}{c}|}%
\hline
\multicolumn{1}{|l|}{%
  \smash{%
    \kern-\arraycolsep
    \rlap{%
      \rule[{-\dp\csname @arstrutbox\endcsname}]%
           {\dimexpr\wd\MytempboxA+2\arraycolsep\relax}%
           {%
             \dimexpr
               \dp\csname @arstrutbox\endcsname+%
               \ht\csname @arstrutbox\endcsname
             \relax
           }%
    }%
  }%
}&\multicolumn{5}{c|}{\copy\MytempboxB}\\
\hline
\copy\MytempboxA& 1 & 2 & 3 & 4 & 5\\%
\hline
1 & a & b & c & d & e\\%
2 & f & g & h & i & j\\%
3 & k & l & m & n & o\\%
\hline
\end{array}%
\]%
\end{table}


\begin{table}%
\caption{Tangram puzzle approach}%
\[%
\setbox\MytempboxA\hbox{\mbox{abc}}%
\setbox\MytempboxB\hbox{\mbox{def}}%
\begin{array}{@{}|r|*{5}{c}|}
\hline
\multicolumn{1}{|l}{%
  \edef\myTempA{%
    \number\numexpr\dimexpr\wd\MytempboxA+2\arraycolsep\relax\relax
  }%
  \edef\myTempB{%
    \number
    \numexpr
      \dimexpr\dp\csname @arstrutbox\endcsname+%
              \ht\csname @arstrutbox\endcsname+%
              \arrayrulewidth
      \relax
    \relax
  }%
  \FPpow\myTempC\myTempA{2}%
  \FPpow\myTempD\myTempB{2}%
  \FPadd\myTempC\myTempC\myTempD
  \FProot\myTempC\myTempC{2}% length of diagonal line in sp
  \FPdiv\myTempD\myTempB\myTempA
  \FParctan\myTempD\myTempD% angle of diagonal line in rad
  \smash{%
    \kern-\arraycolsep
    \rlap{%
      \lower
        \dimexpr
          \dp\csname @arstrutbox\endcsname+\arrayrulewidth
        \relax
        \hbox{%
          \rotatebox[units=-6.283185,origin=br]{\myTempD}{%
            \rule{\myTempC sp}{\arrayrulewidth}%
          }%
        }%
    }%
  }%
}&\multicolumn{5}{c|}{\copy\MytempboxB}\\%
\copy\MytempboxA& 1 & 2 & 3 & 4 & 5\\%
\cline{2-6}%
1 & a & b & c & d & e\\%
2 & f & g & h & i & j\\%
3 & k & l & m & n & o\\%
\hline
\end{array}%
\]%
\end{table}

\begin{table}
\caption{squares approach}%
\setbox\MytempboxA\hbox{\mbox{abc}}%
\setbox\MytempboxB\hbox{\mbox{def}}%
\[%
\begin{array}{@{}|r|*{5}{c}|}%
\hline
\multicolumn{1}{|l|}{}&\multicolumn{5}{c|}{\copy\MytempboxB}\\
\copy\MytempboxA& 1 & 2 & 3 & 4 & 5\\%
\cline{2-6}%
1 & a & b & c & d & e\\%
2 & f & g & h & i & j\\%
3 & k & l & m & n & o\\%
\hline
\end{array}%
\]%
\end{table}

\end{document}

enter image description here

enter image description here

enter image description here

enter image description here

And sometimes I use something like angle arrows:

\documentclass{article}

\usepackage{tikz}

\newbox\MytempboxA
\newbox\MytempboxB
\newbox\MytempboxC
\newcommand\Upbox[1]{%
  \lower\dimexpr-\ht\MytempboxA+\ht\MytempboxB\relax\hbox{#1}%
  %#1%
}%
\newcommand\leftbox[1]{%
  \hbox to\wd\MytempboxC{\hss#1\hss}\hbox to\wd\MytempboxB{\hfill}%
}%
\begin{document}

\begin{table}%
\caption{Angle arrow.}%
\setbox\MytempboxB=\hbox{\mbox{def}}%
\setbox\MytempboxC=\hbox{\mbox{abc}}%
\setbox\MytempboxA=\hbox{\lower\dp\MytempboxC\vbox{%
  \hbox{%
    \begin{tikzpicture}[x=.25cm, y=.25cm, inner sep=0pt]
    \draw[->,thin] (0,0) -- (1,0) node[right]{\copy\MytempboxB};
    \draw[->,thin] (0,0) -- (0,-1) node[below]{\copy\MytempboxC};
    \end{tikzpicture}%
  }%
}}%
\[%
  \begin{array}{c|ccccc}%
  \copy\MytempboxA&\Upbox{1}&\Upbox{2}&\Upbox{3}&\Upbox{4}&\Upbox{5}\\%
  \hline
  \leftbox{1}& a & b & c & d & e\\%
  \leftbox{2} & f & g & h & i & j\\%
  \leftbox{3} & k & l & m & n & o\\%
  \end{array}%
\]
\end{table}


\begin{table}
\caption{Another angle arrow.}%
\setbox\MytempboxB=\hbox{\mbox{def}}%
\setbox\MytempboxC=\hbox{\mbox{abc}}%
\[%
  \begin{array}{c@{}c|ccccc}%
  \smash{%
    \hbox{%
      \kern.5\wd\MytempboxC
      \lower.75\ht\MytempboxB
      \hbox{%
        \begin{tikzpicture}[x=.25cm, y=.25cm, inner sep=0pt]
        \draw[->,thin] (0,0) -- (1,0) node[right]{};
        \draw[->,thin] (0,0) -- (0,-1) node[below]{};
        \end{tikzpicture}%
      }%
    }%
  }&\copy\MytempboxB&1&2&3&4&5\\%
  \copy\MytempboxC&&\\%
  \hline
  1&& a & b & c & d & e\\%
  2&& f & g & h & i & j\\%
  3&& k & l & m & n & o\\%
  \end{array}%
\]%
\end{table}

\end{document}

enter image description here

enter image description here

You can also create the look and feel of coordinate system axes with horizontal and vertical lines of an array:

\documentclass{article}

\usepackage{tikz}

\newbox\MytempboxA
\newbox\MytempboxB
\newbox\MytempboxC

\begin{document}

\begin{table}%
\caption{Coordinate axes approach.}%
\setbox\MytempboxC=\hbox{\mbox{abc}}%
\setbox\MytempboxB=\hbox{\mbox{def}}%
\setbox\MytempboxA\hbox{%
  \lower\arrayrulewidth\hbox{%
  \begin{tikzpicture}[x=.25cm, y=.05cm, inner sep=0pt]%
  \draw[->,line width=\arrayrulewidth] (0,1) -- (1,1) node[right]{};
  \end{tikzpicture}%
  }%
}%
\[%
  \begin{array}{c|cccccl}%
  &1&2&3&4&5&\\%
  \cline{1-6}%
  1& a & b & c & d & e&%
  \kern\dimexpr-\arraycolsep-.5\arrayrulewidth\relax\null
  \smash{%
      \lower\dimexpr-\ht\csname @arstrutbox\endcsname+.75\arrayrulewidth+0pt\relax\copy\MytempboxA
      \lower\dimexpr-\ht\csname @arstrutbox\endcsname+0.5ex+.75\arrayrulewidth\relax\copy\MytempboxB
  }%
  \\%
  2 & f & g & h & i & j&\\%
  3 & k & l & m & n & o&\\%
  \multicolumn{1}{r}{}&%
  \multicolumn{6}{l}{%
    \kern\dimexpr-.5\wd\MytempboxC-\arraycolsep\relax\null
    \vbox{%
      \kern\dimexpr-.5\arrayrulewidth\relax\hbox{%
        \begin{tikzpicture}[x=.25cm, y=.075cm, inner sep=0pt]%
        \draw[->,line width=\arrayrulewidth] (0,1) -- (0,-1) node[below]{\copy\MytempboxC};
        \end{tikzpicture}%
      }%
    }%
  }%
  \\%
  \end{array}%
\]
\end{table}

\end{document}

enter image description here

0
1

Many excellent answers has already been proposed. Actually, I ended up using a subscript and superscript based solution. The negative spacing required some fine-tuning.

The advantage of this approach is that it does not rely on external packages (and hence journal-friendly). The disadvantage is that two different font sizes are used, which is, perhaps, typographically discouraged.

\documentclass{article}
\begin{document}
\begin{table}
\caption{Scripts.}
\[\begin{array}{c|ccccc}
{}_{abc}\mkern-6mu\setminus\mkern-6mu{}^{def} & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}
\end{document}

enter image description here

You must log in to answer this question.

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