If you are not particular about using the latex
backend, you can go with the odt
backend, specifically the *enhanced* ODT backend
. Since the question is more about table grid lines than the math, I have removed the math snippets.
You need to use the enhanced ODT exporter ox-odt-9.5.3.467.tar
or later (or simply, any version whose date is later than June 14, 2022 IST).
The enhanced ODT backend is not available with standard Emacs or Org mode, and you need to install it separately. See Getting Started with ODT export (OpenDocument Text Exporter for Emacs’ Org Mode for instructions.
To get this

do this
#+odt_preferred_output_format: png
#+options: toc:nil
#+title: How to get custom rules in a ODT Table
To create this table
#+NAME: table-with-complex-cell-borders
#+CAPTION: A table with complex grid layout
#+ATTR_ODT: :rel-width 50
#+ATTR_ODT: :col-cookies "| c | c | c |"
#+ATTR_ODT: :suppress-spans t
#+ATTR_ODT: :span "@2$1{2:1} @2$2{2:1} @2$3{4:1}"
#+ATTR_ODT: :span "@4$1{2:1} @4$2{2:1}"
|-----+-----+-------------------|
| /p/ | /q/ | /p \rightarrow q/ |
|-----+-----+-------------------|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
|-----+-----+-------------------|
| 1 | 0 | 0 |
| 1 | 1 | 1 |
|-----+-----+-------------------|
do this
#+NAME: raw-org-table
#+CAPTION: Org version of the preceding table\\
#+CAPTION: Pay attention to =ATTR_ODT= lines
#+ATTR_ODT: :category "table"
#+begin_src org
#+ATTR_ODT: :rel-width 50
#+ATTR_ODT: :col-cookies "| c | c | c |"
#+ATTR_ODT: :suppress-spans t
#+ATTR_ODT: :span "@2$1{2:1} @2$2{2:1} @2$3{4:1}"
#+ATTR_ODT: :span "@4$1{2:1} @4$2{2:1}"
|-----+-----+-------------------|
| /p/ | /q/ | /p \rightarrow q/ |
|-----+-----+-------------------|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
|-----+-----+-------------------|
| 1 | 0 | 0 |
| 1 | 1 | 1 |
|-----+-----+-------------------|
#+end_src
* Explanation
=:span= attribute specifies a set of rectangles using
=@R$C{ROWSPAN:COLSPAN}= format. When =:span= attribute is used with
=:suppress-spans t= attribute, you specify that you want to control
the table borders.
For example, in the Table [[raw-org-table]] above,
- the entry =@2$1{2:1}= says that the rectangle spanning =Row 2,
Column 1= to =Row 3, Column 1=, a =2x1= rectangle has no /inner/
borders, only /surrounding/ borders.
- the entry =@2$3{4:1}= says the rectangle spanning from =Row 2,
Column 3= to =Row 5, Column 3=, a =4x1= rectangle will have no
/inner borders/ but only /surrounding/ borders.
For ease of understanding, match the above description with what you
see in Table [[table-with-complex-cell-borders]].
#+ATTR_ODT: :target "extra_styles"
#+begin_src nxml :noweb yes :exports code :eval never
<style:style style:name="OrgCode"
style:family="text">
<style:text-properties fo:background-color="#ffff00" fo:color="#000000"/>
</style:style>
#+end_src
Explanation
Pay attention to the ATTR_ODT
lines which specify :span
attributes
and :suppress-spans
attribute.
When :span
attribute is used with :suppress-spans t
attribute, you
specify that you want to control the table borders.
:span
attribute contains a set of rectangles, and each rectangle is
designated in @R$C{ROWSPAN:COLSPAN}
format.
For example, a :span
entry of @2$1{2:1}
says that the rectangle
spanning Row 2, Column 1
to Row 3, Column 1
, a 2x1
rectangle,
has no inner borders, but only surrounding borders.
Rectangles / cells that are not in a :span
-set will get all the 4 borders-- top, right, bottom and left. In a sense, the :span
cells marks "bigger" rectangles, that are "exeptions".
(Note that the table grid lines in the example above are controlled by ATTR_ODT
lines and not by the hline
s and vline
s in the Org table. In other words, even if you strip (or mangle) the hline
s and vline
s in above org
table, you would still get the same grid layout)