Skip to content

jettex.install

TinyTeX installation and management functions.

Module Overview

This module handles downloading, installing, and managing TinyTeX.

from jettex import install_tinytex, uninstall_tinytex, tinytex_root

Functions

install_tinytex(version=1, install_dir=None, force=False, add_path=True, progress_callback=None)

Download and install TinyTeX.

Parameters:

Name Type Description Default
version int

TinyTeX version to install - 0: infraonly (minimal, no packages) - 1: default (~90 packages for common documents) - 2: extended (more packages)

1
install_dir Optional[Path]

Custom installation directory (default: platform-specific)

None
force bool

Force reinstall even if already installed

False
add_path bool

Add TinyTeX bin to PATH

True
progress_callback Optional[Callable[[str, int, int], None]]

Optional callback(stage, downloaded, total)

None

Returns:

Name Type Description
Path Path

Installation directory

Raises:

Type Description
RuntimeError

If installation fails

Source code in jettex/install.py
def install_tinytex(
    version: int = 1,
    install_dir: Optional[Path] = None,
    force: bool = False,
    add_path: bool = True,
    progress_callback: Optional[Callable[[str, int, int], None]] = None,
) -> Path:
    """Download and install TinyTeX.

    Args:
        version: TinyTeX version to install
            - 0: infraonly (minimal, no packages)
            - 1: default (~90 packages for common documents)
            - 2: extended (more packages)
        install_dir: Custom installation directory (default: platform-specific)
        force: Force reinstall even if already installed
        add_path: Add TinyTeX bin to PATH
        progress_callback: Optional callback(stage, downloaded, total)

    Returns:
        Path: Installation directory

    Raises:
        RuntimeError: If installation fails
    """
    if install_dir is None:
        install_dir = get_default_install_dir()
    else:
        install_dir = Path(install_dir)

    # Check if already installed
    if not force and is_tinytex_installed():
        existing = get_tinytex_root()
        if existing:
            if add_path:
                bin_dir = get_bin_dir()
                if bin_dir:
                    add_to_path(bin_dir)
            return existing

    # Remove existing installation if force
    if force and install_dir.exists():
        shutil.rmtree(install_dir)

    # Create installation directory
    install_dir.mkdir(parents=True, exist_ok=True)

    # Get download URL
    url = get_download_url(version)

    # Determine archive filename
    archive_name = url.split("/")[-1]

    # Download to temp file
    with tempfile.TemporaryDirectory() as tmpdir:
        tmpdir_path = Path(tmpdir)
        archive_path = tmpdir_path / archive_name

        # Download
        def download_progress(downloaded: int, total: int) -> None:
            if progress_callback:
                progress_callback("downloading", downloaded, total)

        try:
            download_file(url, archive_path, download_progress)
        except requests.RequestException as e:
            raise RuntimeError(f"Failed to download TinyTeX: {e}") from e

        # Extract
        if progress_callback:
            progress_callback("extracting", 0, 0)

        try:
            extract_archive(archive_path, tmpdir_path)
        except Exception as e:
            raise RuntimeError(f"Failed to extract TinyTeX: {e}") from e

        # Find extracted directory (usually named TinyTeX or .TinyTeX)
        extracted_dirs = [
            d for d in tmpdir_path.iterdir()
            if d.is_dir() and d.name != "__MACOSX"
        ]

        if not extracted_dirs:
            raise RuntimeError("No directory found in extracted archive")

        extracted_dir = extracted_dirs[0]

        # Move to installation directory
        if progress_callback:
            progress_callback("installing", 0, 0)

        # If the install_dir is the target, move contents into it
        for item in extracted_dir.iterdir():
            dest = install_dir / item.name
            if dest.exists():
                if dest.is_dir():
                    shutil.rmtree(dest)
                else:
                    dest.unlink()
            shutil.move(str(item), str(dest))

    # Add to PATH if requested
    if add_path:
        bin_dir = get_bin_dir()
        if bin_dir:
            add_to_path(bin_dir)

    if progress_callback:
        progress_callback("complete", 100, 100)

    return install_dir

uninstall_tinytex(install_dir=None)

Uninstall TinyTeX.

Parameters:

Name Type Description Default
install_dir Optional[Path]

Installation directory to remove (default: detect automatically)

None

Returns:

Name Type Description
bool bool

True if uninstalled successfully

Source code in jettex/install.py
def uninstall_tinytex(install_dir: Optional[Path] = None) -> bool:
    """Uninstall TinyTeX.

    Args:
        install_dir: Installation directory to remove (default: detect automatically)

    Returns:
        bool: True if uninstalled successfully
    """
    if install_dir is None:
        install_dir = get_tinytex_root()

    if install_dir is None or not install_dir.exists():
        return False

    try:
        shutil.rmtree(install_dir)
        return True
    except Exception:
        return False

tinytex_root()

Get the TinyTeX installation root directory.

Returns:

Type Description
Optional[Path]

Path or None: Installation directory if found

Source code in jettex/install.py
def tinytex_root() -> Optional[Path]:
    """Get the TinyTeX installation root directory.

    Returns:
        Path or None: Installation directory if found
    """
    return get_tinytex_root()

tinytex_version()

Get the installed TinyTeX/TeX Live version.

Returns:

Type Description
Optional[str]

str or None: Version string if available

Source code in jettex/install.py
def tinytex_version() -> Optional[str]:
    """Get the installed TinyTeX/TeX Live version.

    Returns:
        str or None: Version string if available
    """
    from .tlmgr import tlmgr_version
    return tlmgr_version()

Examples

Basic Installation

from jettex import install_tinytex

# Install with defaults
install_tinytex()

Custom Installation

from pathlib import Path
from jettex import install_tinytex

# Install minimal version to custom directory
install_tinytex(
    version=0,
    install_dir=Path("/opt/tinytex"),
    force=True
)

Progress Tracking

from jettex import install_tinytex

def on_progress(stage, downloaded, total):
    if stage == "downloading":
        percent = (downloaded / total) * 100 if total else 0
        print(f"\rDownloading: {percent:.1f}%", end="")
    elif stage == "extracting":
        print("\nExtracting...")
    elif stage == "complete":
        print("Done!")

install_tinytex(progress_callback=on_progress)