texenv


Nametexenv JSON
Version 0.0.8 PyPI version JSON
download
home_page
Summarylatex inside a python virtual environment
upload_time2024-02-26 04:48:04
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords latex tex environment
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # texenv

Create lightweight TeX virtual environments, and use Python methods directly from TeX code. 

## Installation

`texenv` is currently only supported on Windows, and requires `texlive` to be installed on the system.  
https://tug.org/texlive/windows.html#install


Install `texenv` inside an existing Python virtual environment,

```bash
pip install texenv
```

To create a standalone TeX installation visible only to the current environment, 
```bash
texenv init
```
This installs a bare-bones version of LaTeX in `.venv/tex` and modifies the environment activation scripts so this TeX installation is used instead of the base installation. The console needs to be closed and re-opened for the changes to take effect.

## Usage

The TeX environment supports the standard `tlmgr` tool which can be used to install and update packages as normal.
```bash
tlmgr install <package name>
```

To write all currently installed TeX packages in the environment to a file (excluding core packages required for LaTeX to run),
```bash
texenv freeze > texrequirements.txt
```

To synchronize the TeX installation with the packages found in a requirements file,
```bash
texenv sync texrequirements.txt
```

`.tex` files can be compiled in the environment using the standard tools (i.e `pdflatex`). `texenv` also supports a preprocessor that can be used to call Python methods directly from TeX code. This is useful for generating figures and table data from python, or doing math that is difficult in LaTeX code. The example below shows a simple use case:

Contents of `example.tex`:
```tex
\documentclass{article}

% import a python module either installed in the environment, or just a file in the same folder as the .tex file.
% In this case figures.py is in the same folder.
\import\figures

% preprocessor variables can be defined with \pydef followed by the variable name and value.
\pydef\cleanfig true

\begin{document}
  
  % call a python method. Supports arguments and kwargs, but all are passed in as string types. The return type of 
  % the method must be a string as it is inserted directly into the TeX code.
  \figures\figA[clean=\cleanfig, width=5in]
   
\end{document}
```
Contents of `figures.py`, located in the same directory as `example.tex`:
```python
from texenv.macros import figure
import matplotlib.pyplot as plt

def figA(clean="true", width="3in", **kwargs):
    
    if clean == "false":
        return figure(file="figA.pdf", width=width, **kwargs)

    fig, ax1 = plt.subplots(1, 1)
    ax1.plot(range(11, 17))

    fig.savefig("figA.pdf")

    return figure(file="figA.pdf", width=width, **kwargs)

```

Compile on command line inside virtual environment:
```bash
texenv run example.tex
```

Full example:
[examples/macro_example/macro_example.tex](examples/macro_example/macro_example.tex)

The `run` command invokes the preprocessor, and then calls `pdflatex` on the post-processed `.tex` file. The synctex file is modified after running `pdflatex` so the intermediate file is transparent to synctex.

## Slideshows

`texenv` provides a simple way to generate PDF slideshow presentations directly from Python. Matplotlib figures, images, and LaTeX code (as strings in Python) can be assembled together into a slide using the `Presentation` class:

```python
from texenv import Presentation, datatable

... 

# text to place on slide. This is interpreted as an enumerate list if \item appears first in the string.
# Otherwise, the text is inserted directly as LaTeX code and supports math macros etc...
text = r"""
\item Bullet 1
\item Bullet 2
\[ \nabla \times \mathbf{E} = -\mu {\partial \mathbf{H} \over \partial t} \]
\[ \nabla \times \mathbf{H} = \varepsilon {\partial \mathbf{E} \over \partial t} \]
"""

# create a table with header names, and a format string applied to each cell
table = datatable(
    np.arange(16).reshape((4, 4)), header_row=[f"Header {i}" for i in range(4)], formatter="{:.2f}"
)

pres = Presentation("example_slides.pdf")

# add the content to the first slide. The layout will be a 2x2 grid, with the figure centered along both rows in the
# second column. The text and table will split the first column. 
pres.add_slide(
    content=[[text, fig], 
             [table, None]], 
    title="Example TeX Slides", 
    width_ratio=(1, 2) # make the second column twice as wide as the first column.
)

pres.save()
```

