找回密码
 立即注册
首页 业界区 科技 $\LaTeX{}$之快速编译和删除中间文件

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

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

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

步骤如下:

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

步骤如下:

  • 新建文本文件命名为compile.bat;
  • 复制下面的代码放到文本文件中;
    1. @echo off
    2. :: ==============================================
    3. :: LaTeX Compile Automation Script (XeLaTeX + BibTeX)
    4. :: Usage: Drag and drop the .tex file onto this script or manually specify the file name
    5. :: ==============================================
    6. :: set variable
    7. set TEX_COMPILER=xelatex
    8. set BIB_COMPILER=bibtex
    9. set MAX_ATTEMPTS=3
    10. set LOG_EXTENSIONS=*.aux *.log *.out *.bbl *.blg *.toc *.lof *.lot *.synctex.gz
    11. :: Check whether the file is obtained by dragging
    12. if "%~1"=="" (
    13.     echo Error: Please drag the .tex file onto this script or manually specify the file name
    14.     pause
    15.     exit /b 1
    16. )
    17. :: Extract the file name (without extension)
    18. set "TEX_FILE=%~1"
    19. set "BASE_NAME=%~n1"
    20. :: Compile function definition
    21. :compile
    22. echo.
    23. echo =============== Start Compiling... ===============
    24. echo Compiling document: %TEX_FILE%
    25. :: First XeLaTeX Compilation
    26. echo.
    27. echo [1/4] First %TEX_COMPILER% compiling...
    28. %TEX_COMPILER% -interaction=nonstopmode -synctex=1 "%BASE_NAME%.tex"
    29. if %ERRORLEVEL% neq 0 (
    30.     echo Error: First %TEX_COMPILER% Compilation failed
    31.     goto error_handling
    32. )
    33. :: BibTeX Compilation
    34. echo.
    35. echo [2/4] %BIB_COMPILER% compiling reference...
    36. %BIB_COMPILER% "%BASE_NAME%.aux"
    37. if %ERRORLEVEL% neq 0 (
    38.     echo Warning: %BIB_COMPILER% There may be issues with the compilation (check the .blg file)
    39. )
    40. :: Second XeLaTeX Compilation
    41. echo.
    42. echo [3/4] Second %TEX_COMPILER% compiling...
    43. %TEX_COMPILER% -interaction=nonstopmode -synctex=1 "%BASE_NAME%.tex"
    44. if %ERRORLEVEL% neq 0 (
    45.     echo Error: Second %TEX_COMPILER% Compilation failed
    46.     goto error_handling
    47. )
    48. :: Third XeLaTeX Compilation (Ensure correct cross-referencing)
    49. echo.
    50. echo [4/4] Third %TEX_COMPILER% compiling...
    51. %TEX_COMPILER% -interaction=nonstopmode -synctex=1 "%BASE_NAME%.tex"
    52. if %ERRORLEVEL% neq 0 (
    53.     echo Error: Third %TEX_COMPILER% Compilation failed
    54.     goto error_handling
    55. )
    56. :: Cleaning auxiliary files (Optional)
    57. echo.
    58. echo Cleaning auxiliary files...
    59. del /s /q %LOG_EXTENSIONS% 2>nul
    60. :: Completed Successfully
    61. echo.
    62. echo =============== Compilation Completed Successfully ===============
    63. echo Final output file: %BASE_NAME%.pdf
    64. start "" "%BASE_NAME%.pdf"  :: Automatically open the generated PDF
    65. goto end
    66. :: Error Handling
    67. :error_handling
    68. set /a ATTEMPTS+=1
    69. if %ATTEMPTS% lss %MAX_ATTEMPTS% (
    70.     echo.
    71.     echo Attempting to fix the issue (attempt %ATTEMPTS%/3)...
    72.     goto compile
    73. )
    74. echo.
    75. echo =============== Compilation Failed ===============
    76. echo After %MAX_ATTEMPTS% attempts, it has not been successful. Please check the logs:
    77. type "%BASE_NAME%.log" | more
    78. goto end
    79. :end
    80. pause
    复制代码
  • 将文件放入主文件(.tex)所在文件夹中,拖动主文件到该脚本上,或者命令行运行:compile.bat main.tex。
注意事项:TEX_COMPILER可更换为pdflatex或lualatex编译命令,且删除辅助文件的命令可选择删除掉,避免每次都需要重新生成中间文件浪费时间。
Makefile

常规编译方法

[code]# 定义编译器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 $

相关推荐

您需要登录后才可以回帖 登录 | 立即注册