施婉秀 发表于 2025-10-1 13:38:05

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

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

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

步骤如下:

[*]新建文本文件命名为clean.bat;
[*]复制下面的代码放到文本文件中;
@echo off
echo Cleaning auxiliary files...
del /s /q "*.aux" "*.log" "*.out" "*.bbl" "*.blg" "*.toc" "*.lof" "*.lot" "*.synctex.gz"
echo Cleaning completed!
pause
[*]将文件放入主文件(.tex)所在文件夹中,双击运行即可删除中间文件以及子文件夹中的中间文件。
快速编译并删除中间文件

步骤如下:

[*]新建文本文件命名为compile.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 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 %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 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 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
[*]将文件放入主文件(.tex)所在文件夹中,拖动主文件到该脚本上,或者命令行运行:compile.bat main.tex。
注意事项:TEX_COMPILER可更换为pdflatex或lualatex编译命令,且删除辅助文件的命令可选择删除掉,避免每次都需要重新生成中间文件浪费时间。
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 $

荡俊屯 发表于 2025-11-27 07:11:37

热心回复!

阎一禾 发表于 2025-12-24 20:45:12

前排留名,哈哈哈

谧怏弦 发表于 2025-12-27 23:57:39

新版吗?好像是停更了吧。

碣滥 发表于 2026-1-6 09:40:26

这个好,看起来很实用

轨项尺 发表于 2026-1-11 09:00:28

东西不错很实用谢谢分享

蒙飘 发表于 2026-1-14 07:37:36

感谢分享,下载保存了,貌似很强大

髡芯 发表于 2026-1-16 09:19:07

收藏一下   不知道什么时候能用到

诀锺 发表于 2026-1-21 10:35:14

感谢分享,下载保存了,貌似很强大

轮达 发表于 2026-1-21 23:38:03

yyds。多谢分享

簑威龙 发表于 2026-1-25 12:16:59

过来提前占个楼

赖秀竹 发表于 2026-1-26 04:49:21

新版吗?好像是停更了吧。

郗新语 发表于 2026-1-26 10:11:15

这个有用。

奚娅琼 发表于 2026-1-27 07:19:22

不错,里面软件多更新就更好了

欧阳梓蓓 发表于 2026-1-29 02:49:27

用心讨论,共获提升!

宗和玉 发表于 2026-1-29 05:02:27

谢谢楼主提供!

喜及眩 发表于 2026-2-3 06:26:45

东西不错很实用谢谢分享

琉艺戕 发表于 2026-2-5 06:10:08

感谢分享,学习下。

葛雅隽 发表于 2026-2-8 01:35:55

东西不错很实用谢谢分享

喳谍 发表于 2026-2-9 07:05:16

这个好,看起来很实用
页: [1] 2
查看完整版本: $\LaTeX{}$之快速编译和删除中间文件