$\LaTeX{}$使用批处理文件和Makefile实现快速编译和删除中间文件

本文介绍了在 \(\LaTeX{}\) 中如何使用批处理文件和Makefile来实现快速编译和删除中间文件,保持工作目录的清爽整洁。


批处理文件

在Windows下可以使用批处理文件来处理,也可以使用Makefile(但需配置make环境)。这里为了操作简单性,在Windows下只介绍如何使用批处理文件来实现快速删除中间文件和快速编译。

快速删除中间文件(辅助文件)

步骤如下:

  1. 新建文本文件命名为clean.bat

  2. 复制下面的代码放到文本文件中;

    1
    2
    3
    4
    5
    @echo off
    echo Cleaning auxiliary files...
    del /s /q "*.aux" "*.log" "*.out" "*.bbl" "*.blg" "*.toc" "*.lof" "*.lot" "*.synctex.gz"
    echo Cleaning completed!
    pause
  3. 将文件放入主文件(.tex)所在文件夹中,双击运行即可删除中间文件以及子文件夹中的中间文件。

快速编译并删除中间文件

步骤如下:

  1. 新建文本文件命名为compile.bat

  2. 复制下面的代码放到文本文件中;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    @echo off
    :: ==============================================
    :: LaTeX Compile Automation Script (XeLaTeX + BibTeX)
    :: Usage: Drag and drop the .tex file onto this script or manually specify the file name
    :: ==============================================

    :: set variable
    set TEX_COMPILER=xelatex
    set BIB_COMPILER=bibtex
    set MAX_ATTEMPTS=3
    set LOG_EXTENSIONS=*.aux *.log *.out *.bbl *.blg *.toc *.lof *.lot *.synctex.gz

    :: Check whether the file is obtained by dragging
    if "%~1"=="" (
    echo Error: Please drag the .tex file onto this script or manually specify the file name
    pause
    exit /b 1
    )

    :: Extract the file name (without extension)
    set "TEX_FILE=%~1"
    set "BASE_NAME=%~n1"

    :: Compile function definition
    :compile
    echo.
    echo =============== Start Compiling... ===============
    echo Compiling document: %TEX_FILE%

    :: First XeLaTeX Compilation
    echo.
    echo [1/4] First %TEX_COMPILER% compiling...
    %TEX_COMPILER% -interaction=nonstopmode -synctex=1 "%BASE_NAME%.tex"
    if %ERRORLEVEL% neq 0 (
    echo Error: First %TEX_COMPILER% Compilation failed
    goto error_handling
    )

    :: BibTeX Compilation
    echo.
    echo [2/4] %BIB_COMPILER% compiling reference...
    %BIB_COMPILER% "%BASE_NAME%.aux"
    if %ERRORLEVEL% neq 0 (
    echo Warning: %BIB_COMPILER% There may be issues with the compilation (check the .blg file)
    )

    :: Second XeLaTeX Compilation
    echo.
    echo [3/4] Second %TEX_COMPILER% compiling...
    %TEX_COMPILER% -interaction=nonstopmode -synctex=1 "%BASE_NAME%.tex"
    if %ERRORLEVEL% neq 0 (
    echo Error: Second %TEX_COMPILER% Compilation failed
    goto error_handling
    )

    :: Third XeLaTeX Compilation (Ensure correct cross-referencing)
    echo.
    echo [4/4] Third %TEX_COMPILER% compiling...
    %TEX_COMPILER% -interaction=nonstopmode -synctex=1 "%BASE_NAME%.tex"
    if %ERRORLEVEL% neq 0 (
    echo Error: Third %TEX_COMPILER% Compilation failed
    goto error_handling
    )

    :: Cleaning auxiliary files (Optional)
    echo.
    echo Cleaning auxiliary files...
    del /s /q %LOG_EXTENSIONS% 2>nul

    :: Completed Successfully
    echo.
    echo =============== Compilation Completed Successfully ===============
    echo Final output file: %BASE_NAME%.pdf
    start "" "%BASE_NAME%.pdf" :: Automatically open the generated PDF
    goto end

    :: Error Handling
    :error_handling
    set /a ATTEMPTS+=1
    if %ATTEMPTS% lss %MAX_ATTEMPTS% (
    echo.
    echo Attempting to fix the issue (attempt %ATTEMPTS%/3)...
    goto compile
    )

    echo.
    echo =============== Compilation Failed ===============
    echo After %MAX_ATTEMPTS% attempts, it has not been successful. Please check the logs:
    type "%BASE_NAME%.log" | more
    goto end

    :end
    pause
  3. 将文件放入主文件(.tex)所在文件夹中,拖动主文件到该脚本上,或者命令行运行:compile.bat main.tex

注意事项:TEX_COMPILER可更换为pdflatexlualatex编译命令,且删除辅助文件的命令可选择删除掉,避免每次都需要重新生成中间文件浪费时间。


Makefile

常规编译方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 定义编译器
LATEX = xelatex
# 定义需要清理的辅助文件扩展名
AUX_FILES = *.aux *.log *.out *.toc *.lof *.lot *.bbl *.blg *.synctex.gz *.fls *.fdb_latexmk *.run.xml *.nav *.snm *.vrb *.bcf *.idx *.ilg *.ind *.xdv

# 获取当前目录下所有 .tex 文件(排除带空格的文件名)
TEX_FILES = $(wildcard *.tex)
PDF_FILES = $(TEX_FILES:.tex=.pdf)

# 默认目标:编译所有 .tex 文件
all: $(PDF_FILES)
@echo "编译完成!"

# 模式规则:从 .tex 生成 .pdf
%.pdf: %.tex
$(LATEX) -interaction=nonstopmode -halt-on-error $<
#@# 如果有参考文献,运行 biber 或 bibtex
#@if [ -f $(basename $<).bcf ]; then biber $(basename $<); fi
#@if [ -f $(basename $<).aux ]; then bibtex $(basename $<); fi
@# 第二次编译确保交叉引用正确
$(LATEX) -interaction=nonstopmode -halt-on-error $<
#@# 第三次编译确保所有引用稳定
#$(LATEX) -interaction=nonstopmode -halt-on-error $<

# 清理所有辅助文件
clean:
@echo "正在清理辅助文件..."
@rm -f $(AUX_FILES)
@echo "清理完成!"

# 编译并清理(常用目标)
build: all clean

.PHONY: all clean build