编写Shell脚本格式化LaTeX文件:为公式环境加空行与\noindent
LaTeX格式自动化整理Shell脚本优化
需求与问题
需要编写Shell脚本实现LaTeX文件的两项格式化:
- 为所有
equation和align环境前后各添加一个空行,且重复运行脚本时不会重复添加空行 - 在
equation/align环境结束后的段落开头添加\noindent命令
现有脚本仅实现了部分空行添加功能,但存在重复添加空行的问题,且未完成\noindent的添加逻辑。
现有脚本
# Check if a filename is provided if [ "$#" -ne 1 ]; then echo "Usage: $0 filename" exit 1 fi # Check if the file exists if [ ! -f $1 ]; then echo "File $1 not found!" exit 1 fi # Process the file awk ' BEGIN { RS = "\n" ORS = "\n" prev_line = "" prev_prev_line = "" } { if ($0 ~ /\\begin\{equation\}|\\begin\{align\}/) { if (prev_line !~ /^$/ && prev_prev_line !~ /\\begin\{equation\}|\\begin\{align\}/) { print "\n" $0 } else { print $0 } } else if ($0 ~ /\\end\{equation\}|\\end\{align\}/) { if (prev_line !~ /^$/ && prev_prev_line !~ /\\end\{equation\}|\\end\{align\}/) { print $0 "\n" } else { print $0 } } else { print $0 } prev_prev_line = prev_line prev_line = $0 }' $1 > $1.tmp # Replace the original file with the processed file mv $1.tmp $1
改进后的脚本
通过跟踪环境状态、检查空行存在性、处理\noindent插入逻辑,解决重复添加和命令插入问题:
#!/bin/bash # 检查参数数量 if [ "$#" -ne 1 ]; then echo "Usage: $0 filename" exit 1 fi # 检查文件存在 if [ ! -f "$1" ]; then echo "File $1 not found!" exit 1 fi # 使用awk处理文件 awk ' BEGIN { in_env = 0 # 是否处于equation/align环境中 after_env = 0 # 是否刚结束环境 ORS = "\n" } # 匹配环境开始 /^\\begin\{(equation|align)\}/ { # 如果前一行不是空行,先打印空行 if (prev_line !~ /^$/ && !in_env) { print "" } print $0 in_env = 1 after_env = 0 prev_line = $0 next } # 匹配环境结束 /^\\end\{(equation|align)\}/ { print $0 in_env = 0 after_env = 1 prev_line = $0 next } # 处理非环境行 { # 刚结束环境的情况:跳过空行,插入\noindent(如果不存在) if (after_env) { if ($0 ~ /^$/) { print $0 prev_line = $0 next } if ($0 !~ /^\\noindent/) { print "" print "\\noindent" } after_env = 0 } # 处理环境开始后的空行(避免重复) if (!in_env && prev_line ~ /^\\begin\{(equation|align)\}/ && $0 !~ /^$/) { print "" } print $0 prev_line = $0 } ' "$1" > "$1.tmp" # 替换原文件 mv "$1.tmp" "$1"
效果验证
原LaTeX内容
To simplify the equation, we can assume that the perturbation of the Lagrangian displacement is proportional to a spherical harmonic function, $Y_{\ell m}(\theta,\phi)$, where $\ell$ is the degree of the harmonic and $m$ is the azimuthal order. Then, we can write: \begin{equation} \xi(r,\theta,\phi) = \sum_{\ell=0}^{\infty}\sum_{m=-\ell}^{\ell} \xi_{\ell m}(r) Y_{\ell m}(\theta,\phi) \end{equation} Substituting this expression into the equation of motion, we obtain: \begin{equation} \frac{d}{dr}\left(\frac{\rho r^2}{P}\frac{dc_s^2}{dr}\frac{\xi_{\ell m}}{r}\right)-\frac{\omega^2\rho r^2\xi_{\ell m}}{G} = -\frac{\rho}{P}\frac{\partial}{\partial r}\left(r^2\frac{d\Phi}{dr}\right)S_{\ell m}(r) \end{equation}
转换后效果
To simplify the equation, we can assume that the perturbation of the Lagrangian displacement is proportional to a spherical harmonic function, $Y_{\ell m}(\theta,\phi)$, where $\ell$ is the degree of the harmonic and $m$ is the azimuthal order. Then, we can write: \begin{equation} \xi(r,\theta,\phi) = \sum_{\ell=0}^{\infty}\sum_{m=-\ell}^{\ell} \xi_{\ell m}(r) Y_{\ell m}(\theta,\phi) \end{equation} \noindent Substituting this expression into the equation of motion, we obtain: \begin{equation} \frac{d}{dr}\left(\frac{\rho r^2}{P}\frac{dc_s^2}{dr}\frac{\xi_{\ell m}}{r}\right)-\frac{\omega^2\rho r^2\xi_{\ell m}}{G} = -\frac{\rho}{P}\frac{\partial}{\partial r}\left(r^2\frac{d\Phi}{dr}\right)S_{\ell m}(r) \end{equation}
内容的提问来源于stack exchange,提问作者Mare Dedeu
相关产品推荐
相关产品推荐

