Consider the following command:

TextCell["\!\(\*SubscriptBox[\(H\), \(2\)]\)", "Section"]

How can I display the string as section header and get the DisplayForm? Right now it writes \!\(\*SubscriptBox[\(H\), \(2\)]\) as section header which hurts my eyes.


To give a little more background. I have a long list of Strings with sub- and superscripts (typed in using keyboard shortcuts), i.e. explicitely not using Superscript[..] etc.. Now I want to create a notebook with one section per list entry and the string should go into the section title with proper display of super/subscripts.

Here is a hopefully better example:

I create a list of section headers. For A_1 I type A <Ctrl>+_ 1, etc. Then I want to create sections from the list. The code copied as input text:

headers = {"\!\(\*SubscriptBox[\(A\), \(1\)]\)", 
   "\!\(\*SubscriptBox[\(A\), \(2\)]\)", 
   "\!\(\*SubscriptBox[\(A\), \(3\)]\)"};
CellPrint[TextCell[#, "Section"]] & /@ headers

Edit 2:

Using ToExpression almost does the job. I am using the following code. Note that ToExpression scrambles the order of the characters in the header strings. Any idea on how to improve?

headers = {"\!\(\*SubscriptBox[\(B\), \
    \(1\)]\)\!\(\*SuperscriptBox[\(A\), \(2\)]\)", 
      "\!\(\*SubscriptBox[\(B\), \(1\)]\)\!\(\*SuperscriptBox[\(A\), \(3\
    \)]\)", "\!\(\*SubscriptBox[\(B\), \
    \(1\)]\)\!\(\*SuperscriptBox[\(A\), \(5\)]\)"};
       TextCell[ToExpression[i], "Section"],
       ExpressionCell[Plot[Sin[x], {x, 0, 6}]]
      , {i, headers}]
    ,{TextCell["Title", "Title"], 
     TextCell["created:" <> ToString[DateString[]], "Subtitle"]}]
Visible -> True];

3 Answers 3


I'm not certain, but pehaps this would work for you:

 TextCell[ToExpression@"\!\(\*SubscriptBox[\(H\), \(2\)]\)", "Section"]

