{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Basic Example\n\nAn example highlighting the main differences between `pylatex` and `latexdocs`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# This code is a copy of the following example:\n# https://jeltef.github.io/PyLaTeX/current/examples/full.html\n\nimport numpy as np\nfrom pylatex import Document, Section, Subsection, Tabular, Math, \\\n    TikZ, Axis, Plot, Figure, Matrix, Alignat, NoEscape\nfrom pylatex.utils import italic\nimport pypdfium2 as pdfium\nimport matplotlib.pyplot as plt\n\nimage_filename = 'image.png'\n\ngeometry_options = {\n    \"tmargin\": \"1.5cm\",\n    \"lmargin\": \"1.5cm\",\n    \"rmargin\": \"1.5cm\"\n}\ndoc = Document(geometry_options=geometry_options)\n\nwith doc.create(Section('Some basic content')):\n    doc.append('Some regular text and some')\n    doc.append(italic('italic text. '))\n    doc.append('\\nAlso some crazy characters: $&#{}')\n    with doc.create(Subsection('Math that is incorrect')):\n        doc.append(Math(data=['2*3', '=', 9]))\n\n    with doc.create(Subsection('Table of something')):\n        with doc.create(Tabular('rc|cl')) as table:\n            table.add_hline()\n            table.add_row((1, 2, 3, 4))\n            table.add_hline(1, 2)\n            table.add_empty_row()\n            table.add_row((4, 5, 6, 7))\n\na = np.array([[100, 10, 20]]).T\nM = np.matrix([[2, 3, 4], [0, 0, 1], [0, 0, 2]])\n\nwith doc.create(Section('Another section')):\n    with doc.create(Subsection('Correct matrix equations')):\n        doc.append(Math(data=[Matrix(M), Matrix(a), '=', Matrix(M * a)]))\n\n    with doc.create(Subsection('Alignat math environment')):\n        with doc.create(Alignat(numbering=False, escape=False)) as agn:\n            agn.append(r'\\frac{a}{b} &= 0 \\\\')\n            agn.extend([Matrix(M), Matrix(a), '&=', Matrix(M * a)])\n\n    with doc.create(Subsection('Beautiful graphs')):\n        with doc.create(TikZ()):\n            plot_options = 'height=4cm, width=6cm, grid=major'\n            with doc.create(Axis(options=plot_options)) as plot:\n                plot.append(Plot(name='model', func='-x^5 - 242'))\n\n                coordinates = [\n                    (-4.77778, 2027.60977),\n                    (-3.55556, 347.84069),\n                    (-2.33333, 22.58953),\n                    (-1.11111, -493.50066),\n                    (0.11111, 46.66082),\n                    (1.33333, -205.56286),\n                    (2.55556, -341.40638),\n                    (3.77778, -1169.24780),\n                    (5.00000, -3269.56775),\n                ]\n\n                plot.append(Plot(name='estimate', coordinates=coordinates))\n\n    with doc.create(Subsection('An image')):\n        with doc.create(Figure(position='h!')) as pic:\n            pic.add_image(image_filename, width='350px')\n            pic.add_caption('A simple strucutre.')\n\ndoc.generate_pdf('basic_example_pylatex', clean_tex=False, compiler='pdflatex')\n\npdf = pdfium.PdfDocument(\"basic_example_pylatex.pdf\")\npage = pdf.get_page(0)\npil_image = page.render_topil()\nplt.imshow(pil_image)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now the same using ``latexdocs`` to have a little bit more control over when and what we do:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from latexdocs import Document, TikZFigure, Image\n\ndoc = Document()\n\ndoc['Some basic content'].append('Some regular text and some')\ndoc['Some basic content'].append(italic('italic text. '))\ndoc['Some basic content'].append('\\nAlso some crazy characters: $&#{}')\ndoc['Some basic content', 'Math that is incorrect'].append((Math(data=['2*3', '=', 9])))\n                                                        \ncontent = Math(data=[Matrix(M), Matrix(a), '=', Matrix(M * a)])\ndoc['Another section', 'Correct matrix equations'].append(content)\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['Some basic content', 'Table of something'].append(table)\n\nagn = Alignat(numbering=False, escape=False)\nagn.append(r'\\frac{a}{b} &= 0 \\\\')\nagn.extend([Matrix(M), Matrix(a), '&=', Matrix(M * a)])\ndoc['Another section', 'Alignat math environment'].append(agn)\n\nfig = TikZFigure(plot_options='height=4cm, width=6cm, grid=major')\nfig.append(Plot(name='model', func='-x^5 - 242'))\ncoordinates = [\n    (-4.77778, 2027.60977),\n    (-3.55556, 347.84069),\n    (-2.33333, 22.58953),\n    (-1.11111, -493.50066),\n    (0.11111, 46.66082),\n    (1.33333, -205.56286),\n    (2.55556, -341.40638),\n    (3.77778, -1169.24780),\n    (5.00000, -3269.56775),\n]\nfig.append(Plot(name='estimate', coordinates=coordinates))\ndoc['Another section', 'Beautiful graphs'].append(fig)\n\nimg = Image(filename=image_filename, position='h!', \n            caption='A simple structure.', width='350px')\ndoc['Another section', 'An image'].append(img)\n\ndoc.build().generate_pdf('basic_example_latexdocs', clean_tex=True, compiler='pdflatex')\n\n# Take a look at the generated file\n\npdf = pdfium.PdfDocument(\"basic_example_latexdocs.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
}