Files
mytoolkit/mycli/commands/latex.py
T

50 lines
1.2 KiB
Python

"""LaTeX utilities."""
import subprocess
from pathlib import Path
import click
from mycli.utils import handle_errors, run_command
@click.group()
def latex():
"""LaTeX compilation and utilities."""
pass
@latex.command("compile")
@click.argument("files", nargs=-1, required=True)
@click.option("--engine", "-e", default="pdflatex", help="LaTeX engine")
@click.option("--dry-run", "-n", is_flag=True, help="Show what would be done")
@handle_errors
def compile(files, engine, dry_run):
"""Compile LaTeX files."""
for pattern in files:
for tex_file in Path(".").glob(pattern):
if tex_file.suffix.lower() != ".tex":
continue
if dry_run:
click.echo(f"Would compile: {tex_file}")
continue
click.echo(f"Compiling: {tex_file}")
run_command([engine, str(tex_file)], check=False)
@latex.command("count")
@click.argument("file", type=click.Path(exists=True))
@handle_errors
def count(file):
"""Count words in a LaTeX file (excluding commands)."""
result = run_command(
["detex", file],
capture_output=True,
text=True,
check=False,
)
words = len(result.stdout.split())
click.echo(f"Words: {words}")