For your example that would be

 CellPrint[TextCell[ToExpression@#, "Section"]] & /@ headers

While the above works, depending on what you are doing, it may not be what you actually want. For example

 TextCell[ToExpression@"\!\(\*SubscriptBox[\(H\), \(2\)]\)", "Section"]

Would print a title header with newsymbolname subscript 2, rather then H. If what you want is to just to render the displayform of the boxexpressions you are working with, the propper way would be to tell Mathematica that you giving it BoxData, thus as Mr. Wizard wrote:

 CellPrint@Cell[BoxData@"\!\(\*SubscriptBox[\(H\), \(2\)]\)", "Section"]

A great way to see the difference is to select a cell, in this case the generated header cell, and go to cell>Show Cell Expression (alternatively ctrl-shift-E). This will show you what the boxform ends up looking like.

In these cases, your original aproach would be:

CellPrint@Cell["\!\(\*SubscriptBox[\(H\), \(2\)]\)", "Section"]

 Cell["\!\(\*SubscriptBox[\(H\), \(2\)]\)", "Section",...]

My stated approach has a lot of structure because it's not just taking the string "H" but the symbol, the main gest of the output is that it needs an interpretation box to tell you that it's showing one thing, but that it's interpreted in another:

    SubscriptBox["H", "2"]], "Section"],
   Subscript[$CellContext`H, 2], "Section"]]], "Output"]

And lastly, if you just want to tell Mathematica to correctly interpret the boxdata shortforms, you have:

 CellPrint@Cell[BoxData@"\!\(\*SubscriptBox[\(H\), \(2\)]\)", "Section"]     
 Cell[BoxData[SubscriptBox["H", "2"]], "Section"]

An important thing to keep in mind when editing documents is that things such as TextCell, ExpressionCell and CellGroup are not low level structures, so for example CellGroup will not take Cell as argument. The stated example can be printed with:

 Cell["Title", "Title"],
 Cell["created:" <> ToString[DateString[]], "Subtitle"]
 }, Table[Cell@CellGroupData[
  {Cell[BoxData[i], "Section"], 
   Cell[{ToBoxes@Plot[Sin[x], {x, 0, 6}]}]}
  , Open], {i, headers}]
 , Visible -> True, CellGrouping -> Manual];

Remember that you need CellGrouping -> Manual if you want to control cell grouping yourself. Also quickly checked the nb structure and found out that the proper low-level structure to control cell grouping is CellGroupData[].

  • $\begingroup$ That works, thank you. It is counter-intuitive though to convert a string to an expression before putting it into a section title - never would have thought of that. $\endgroup$ Jun 13, 2012 at 11:02
  • $\begingroup$ CellPrint[TextCell[ToExpression@#, "Section"]] & /@ headers prints three cells (which, btw, do not match the fontstyle of Section), followed by {Null, Null,Null}. Now, try this method with the following list of strings for section headers headers2 = {"section one all mixed up ", "What is going on", "Plot[Sin[x],{x,-Pi,Pi}]","What's going on?"}. $\endgroup$
    – kglr
    Jun 13, 2012 at 13:18
  • $\begingroup$ @kguler It prints the 3 cells correctly, if you don't want the {Null,Null,Null}, you need to add ";" to suppress the output. CellPrint outputs Null. And they do inherit any styling from Section, however symbols don't get the fontstyle. As for the "unexpected" results, they are exactly what you'd expect from evaluating the expressions (which what I thought OP needed, until I saw his comment). If you only want to extract the boxdata structure, I added some examples to my answer. $\endgroup$
    – jVincent
    Jun 13, 2012 at 13:24
  • $\begingroup$ @jVincent Thanks! It seems like BoxData is the way to go. However, if use CreateNotebook[CellGroup[{CellPrint[..], ExpressionCell[...]}], the output of CellPrint appears in the parent notebook. Any idea on how to redirect it to the newly created one? $\endgroup$ Jun 13, 2012 at 13:39
  • 1
    $\begingroup$ @MarkusRoellig See the latest addition. It's somewhat confusing where the borders between high level functions like TextCell[] and lower level structures like Cell really is. Personally I find it easier to just write the low-level structure, since then I know what the output will look like. $\endgroup$
    – jVincent
    Jun 13, 2012 at 17:40

Perhaps something like the following?

data = {"Section heading \!\(\*SubscriptBox[\(H\), \(X\)]\)", 
"Subsection \!\(\*SubsuperscriptBox[\(H\), \(a\), \(b\)]\)", 
"a subsection with section style \!\(\*SubsuperscriptBox[\(H\), \(a\), \(b\)]\)", 
"... some text", 
"and a section with subsection style \!\(\*SuperscriptBox[\(S\), \(Y\)]\)"};
Notebook[{Cell[StyleBox[#1, "Section"], "Section"], 
 Cell[StyleBox[#2, "Subsection"], "Subsection"], 
 Cell[StyleBox[#3, "Subsection"], "Section"], 
 Cell[#4, "Text"], 
 Cell[StyleBox[#5, "Section"], "Subsection"]}]] &[Sequence @@ data]


enter image description here

EDIT: to avoid the issues caused by ToExpression mentioned in OP's edit, one can use:

   {Cell[StyleBox[i, "Section"], "Section"], 
    Cell[BoxData@ToBoxes@Plot[Sin[x], {x, 0, 6}, ImageSize -> 200], "Output"]},
  {i, headers}], 
  {Cell["Title", "Title"], 
   Cell["created:" <> ToString[DateString[]], "Subtitle"]}], 1]]]


  {TextCell[String[i, "Section"], "Section"], 
   ExpressionCell[Plot[Sin[x], {x, 0, 6}, ImageSize -> 200],"Output"]}],
  {i, headers}], 
  {TextCell["Title", "Title"], 
  TextCell["created:" <> ToString[DateString[]], "Subtitle"]}], 1],
Visible -> True];

both give

enter image description here

  • $\begingroup$ I don't think this was asked for but +1 for effort! $\endgroup$
    – Mr.Wizard
    Jun 13, 2012 at 12:00
  • $\begingroup$ @Mr.Wizard,thank you for the vote. From OP's edit: "... Now I want to create a notebook with one section per list entry and the string should go into the section title ..." . I thought this was meant to include all styling features of Section. Now, when I try CellPrint[Cell[BoxData@#, "Section"] & /@ headers]] or NotebookPut[Notebook[Cell[BoxData@#, "Section"] & /@ headers]] the text style does not match the Section style (style you get with CellPrint[Cell["plain section", "Section"]]. $\endgroup$
    – kglr
    Jun 13, 2012 at 12:38
  • $\begingroup$ You're right, I didn't notice that because I often use a custom style. I need to look at this again. $\endgroup$
    – Mr.Wizard
    Jun 13, 2012 at 13:08
  • 1
    $\begingroup$ Okay, if this were my question I'd accept this answer. $\endgroup$
    – Mr.Wizard
    Jun 13, 2012 at 13:19

Starting with:

headers = {"\!\(\*SubscriptBox[\(A\), \(1\)]\)", 
   "\!\(\*SubscriptBox[\(A\), \(2\)]\)", 
   "\!\(\*SubscriptBox[\(A\), \(3\)]\)"};

If you don't need the parenthesis use simply:

CellPrint[Cell[BoxData@#, "Section"] & /@ headers]
  • $\begingroup$ Thanks for the reply. However, I think I can't use your suggestion directly. See my edit above. $\endgroup$ Jun 13, 2012 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.