Skip to content

ddocs.latex.clean

Remove LaTeX build artifacts (.aux, .log, .bbl, ...) from a directory. Backs the ddocs clean command; independent of the Markdown-to-LaTeX conversion.

ddocs.latex.clean

Remove LaTeX build artifacts (.aux, .log, .bbl, ...) from a directory.

Backs the ddocs clean command. This is independent of the Markdown-to-LaTeX conversion -- it only deletes the throwaway files a LaTeX/BibTeX run leaves behind.

clean_latex_build_files

clean_latex_build_files(directory: Path, pattern: str = '**/*') -> int

Clean LaTeX build artifacts from a directory.

Removes common LaTeX build files generated by pdflatex, bibtex, and related tools: - .aux, .log, .bbl, .blg, .toc, .lof, .lot files - .fdb_latexmk, .fls, .synctex.gz files - .out, .nav, .snm, .vrb files (for beamer/presentation) - .idx, .ilg, .ind files (for index generation)

Parameters:

Name Type Description Default
directory Path

Directory to clean

required
pattern str

Glob pattern for subdirectories to include (default: "*/" for recursive)

'**/*'

Returns:

Type Description
int

Number of files removed

Source code in src\ddocs\latex\clean.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def clean_latex_build_files(directory: Path, pattern: str = "**/*") -> int:
    """Clean LaTeX build artifacts from a directory.

    Removes common LaTeX build files generated by pdflatex, bibtex, and related tools:
    - .aux, .log, .bbl, .blg, .toc, .lof, .lot files
    - .fdb_latexmk, .fls, .synctex.gz files
    - .out, .nav, .snm, .vrb files (for beamer/presentation)
    - .idx, .ilg, .ind files (for index generation)

    Args:
        directory: Directory to clean
        pattern: Glob pattern for subdirectories to include (default: "**/*" for recursive)

    Returns:
        Number of files removed
    """
    if not directory.exists():
        print(f"Error: Directory not found: {directory}")
        return 0

    if not directory.is_dir():
        print(f"Error: Path is not a directory: {directory}")
        return 0

    # LaTeX build file extensions to remove
    latex_extensions = {
        '.aux',     # Auxiliary file
        '.log',     # Log file
        '.bbl',     # Bibliography file (bibtex output)
        '.blg',     # Bibliography log file
        '.toc',     # Table of contents
        '.lof',     # List of figures
        '.lot',     # List of tables
        '.fdb_latexmk',  # latexmk file database
        '.fls',     # File list (latex -recorder)
        '.synctex.gz',   # SyncTeX file
        '.out',     # Hyperref outline
        '.nav',     # Beamer navigation
        '.snm',     # Beamer slide notes
        '.vrb',     # Beamer verbatim
        '.idx',     # Index file
        '.ilg',     # Index log
        '.ind',     # Index output
        '.xdy',     # xindy index style
    }

    removed_count = 0

    # Find all files matching the extensions
    for ext in latex_extensions:
        if pattern == "**/*":
            # Recursive pattern
            glob_pattern = f"**/*{ext}"
        else:
            # Non-recursive pattern
            glob_pattern = f"*{ext}"

        files_to_remove = list(directory.glob(glob_pattern))

        for file_path in files_to_remove:
            try:
                file_path.unlink()
                print(f"Removed: {file_path.relative_to(directory)}")
                removed_count += 1
            except OSError as e:
                print(f"Warning: Could not remove {file_path}: {e}")

    return removed_count

clean_latex_cli

clean_latex_cli(args: Namespace) -> int

CLI interface for cleaning LaTeX build files.

Source code in src\ddocs\latex\clean.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def clean_latex_cli(args: argparse.Namespace) -> int:
    """CLI interface for cleaning LaTeX build files."""

    # Check if directory exists
    if not args.directory.exists():
        print(f"Error: Directory not found: {args.directory}")
        return 1

    if not args.directory.is_dir():
        print(f"Error: Path is not a directory: {args.directory}")
        return 1

    print(f"Cleaning LaTeX build files from: {args.directory}")

    # Determine search pattern
    pattern = "**/*" if args.recursive else "*"

    removed_count = clean_latex_build_files(args.directory, pattern)

    if removed_count > 0:
        print(f"Cleaned {removed_count} LaTeX build file(s)")
    else:
        print("No LaTeX build files found to clean")

    return 0