Repository: google/latexify_py

Issue:
Support for nested functions
As an example use case, I have a function that takes in a dataset and returns a closure based on that dataset.
```python
def gen_normalizer(df):

    def normalize_row_based_on_dataset(row):
        return (row - df.mean()) / df.std()
    
    return normalize_row_based_on_dataset
```

I would love to be able to apply the decorator to the nested function so that later in my experiment I can display a set of different Normalizers in LaTeX. However, currently adding the decorator to the nested function results in the following error:
```python
File <unknown>:1
    @latexify.with_latex
    ^
IndentationError: unexpected indent
```

Discussion:

Patch:
diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml
index 1e6ed35..ed4cc7c 100644
--- a/.github/workflows/python-package.yml
+++ b/.github/workflows/python-package.yml
@@ -24,7 +24,7 @@ jobs:
         python -m pip install --upgrade pip
         python -m pip install -e ".[dev]"
     - name: Test
-      run: python -m pytest tests
+      run: python -m pytest src
 
   black:
     runs-on: ubuntu-latest
@@ -39,7 +39,7 @@ jobs:
         python -m pip install --upgrade pip
         python -m pip install -e ".[dev]"
     - name: Check
-      run: python -m black src tests
+      run: python -m black src
 
   flake8:
     runs-on: ubuntu-latest
@@ -54,4 +54,4 @@ jobs:
         python -m pip install --upgrade pip
         python -m pip install -e ".[dev]"
     - name: Check
-      run: python -m pflake8 src tests
+      run: python -m pflake8 src
diff --git a/checks.sh b/checks.sh
new file mode 100755
index 0000000..205bf86
--- /dev/null
+++ b/checks.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+set -eoux pipefail
+
+python -m pytest src
+python -m black src
+python -m pflake8 src
diff --git a/src/latexify/core.py b/src/latexify/core.py
index bc50596..8a9f0d1 100644
--- a/src/latexify/core.py
+++ b/src/latexify/core.py
@@ -17,6 +17,7 @@
 
 import ast
 import inspect
+import textwrap
 
 import dill
 
@@ -307,6 +308,8 @@ def get_latex(fn, *args, **kwargs):
         # Maybe running on console.
         source = dill.source.getsource(fn)
 
+    source = textwrap.dedent(source)
+
     return LatexifyVisitor(*args, **kwargs).visit(ast.parse(source))
 
 


Test patch:
diff --git a/src/integration_tests/regression_test.py b/src/integration_tests/regression_test.py
index 9ff8b8d..7a2f432 100644
--- a/src/integration_tests/regression_test.py
+++ b/src/integration_tests/regression_test.py
@@ -16,7 +16,7 @@
 import math
 import pytest
 
-from latexify import with_latex
+from latexify import get_latex, with_latex
 
 
 def solve(a, b, c):
@@ -91,3 +91,20 @@ def test_with_latex_to_str(func, expected_latex, math_symbol):
     assert str(latexified_function) == expected_latex
     expected_repr = r"$$ \displaystyle %s $$" % expected_latex
     assert latexified_function._repr_latex_() == expected_repr
+
+
+def test_nested_function():
+    def nested(x):
+        return 3 * x
+
+    assert get_latex(nested) == r"\mathrm{nested}(x) \triangleq 3x"
+
+
+def test_double_nested_function():
+    def nested(x):
+        def inner(y):
+            return x * y
+
+        return inner
+
+    assert get_latex(nested(3)) == r"\mathrm{inner}(y) \triangleq xy"