title: Using sed in bash script to replace LaTeX aliases
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>expr file containing shortcuts and long-forms</p>
  
  <pre class="lang-latex prettyprint-override"><code>% 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$}
  </code></pre>
  
  <p>infile containing shortcuts to be replaced by long-forms</p>
  
  <pre class="lang-latex prettyprint-override"><code>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}
  </code></pre>
  
  <p>Relevant subsection of script called with expr and infile as arguments </p>
  
  
  
  <pre><code>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 &lt; ${expr}
  </code></pre>
  
  <p>UPDATE:
  For clarification, this is what the desired result would be, the shortcuts present in infile would be substituted with the appropriate long-form</p>
  
  <pre class="lang-latex prettyprint-override"><code>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></pre>