title: Problems interpreting input cell box expressions
body: <p>How do I convert an arbitrary box specification extracted from a cell expression into an input expression?</p>
  
  <p>This came up as a problem with my answer to <a href="https://stackoverflow.com/q/8282605/211232">Save Mathematica code in <code>FullForm</code> syntax</a>.  In that context, pattern matching was being used to extract box specifications from notebook expressions read using <code>Import</code>.</p>
  
  <p>I thought that <code>ToExpression</code> or <code>MakeExpression</code> would do the job of box interpretation, but in some circumstances they do not.</p>
  
  <p>Consider an input cell that contains the expression:</p>
  
  <pre><code>StringForm["a = ``", 1]
  </code></pre>
  
  <p>The cell expression for such a cell looks like this:</p>
  
  <pre><code>Cell[BoxData[
   RowBox[{"StringForm", "[", 
    RowBox[{"\"\&lt;a = ``\&gt;\"", ",", " ", "1"}], "]"}]], "Input"]
  </code></pre>
  
  <p>I can take the <code>BoxData</code> subexpression from this cell and use <code>ToExpression</code> to obtain the same output as if I had evaluated the original cell:</p>
  
  <pre><code>ToExpression @
    BoxData[
      RowBox[{"StringForm", "[", 
        RowBox[{"\"\&lt;a = ``\&gt;\"", ",", " ", "1"}], "]"}]]
  </code></pre>
  
  <p>But now consider the following input expression:</p>
  
  <p><code>StringForm["<em>a</em> = ``", 1]</code></p>
  
  <p>You'll have to look closely to see the difference: the <code><em>a</em></code> is in italics.  Here is the corresponding cell expression:</p>
  
  <pre><code>Cell[BoxData[
   RowBox[{"StringForm", "[", 
    RowBox[{"\"\&lt;\!\(\*
  StyleBox[\"a\",
  FontSlant-&gt;\"Italic\"]\) = ``\&gt;\"", ",", " ", "1"}], "]"}]], "Input"]
  </code></pre>
  
  <p>If I evaluate this cell normally, I get the expected result.  But if I try applying <code>ToExpression</code> to the <code>BoxData</code> subexpression like before:</p>
  
  <pre><code>ToExpression @
    BoxData[
     RowBox[{"StringForm", "[", 
      RowBox[{"\"\&lt;\!\(\*
    StyleBox[\"a\",
    FontSlant-&gt;\"Italic\"]\) = ``\&gt;\"", ",", " ", "1"}], "]"}]]
  </code></pre>
  
  <p>an error occurs:</p>
  
  <pre><code>StringForm::string : String expected at position 1 in StringForm[]\) = '',
  FontSlant-&gt;"\~\(\*\nStyleBox["a Italic, 1].
  </code></pre>
  
  <p>The same kind of error occurs for many, if not all, of the inline string box escape sequences.  I've tried explicitly specifying the form to <code>ToExpression</code> and <code>MakeExpression</code>, but I get the same error.  Which brings me to my question...</p>
  
  <p>What do I have to do to emulate the way Mathematica interprets the boxes from an input cell expression?</p>