{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multicolumns and multirows" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+----+----+----+----+\n", "| 1 | 2 | 3 | 4 |\n", "+----+----+----+----+\n", "| 5 | 6 | 7 | 8 |\n", "+----+----+----+----+\n", "| 9 | 10 | 11 | 12 |\n", "+----+----+----+----+\n" ] } ], "source": [ "from tabletexifier import Table\n", "\n", "t = Table(header=[1, 2, 3, 4], table_style=\"A\")\n", "t.add_row(list(range(5, 9)))\n", "t.add_row(list(range(9, 13)))\n", "print(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Setting as a multicolumn, works on both the terminal version of the table and, the changes are propagated to LaTeX through the multicol and multirow commands" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+----+----+----+----+\n", "| 1 | 2 | 3 | 4 |\n", "+----+----+----+----+\n", "| 5 | 7 | 8 |\n", "+----+----+ +----+\n", "| 9 | 10 | | 12 |\n", "+----+----+----+----+\n", "\n", "\\begin{table}[H]\n", "\\caption{}\n", "\\label{}\n", "\\centering\n", "\\begin{tabular}{c|c|c|c|}\n", " 1 & 2 & 3 & 4 \\\\ \\hline\n", "\\multicolumn{2}{c}{5} &\\multirow{2}{*}{7}& 8 \\\\ \\hline\n", " 9 & 10 & & 12 \\\\ \\hline\n", "\\end{tabular}\n", "\\end{table}\n" ] } ], "source": [ "t.set_cell_as_multi_col(start_col=0,\n", " start_row=1,\n", " number_of_cols=1\n", " )\n", "t.set_cell_as_multi_row(start_row=1,\n", " start_col= 2, \n", " number_of_rows=1)\n", "\n", "print(t)\n", "print(t.build_latex())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a few caveats if we attempt to build a multicol and a multirow (i.e. build a square of cells):\n", "\n", "1) On the text-version we get the extra \"+\" if the vlines are active in those cells\n", "2) On the LaTex version, the hlines are broken (it keeps using the \\hlines instead of the clines)\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+----+----+----+----+\n", "| 1 | 2 | 3 | 4 |\n", "+----+----+----+----+\n", "| 5 | 7 | 8 |\n", "+ + +----+----+\n", "| | 11 | 12 |\n", "+----+----+----+----+\n", "\n", "\\begin{table}[H]\n", "\\caption{}\n", "\\label{}\n", "\\centering\n", "\\begin{tabular}{c|c|c|c|}\n", " 1 & 2 & 3 & 4 \\\\ \\hline\n", "\\multicolumn{2}{c}{\\multirow{2}{*}{5}} & 7 & 8 \\\\ \\hline\n", "\\multicolumn{2}{c}{} & 11 & 12 \\\\ \\hline\n", "\\end{tabular}\n", "\\end{table}\n" ] } ], "source": [ "t = Table(header=[1, 2, 3, 4], table_style=\"A\")\n", "t.add_row(list(range(5, 9)))\n", "t.add_row(list(range(9, 13)))\n", "\n", "t.set_cell_as_multi_col(start_col=0,\n", " start_row=1,\n", " number_of_cols=1\n", " )\n", "\n", "t.set_cell_as_multi_col(start_col=0,\n", " start_row=2,\n", " number_of_cols=1\n", " )\n", "\n", "t.set_cell_as_multi_row(start_row=1,\n", " start_col= 0, \n", " number_of_rows=1)\n", "\n", "t.set_cell_as_multi_row(start_row=1,\n", " start_col= 1, \n", " number_of_rows=1)\n", "print(t)\n", "print(t.build_latex())" ] } ], "metadata": { "kernelspec": { "display_name": "aegir-JSxcqN-I-py3.8", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" } }, "nbformat": 4, "nbformat_minor": 2 }