当前位置: 首页 > news >正文

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

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


批处理文件

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

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

步骤如下:

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

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

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

    @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 namepauseexit /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 failedgoto 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 failedgoto 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 failedgoto 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

常规编译方法

# 定义编译器
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
http://www.wxhsa.cn/company.asp?id=203

相关文章:

  • 我们一起“扒一扒”ReentrantLock:看看锁背后那些精妙的设计
  • win10使用openssl生成证书
  • $\LaTeX{}$之minted使用 - Invinc
  • linux服务器 系统服务文件
  • Codeforces Round 1049 (Div. 2) 部分题解
  • Critical Thinking Academic Writing
  • 1.3 课前问题思考
  • 【知识管理工具分享】基于AI搭建个人法律知识库:我的PandaWiki实践心得
  • 你的中间件一团糟-是时候修复它了-️
  • 超越-env-一份成熟的应用程序配置指南
  • 告别框架臃肿-我如何在不牺牲性能的情况下重新发现简单之美
  • 像元大小(例如 1.4 m 1.4 m)具体的含义和用途
  • Codeforces Round 1049 (Div. 2) 一些 idea
  • 医学如果不追求深入的话,其实门槛没有特别高
  • Canvas 的性能卓越,用它解决一个棘手的问题!
  • CSS Box-Sizing 详解:解决移动端布局溢出问题的关键
  • Visual Studio Code 开发环境搭建(Rust)
  • Spring Boot 项目中,同一个版本的依赖,内容却不一样?一次因依赖污染导致 Redis 启动失败的排查
  • 微信机器人开发文档
  • 从0到1:餐饮微信点餐小程序源码解析(含扫码点餐+外卖系统+后台管理)
  • 推荐一款线程or进程间数据同步解决方案
  • part 2
  • Apache服务器自动化运维与安全加固脚本详解
  • 无障碍资源导航
  • The 2022 ICPC Asia Shenyang Regional Contest
  • 还在微信群追问任务进展?领歌看板让逾期工作无处可藏
  • 别再猜了-开始测量吧-一份实用的Web性能指南
  • 你的开发服务器在说谎-热重载与热重启的关键区别
  • 大屏开发
  • 检测域名证书有效期