Multirow example

This example shows how multirow and multicolumns can be used.

The code

from pylatex import Document, MultiColumn, MultiRow, Section, Subsection, Tabular

doc = Document("multirow")
section = Section("Multirow Test")

test1 = Subsection("MultiColumn")
test2 = Subsection("MultiRow")
test3 = Subsection("MultiColumn and MultiRow")
test4 = Subsection("Vext01")

table1 = Tabular("|c|c|c|c|")
table1.add_hline()
table1.add_row((MultiColumn(4, align="|c|", data="Multicolumn"),))
table1.add_hline()
table1.add_row((1, 2, 3, 4))
table1.add_hline()
table1.add_row((5, 6, 7, 8))
table1.add_hline()
row_cells = ("9", MultiColumn(3, align="|c|", data="Multicolumn not on left"))
table1.add_row(row_cells)
table1.add_hline()

table2 = Tabular("|c|c|c|")
table2.add_hline()
table2.add_row((MultiRow(3, data="Multirow"), 1, 2))
table2.add_hline(2, 3)
table2.add_row(("", 3, 4))
table2.add_hline(2, 3)
table2.add_row(("", 5, 6))
table2.add_hline()
table2.add_row((MultiRow(3, data="Multirow2"), "", ""))
table2.add_empty_row()
table2.add_empty_row()
table2.add_hline()

table3 = Tabular("|c|c|c|")
table3.add_hline()
table3.add_row(
    (MultiColumn(2, align="|c|", data=MultiRow(2, data="multi-col-row")), "X")
)
table3.add_row((MultiColumn(2, align="|c|", data=""), "X"))
table3.add_hline()
table3.add_row(("X", "X", "X"))
table3.add_hline()

table4 = Tabular("|c|c|c|")
table4.add_hline()
col1_cell = MultiRow(4, data="span-4")
col2_cell = MultiRow(2, data="span-2")
table4.add_row((col1_cell, col2_cell, "3a"))
table4.add_hline(start=3)
table4.add_row(("", "", "3b"))
table4.add_hline(start=2)
table4.add_row(("", col2_cell, "3c"))
table4.add_hline(start=3)
table4.add_row(("", "", "3d"))
table4.add_hline()

test1.append(table1)
test2.append(table2)
test3.append(table3)
test4.append(table4)

section.append(test1)
section.append(test2)
section.append(test3)
section.append(test4)

doc.append(section)
doc.generate_pdf(clean_tex=False)

The generated files

multirow.tex
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{multirow}%
%
%
%
\begin{document}%
\normalsize%
\section{Multirow Test}%
\label{sec:MultirowTest}%
\subsection{MultiColumn}%
\label{subsec:MultiColumn}%
\begin{tabular}{|c|c|c|c|}%
\hline%
\multicolumn{4}{|c|}{Multicolumn}\\%
\hline%
1&2&3&4\\%
\hline%
5&6&7&8\\%
\hline%
9&\multicolumn{3}{|c|}{Multicolumn not on left}\\%
\hline%
\end{tabular}

%
\subsection{MultiRow}%
\label{subsec:MultiRow}%
\begin{tabular}{|c|c|c|}%
\hline%
\multirow{3}{*}{Multirow}&1&2\\%
\cline{2%
-%
3}%
&3&4\\%
\cline{2%
-%
3}%
&5&6\\%
\hline%
\multirow{3}{*}{Multirow2}&&\\%
&&\\%
&&\\%
\hline%
\end{tabular}

%
\subsection{MultiColumn and MultiRow}%
\label{subsec:MultiColumnandMultiRow}%
\begin{tabular}{|c|c|c|}%
\hline%
\multicolumn{2}{|c|}{\multirow{2}{*}{multi{-}col{-}row}}&X\\%
\multicolumn{2}{|c|}{}&X\\%
\hline%
X&X&X\\%
\hline%
\end{tabular}

%
\subsection{Vext01}%
\label{subsec:Vext01}%
\begin{tabular}{|c|c|c|}%
\hline%
\multirow{4}{*}{span{-}4}&\multirow{2}{*}{span{-}2}&3a\\%
\cline{3%
-%
3}%
&&3b\\%
\cline{2%
-%
3}%
&\multirow{2}{*}{span{-}2}&3c\\%
\cline{3%
-%
3}%
&&3d\\%
\hline%
\end{tabular}

%
\end{document}

multirow.pdf

../_images/multirow.pdf.png