7

I would like to make a table in LaTeX, where I box some of the cells within my table. And I would like to be able to nest boxes so that I can have a box in a box. Here I have an example of what I mean:

enter image description here

I have searched for this problem for some time, and I doesn't seem like I am capable of finding a solution to this problem, and I don't know how to attack the problem, since I'm pretty new to LaTeX.

Karlo
  • 3,257
Talouv
  • 193

2 Answers2

5

There are perhaps many ways of doing this, depending on how you may want to extend this. I've used an array since the arrangement in your question looks pretty tabular-like:

enter image description here

\documentclass{article}
\begin{document}
\[\begin{array}{l|cclc|l}
  \multicolumn{1}{l}{1.} & \quad & & \forall x\forall y : P(x,y) & \multicolumn{1}{c}{\quad} & \mbox{premise} \\ \cline{2-5}
  2. & & u_0 & \forall y : P(u_0,y) && \forall e \\ \cline{3-4}
  3. & & \multicolumn{1}{|c}{v_0} & P(u_0,v_0) & \multicolumn{1}{|c|}{} & \forall e \\ \cline{3-4}
  4. & & & \forall v : P(u_0,v) && \forall i \\ \cline{2-5}
  \multicolumn{1}{l}{5.} & & & \forall u\forall v : P(u,v) &\multicolumn{1}{c}{} & \forall i
\end{array}\]
\end{document}​

Columns 2 and 5 are just place-holders, specified as centred; they only contain the entry \quad for spacing purposes.


tabular creation in LaTeX is tedious for large constructions. There are a couple of GUIs that could be of help:

  1. excel2latex

    enter image description here

    The default is to include \bigstruts, which requires the bigstrut package. Also, you may want to modify the default tabular output to array, and wrap it inside a display math environment \[...\].

  2. Gnumeric which allows exporting spreadsheet-like constructions to LaTeX.

  3. ...see more at Comprehensive list of tools that simplify the generation of LaTeX tables.

Werner
  • 603,163
  • Thanks.. But this is going to explode, if you want more nesting, isn't it? If I want eg. 10 nested boxes, I will need about 20 placeholders, won't I? Is there a "better" way, so that I don't need empty cells? – Talouv Oct 03 '12 at 13:24
  • @user1712876: I've added some tabular creation aids. – Werner Oct 06 '12 at 04:01
  • 3
    @Werner There is calc2latex for OpenOffice/LibreOffice. – Qrrbrbirlbel Oct 06 '12 at 04:09
  • @Werner Isn't \text{} more appropriate than \mbox{}? – azetina Oct 07 '12 at 02:34
  • @azetina: In a mathematical context where you may switch between different styles (\displaystyle, \textstyle, \scriptstyle, ...), yes. However, in this instance, not so much. – Werner Oct 07 '12 at 02:42
  • @Werner Ohhhhhhhh ok. I stick to one so yeah. Thanks for the clarification. – azetina Oct 07 '12 at 02:48
  • Thanks guys.. I'll look into that calc2latex in the future.. I looks like it can realy help with large tables.. – Talouv Oct 08 '12 at 16:04
2

Here is a way to do that with {NiceTabular} of nicematrix. This environment is similar to the classical {tabular} (of array) but adds PGF/Tikz nodes under the cells, rows and columns. It's possible to use those nodes to draw whatever you want after the construction of the array with Tikz in the \CodeAfter.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

[\begin{NiceArray}{lcclcl}

  1. & \quad & & \forall x\forall y : P(x,y) & \quad & \mbox{premise} \
  2. & & u_0 & \forall y : P(u_0,y) && \forall e \
  3. & & v_0 & P(u_0,v_0) & & \forall e \
  4. & & & \forall v : P(u_0,v) && \forall i \
  5. & & & \forall u\forall v : P(u,v) & & \forall i

\CodeAfter \tikz \draw (2-|2) rectangle (5-|6) (3-|3) rectangle (4-|5) ; \end{NiceArray}]

\end{document}​

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

Output of the first code

In fact, in your case, it's also possible to use the built-in command \Block with the key draw in the cells which correspond to the upper-left corners. You don't have to use Tikz explicitely.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

[\begin{NiceArray}{lcclcl}

  1. & \quad & & \forall x\forall y : P(x,y) & \quad & \mbox{premise} \
  2. & \Block[draw]{3-4}{} & u_0 & \forall y : P(u_0,y) && \forall e \
  3. & & \Block[draw]{1-2}{} v_0 & P(u_0,v_0) & & \forall e \
  4. & & & \forall v : P(u_0,v) && \forall i \
  5. & & & \forall u\forall v : P(u,v) & & \forall i

\end{NiceArray}]

\end{document}​

The output is the same.

F. Pantigny
  • 40,250