I am relatively new to bash scripting and have no experience with LaTeX. I've been asked to develop a script which will replace convenience shortcuts in LaTeX documents with their more cumbersome long-form equivalents.
My approach thus far has been to isolate both the shortcut and the long-form in separate variables and then try to replace them in the text by using sed. I've attached short example files below.
As it is currently the script takes 2 arguments, a file expr from which it retrieves the shortcuts and long-form terminology and an infile to which is is supposed to make the appropriate changes. I know that the script is properly isolating both the shortcuts and long-forms and can return them, but it can't seem to execute the sed command.
I have tried searching this online and found multiple similar question where the suggestion was that sed has difficultly recognizing variable and that various type of quotation combinations might solve the problem. I have tried many permutations and none appear to work. The long-form terminologies in many cases contain special characters such as '$' and '{}', so I suspect that this might be the issue but I'm not sure. I am also very much open to other ideas about how to solve the problem. Please find below samples of both the script and the 2 argument files, expr and infile.
expr file containing shortcuts and long-forms
% 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$}
infile containing shortcuts to be replaced by long-forms
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}
Relevant subsection of script called with expr and infile as arguments
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}
UPDATE: For clarification, this is what the desired result would be, the shortcuts present in infile would be substituted with the appropriate long-form
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
This is my test {$^{18}$O}code
{$^{18}$O/$^{16}$O} my test is thiscode
Does it work {\begin{equation}}code
{\begin{eqnarray}} test test testcode
work work work {C$_3$}code
{C$_4$} This is my testcode
my test is this {CO$_2$} – Twitter