![example2](https://raw.githubusercontent.com/ricklyon/texenv/main/docs/img/example_slide.png)

Full example:
[examples/slideshow/slideshow.py](examples/slideshow/slideshow.py)


## VSCode Setup

`texenv` is designed to work with the Latex Workshop extension in VSCode. Once the extension is installed, the following
settings should be added to the user `settings.json` file:

```json
    "latex-workshop.latex.recipes": [
        {
            "name": "texenv",
            "tools": [
                "texenv"
            ]
        }
    ],
    "latex-workshop.latex.tools": [
        {
          "name": "texenv",
          "command": "texenv",
          "args": [
            "run", "%DOC_EXT%"
          ],
          "env": {
            "Path": "%WORKSPACE_FOLDER%/.venv/tex/bin/windows;%WORKSPACE_FOLDER%/.venv/Scripts;%PATH%"
          }
        },
    ],
    "latex-workshop.view.pdf.internal.synctex.keybinding": "double-click",
    "latex-workshop.latex.autoBuild.run": "onSave",
```

## License

texenv is licensed under the MIT License.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "texenv",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "latex,tex,environment",
    "author": "",
    "author_email": "Rick Lyon <rlyon14@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/3f/a4/b51cb21858ac7eeb1c836e8f2f06a9c7d340ddf99c576cf861ee1627e10c/texenv-0.0.8.tar.gz",
    "platform": null,
    "description": "# texenv\r\n\r\nCreate lightweight TeX virtual environments, and use Python methods directly from TeX code. \r\n\r\n## Installation\r\n\r\n`texenv` is currently only supported on Windows, and requires `texlive` to be installed on the system.  \r\nhttps://tug.org/texlive/windows.html#install\r\n\r\n\r\nInstall `texenv` inside an existing Python virtual environment,\r\n\r\n```bash\r\npip install texenv\r\n```\r\n\r\nTo create a standalone TeX installation visible only to the current environment, \r\n```bash\r\ntexenv init\r\n```\r\nThis installs a bare-bones version of LaTeX in `.venv/tex` and modifies the environment activation scripts so this TeX installation is used instead of the base installation. The console needs to be closed and re-opened for the changes to take effect.\r\n\r\n## Usage\r\n\r\nThe TeX environment supports the standard `tlmgr` tool which can be used to install and update packages as normal.\r\n```bash\r\ntlmgr install <package name>\r\n```\r\n\r\nTo write all currently installed TeX packages in the environment to a file (excluding core packages required for LaTeX to run),\r\n```bash\r\ntexenv freeze > texrequirements.txt\r\n```\r\n\r\nTo synchronize the TeX installation with the packages found in a requirements file,\r\n```bash\r\ntexenv sync texrequirements.txt\r\n```\r\n\r\n`.tex` files can be compiled in the environment using the standard tools (i.e `pdflatex`). `texenv` also supports a preprocessor that can be used to call Python methods directly from TeX code. This is useful for generating figures and table data from python, or doing math that is difficult in LaTeX code. The example below shows a simple use case:\r\n\r\nContents of `example.tex`:\r\n```tex\r\n\\documentclass{article}\r\n\r\n% import a python module either installed in the environment, or just a file in the same folder as the .tex file.\r\n% In this case figures.py is in the same folder.\r\n\\import\\figures\r\n\r\n% preprocessor variables can be defined with \\pydef followed by the variable name and value.\r\n\\pydef\\cleanfig true\r\n\r\n\\begin{document}\r\n  \r\n  % call a python method. Supports arguments and kwargs, but all are passed in as string types. The return type of \r\n  % the method must be a string as it is inserted directly into the TeX code.\r\n  \\figures\\figA[clean=\\cleanfig, width=5in]\r\n   \r\n\\end{document}\r\n```\r\nContents of `figures.py`, located in the same directory as `example.tex`:\r\n```python\r\nfrom texenv.macros import figure\r\nimport matplotlib.pyplot as plt\r\n\r\ndef figA(clean=\"true\", width=\"3in\", **kwargs):\r\n    \r\n    if clean == \"false\":\r\n        return figure(file=\"figA.pdf\", width=width, **kwargs)\r\n\r\n    fig, ax1 = plt.subplots(1, 1)\r\n    ax1.plot(range(11, 17))\r\n\r\n    fig.savefig(\"figA.pdf\")\r\n\r\n    return figure(file=\"figA.pdf\", width=width, **kwargs)\r\n\r\n```\r\n\r\nCompile on command line inside virtual environment:\r\n```bash\r\ntexenv run example.tex\r\n```\r\n\r\nFull example:\r\n[examples/macro_example/macro_example.tex](examples/macro_example/macro_example.tex)\r\n\r\nThe `run` command invokes the preprocessor, and then calls `pdflatex` on the post-processed `.tex` file. The synctex file is modified after running `pdflatex` so the intermediate file is transparent to synctex.\r\n\r\n## Slideshows\r\n\r\n`texenv` provides a simple way to generate PDF slideshow presentations directly from Python. Matplotlib figures, images, and LaTeX code (as strings in Python) can be assembled together into a slide using the `Presentation` class:\r\n\r\n```python\r\nfrom texenv import Presentation, datatable\r\n\r\n... \r\n\r\n# text to place on slide. This is interpreted as an enumerate list if \\item appears first in the string.\r\n# Otherwise, the text is inserted directly as LaTeX code and supports math macros etc...\r\ntext = r\"\"\"\r\n\\item Bullet 1\r\n\\item Bullet 2\r\n\\[ \\nabla \\times \\mathbf{E} = -\\mu {\\partial \\mathbf{H} \\over \\partial t} \\]\r\n\\[ \\nabla \\times \\mathbf{H} = \\varepsilon {\\partial \\mathbf{E} \\over \\partial t} \\]\r\n\"\"\"\r\n\r\n# create a table with header names, and a format string applied to each cell\r\ntable = datatable(\r\n    np.arange(16).reshape((4, 4)), header_row=[f\"Header {i}\" for i in range(4)], formatter=\"{:.2f}\"\r\n)\r\n\r\npres = Presentation(\"example_slides.pdf\")\r\n\r\n# add the content to the first slide. The layout will be a 2x2 grid, with the figure centered along both rows in the\r\n# second column. The text and table will split the first column. \r\npres.add_slide(\r\n    content=[[text, fig], \r\n             [table, None]], \r\n    title=\"Example TeX Slides\", \r\n    width_ratio=(1, 2) # make the second column twice as wide as the first column.\r\n)\r\n\r\npres.save()\r\n```\r\n\r\n![example2](https://raw.githubusercontent.com/ricklyon/texenv/main/docs/img/example_slide.png)\r\n\r\nFull example:\r\n[examples/slideshow/slideshow.py](examples/slideshow/slideshow.py)\r\n\r\n\r\n## VSCode Setup\r\n\r\n`texenv` is designed to work with the Latex Workshop extension in VSCode. Once the extension is installed, the following\r\nsettings should be added to the user `settings.json` file:\r\n\r\n```json\r\n    \"latex-workshop.latex.recipes\": [\r\n        {\r\n            \"name\": \"texenv\",\r\n            \"tools\": [\r\n                \"texenv\"\r\n            ]\r\n        }\r\n    ],\r\n    \"latex-workshop.latex.tools\": [\r\n        {\r\n          \"name\": \"texenv\",\r\n          \"command\": \"texenv\",\r\n          \"args\": [\r\n            \"run\", \"%DOC_EXT%\"\r\n          ],\r\n          \"env\": {\r\n            \"Path\": \"%WORKSPACE_FOLDER%/.venv/tex/bin/windows;%WORKSPACE_FOLDER%/.venv/Scripts;%PATH%\"\r\n          }\r\n        },\r\n    ],\r\n    \"latex-workshop.view.pdf.internal.synctex.keybinding\": \"double-click\",\r\n    \"latex-workshop.latex.autoBuild.run\": \"onSave\",\r\n```\r\n\r\n## License\r\n\r\ntexenv is licensed under the MIT License.\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "latex inside a python virtual environment",
    "version": "0.0.8",
    "project_urls": {
        "repository": "https://github.com/ricklyon/texenv"
    },
    "split_keywords": [
        "latex",
        "tex",
        "environment"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e3b87b854e4649446159c1f9bab8d8ef955621e1af85ae3f18c887fff8512336",
                "md5": "f730570fa0357da32a0373fb4fa6c496",
                "sha256": "cdd067cbcc6079e5e91adb12affffa7176ad5ca4e62c5f7b66885d5bcc101bb9"
            },
            "downloads": -1,
            "filename": "texenv-0.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f730570fa0357da32a0373fb4fa6c496",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 20913,
            "upload_time": "2024-02-26T04:48:02",
            "upload_time_iso_8601": "2024-02-26T04:48:02.667029Z",
            "url": "https://files.pythonhosted.org/packages/e3/b8/7b854e4649446159c1f9bab8d8ef955621e1af85ae3f18c887fff8512336/texenv-0.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3fa4b51cb21858ac7eeb1c836e8f2f06a9c7d340ddf99c576cf861ee1627e10c",
                "md5": "dd5e3f9cdeb5a68dcf19541beb908acf",
                "sha256": "adb8cb29773328be29ff8620a2ed80c45ef5755d556f39fcc1be8f86ba227a66"
            },
            "downloads": -1,
            "filename": "texenv-0.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "dd5e3f9cdeb5a68dcf19541beb908acf",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 21144,
            "upload_time": "2024-02-26T04:48:04",
            "upload_time_iso_8601": "2024-02-26T04:48:04.326441Z",
            "url": "https://files.pythonhosted.org/packages/3f/a4/b51cb21858ac7eeb1c836e8f2f06a9c7d340ddf99c576cf861ee1627e10c/texenv-0.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-26 04:48:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ricklyon",
    "github_project": "texenv",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "texenv"
}
        
Elapsed time: 0.20337s