我相对于 bash 脚本编程比较新手,没有 LaTeX 的经验。我被要求开发一个脚本,用来将 LaTeX 文档中的快捷方式替换为它们更繁琐的长式等效语句。
到目前为止,我的方法是将快捷方式和长式分别隔离在不同的变量中,然后尝试使用 sed 在文本中替换它们。我在下面附上了简短的示例文件。
当前脚本需要 2 个参数:一个是 expr 文件,从中获取快捷方式和长式术语,另一个是输入文件,需要进行适当更改。我知道脚本正在正确地隔离快捷方式和长式,并且可以返回它们,但似乎无法执行 sed 命令。
我尝试在互联网上搜索相关信息,并找到了多个类似的问题,建议使用引号组合以解决 sed 变量识别困难的问题。我尝试了许多排列组合,但都不起作用。在许多情况下,长式术语包含特殊字符,例如 '$' 和 '{}',因此我认为这可能是问题所在,但我不确定。我也非常愿意听取其他解决问题的想法。请在下面找到脚本和 2 个参数文件 expr 和 infile 的示例。
expr 文件包含快捷方式和长式术语
% a
\newcommand{\ao}{$^{18}$O}
\newcommand{\aodso}{$^{18}$O/$^{16}$O}
% b
\newcommand{\bea}{\begin{equation}}
\newcommand{\beaa}{\begin{eqnarray}}
% c
\newcommand{\cthree}{C$_3$}
\newcommand{\cfour}{C$_4$}
\newcommand{\coz}{CO$_2$}
包含需要替换为长格式的快捷方式的输入文件
This is my test {\ao}
{\aodso} my test is this
Does it work {\bea}
{\beaa} test test test
work work work {\cthree}
{\cfour} This is my test
my test is this {\coz}
使用expr和infile作为参数调用的脚本相关子部分
while read line; do
if [[ $line == \newcommand* ]]; then
temp=${line#*\{}
sc=${temp%%\}*}
templf=${temp#*\{}
lf=${templf%\}}
#echo $sc, $lf
sed -i -e 's/${sc}/${lf}/g' ${infile}
fi
done < ${expr}
更新: 为了澄清,期望的结果是将infile中出现的快捷方式替换为相应的长格式。
This is my test {$^{18}$O}
{$^{18}$O/$^{16}$O} my test is this
Does it work {\begin{equation}}
{\begin{eqnarray}} test test test
work work work {C$_3$}
{C$_4$} This is my test
my test is this {CO$_2$}
code
这是我的测试{$^{18}$O}code
{$^{18}$O/$^{16}$O}我的测试就是这个code
它是否有效{\begin{equation}}code
{\begin{eqnarray}}测试测试测试code
工作工作工作{C$_3$}code
{C$_4$}这是我的测试code
我的测试就是这个{CO$_2$} - user2460253