{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Document Tabular Data\n\nMultiple ways of inserting tables into your document.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pylatex import NoEscape, NewLine\nfrom latexdocs import Document\nimport numpy as np\n\ndoc = Document()\n\n# The height of each row is set to 1.5 relative to its default height.\ndoc.append(NoEscape(r'\\renewcommand{\\arraystretch}{1.5}'))\n\n# The space between the text and the left/right border of its\n# containing cell is set to 18pt with this command. Again,\n# you may use other units if needed.\ndoc.append(NoEscape(r'\\setlength{\\tabcolsep}{18pt}'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Tables using `pylatex`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pylatex import Tabular, Tabularx\n\ntable = Tabular('rc|cl')\ntable.add_hline()\ntable.add_row((1, 2, 3, 4))\ntable.add_hline(1, 2)\ntable.add_empty_row()\ntable.add_row((4, 5, 6, 7))\ndoc.append(table)\ndoc.append(NewLine())\n\ntable = Tabularx('X|X|X|X')\ntable.add_hline()\ntable.add_row((1, 2, 3, 4))\ntable.add_hline(1, 2)\ntable.add_empty_row()\ntable.add_row((4, 5, 6, 7))\ndoc.append(table)\ndoc.append(NewLine())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Tables using `texttable` and `latextable`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from texttable import Texttable\nimport latextable\n\ntable_1 = Texttable()\ntable_1.set_cols_align([\"l\", \"r\", \"c\"])\ntable_1.set_cols_valign([\"t\", \"m\", \"b\"])\ntable_1.add_rows([[\"Name\", \"Age\", \"Nickname\"],\n                 [\"Mr\\nXavier\\nHuon\", 32, \"Xav'\"],\n                 [\"Mr\\nBaptiste\\nClement\", 1, \"Baby\"],\n                 [\"Mme\\nLouise\\nBourgeau\", 28, \"Lou\\n \\nLoue\"]])\nprint('-- Example 1: Basic --')\nprint('Texttable Output:')\nprint(table_1.draw())\nprint('\\nLatextable Output:')\ncontent = latextable.draw_latex(table_1, caption=\"An example table.\",\n                                label=\"table:example_table\")\nprint(content)\ndoc.append(NoEscape(content))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Tables using `latexdocs`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from latexdocs import Table, TableX\n\nlabels = ['A', 'B', 'C', 'D']\ndata = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\ndoc.append(Table(data=data, columns=labels, table_spec=r\"c|c|c|c\"))\n\ntable = Table('c c c c', 'h!', caption=\"This is a table.\",\n              label=\"table:tbl1\")\ntable.add_hline()\ntable.add_row(('Case', \"Method 1\", \"Method 2\", \"Method 3\"))\ntable.add_hline()\ntable.add_hline()\ntable.add_row((1, 50, 837, 970))\ntable.add_row((2, 51, 838, 971))\ntable.add_row((3, 52, 839, 972))\ntable.add_row((4, 53, 840, 973))\ntable.add_hline()\ndoc.append(table)\n\nlabels = ['A', 'B', 'C', 'D']\ndata = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\nnD = data.shape[-1]\ntable_spec = r\"|\".join(nD * [r\">{\\centering\\arraybackslash}X\",])\ndoc.append(TableX(table_spec, 'h!', data=data, columns=labels))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Build the document\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "doc.build().generate_pdf('tables', compiler='pdflatex')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Show the result using `pypdfium2` and `matplotlib`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pypdfium2 as pdfium\nimport matplotlib.pyplot as plt\n\npdf = pdfium.PdfDocument(\"tables.pdf\")\npage = pdf.get_page(0)\npil_image = page.render_topil()\nplt.imshow(pil_image)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}