4

I have a table and I would like to know how can I write specific text outside the table, all around the table.

A simple example is this:

Above the table: a b c

RHS of the table: d e f

LHS of the table: g h i

Below the table: j k l

In a way that those letters are aligned to the rows and columns respectively.

\begin{document}

\begin{table}[H]
    \centering
    \begin{tabular}{|c|c|c|}
\hline
        a & b & c \\
        \hline
        d & e & f \\
        \hline
        h & i & j \\
        \hline
    \end{tabular}

\end{table}

\end{document}

Could someone tell me how can I do this?

Thanks in advance

4 Answers4

7

Maybe not the most elegant, but works:

\documentclass{article}
\usepackage{float}

\begin{document}

\begin{table}[H]
    \centering
    \newcommand{\myline}{\cline{2-4}}
    \newcommand{\noline}[1]{\multicolumn{1}{c}{#1}}
    \begin{tabular}{c|c|c|c|c}
        \noline{} & \noline{a} & \noline{b} & \noline{c} & \\
        \myline
        g & a & b & c & d \\
        \myline
        h & d & e & f & e \\
        \myline
        i & h & i & j & f \\
        \myline
        \noline{} & \noline{j} & \noline{k} & \noline{l} &
    \end{tabular}
\end{table}

\end{document}

enter image description here

Some comments:

  • I added a column each left and right without a vertical line | in front/after. So these columns are outside of the "outer frame" of the table.
  • I didn't start or end with a \hline so that the first and last row are outside of the "outer frame" of the table.
  • I use \cline{2-4} instead of \hline to have the vertical lines not extend to the two outermost columns.
  • I use \multicolumn{1}{c} to remove the vertical lines around the cells in the first and last row.
Tiuri
  • 7,749
6

Just for fun a version using a tikz matrix:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix(table)[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center,text height=2ex,text depth=0.25ex, minimum width=1cm, fill=none, draw=black},
  column 1/.style = {nodes={draw=none}},
  column 5/.style = {nodes={draw=none}},
  row 1/.style={nodes={draw=none}},
  row 5/.style={nodes={draw=none}},
  ] 
  {  & a & b & c &   \\
   g & a & b & c & d \\
   h & d & e & f & e \\
   i & h & i & j & f \\
     & j & k & l &  \\
  };
\end{tikzpicture}

\end{document}
leandriis
  • 62,593
1

With {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{ccc}[first-row,last-row,first-col,last-col,hvlines] & a & b & c \ g & a & b & c & d \ h & d & e & f & e \ i & h & i & j & f \ & j & k & l \ \end{NiceTabular}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250
1

With tabularray:

\documentclass{article}
\usepackage{tabularray}

\begin{document}

\section{Manually}

\begin{tblr}{ hline{2-6} = {2-4}{solid}, vline{2-5} = {2-5}{solid}, } & 1 & 2 & 3 & \ a & X & Y & Z & b\ c & U & S & T & d\ e & N & M & O & f\ g & J & K & L & h\ & i & ii & iii & \ \end{tblr}

\section{Automated}

\begin{tblr}{ hline{2-\value{rowcount}} = {2-\numexpr\value{colcount}-1}{solid}, vline{2-\value{colcount}} = {2-\numexpr\value{rowcount}-1}{solid}, } & 1 & 2 & 3 & \ a & X & Y & Z & b\ c & U & S & T & d\ e & N & M & O & f\ g & J & K & L & h\ & i & ii & iii & \ \end{tblr}

\end{document}

benjamin
  • 583
  • You can write hline{2-Y} = {2-Y}{solid} and vline{2-Y} = {2-Y}{solid} with the latest source code of tabularray (will be released as version 2021M scheduled on 1st August). X, Y, Z always stand for the last three childs, respectively. – L.J.R. Jul 23 '21 at 12:05