$\LaTeX{}$之快速编译和删除中间文件

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


批处理文件

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

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

步骤如下:

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

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

    bat 复制代码
    @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. 复制下面的代码放到文本文件中;

    bat 复制代码
    @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

常规编译方法

makefile 复制代码
# 定义编译器
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
相关推荐
Invinc-Z6 小时前
$\LaTeX{}$之图片使用
latex
Time_Memory_cici1 天前
WinEdt编译tex文件失败解决办法
latex
喝凉白开都长肉的大胖子1 个月前
latex中“itemize”
latex
cxylay1 个月前
LaTeX(排版系统)Texlive(环境)Vscode(编辑器)环境配置与安装
ide·vscode·编辑器·latex·texlive
米饭的白色1 个月前
vscode/trae 的 settings.json 中配置 latex 的一些记录
vscode·json·latex
Chensf20211 个月前
用latex+vscode+ctex写毕业论文
vscode·论文·latex·ctex
nlp研究牲1 个月前
latex中既控制列内容位置又控制列宽,使用>{\centering\arraybackslash}p{0.85cm}
服务器·前端·人工智能·算法·latex
山登绝顶我为峰 3(^v^)32 个月前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
秋窗72 个月前
Mac 部署Latex OCR并优化体验(打包成App并支持全局快捷键)
macos·ocr·latex