Merge cells in org-mode tables

EmacsCellOrg Mode

Emacs Problem Overview


Is it possible to merge cells in Org-mode tables?

Examples of horizontal merging would be something like that:

| Header | Merged header |
|--------+-------+-------|
| Text1  | Text2 | Text3 |
| Text4  | Text5 | Text6 |

Example of vertical merging:

| Header1 | Header2 | Header3 |
|---------+---------+---------|
| Merged  | Text1   | Text2   |
| text    |---------+---------|
| here    | Text3   | Text4   |

If that is somehow possible in org-mode? If not, what is the most idiomatic way to simulate merged cells?

Emacs Solutions


Solution 1 - Emacs

It is not possible with org-mode tables. However, have a look at table.el package (included with emacs for some time so try: C-h d table). Org-mode has some support for tables from this library, e.g. when exporting, but don't expect full compatibility.

As for simulating merged cell, it depends on what you want. Inlining text strings in the same cell might be enough for computation/publication, but not for visualisation.

Solution 2 - Emacs

I just found an example from emacs-orgmode mail list which works just fine for me.

+---+-----+-----------+
|   | A   | B         |
+---+-----+-----+-----+
| 1 | A1  | B1  | C1  |
|   |     +-----+-----+
|   | A1b | B1b | C1b |
+---+-----+-----+-----+
| 2 | A2  |   B2 C2   |
|   +-----+           |
|   | A2b |  B2b C2b  |
+---+-----+-----------+

If you modify your table like below, it works too.

+---------+---------+---------+
| Header1 | Header2 | Header3 |
+---------+---------+---------+
| Merged  | Text1   | Text2   |
| text    +---------+---------+
| here    | Text3   | Text4   |
+---------+---------+---------+

So I find some tips on that:

  • Use | to expand row

  • Use +-..-+ to surround the split row


Here is another alternative choice which is not very convenient. You can use Quoting HTML tags to handle HTML table export.

#+BEGIN_EXPORT html
<TABLE BORDER="1">
  <TR>
    <TH>Header1</TH>
    <TH>Header2</TH>
    <TH>Header3</TH>
  </TR>
  <TR>
    <TD ROWSPAN="2">Merged text here</TD>
    <TD>Text1</TD>
    <TD>Text2</TD>
  </TR>
  <TR>
    <TD>Text3</TD>
    <TD>Text4</TD>
  </TR>
</TABLE>
#+END_EXPORT

This synatx works only when exported to html. If you want export table to pdf, you should use syntax below. You can use LaTeX Tables Generator to generate latex table online.

#+BEGIN_EXPORT latex
% Please add the following required packages to your document preamble:
% \usepackage{multirow}
\begin{table}[]
\begin{tabular}{lll}
Header1                           & Header2 & Header3 \\
\multirow{2}{*}{Merged text here} & Text1   & Text2   \\
                                  & Text3   & Text4  
\end{tabular}
\end{table}
#+END_EXPORT

There exsist many back-ends in in org export, you possiblely need to write customzation code to those backends if you want to export to those backends.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMirzhan IrkegulovView Question on Stackoverflow
Solution 1 - EmacsbzgView Answer on Stackoverflow
Solution 2 - EmacsYnjxsjmhView Answer on Stackoverflow