Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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];
share|improve this question

3 Answers 3

up vote 3 down vote accepted

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[].

share|improve this answer
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. –  Markus Roellig Jun 13 '12 at 11:02
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?"}. –  kguler Jun 13 '12 at 13:18
@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. –  jVincent Jun 13 '12 at 13:24
@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? –  Markus Roellig Jun 13 '12 at 13:39
@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. –  jVincent Jun 13 '12 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

share|improve this answer
I don't think this was asked for but +1 for effort! –  Mr.Wizard Jun 13 '12 at 12:00
@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"]]. –  kguler Jun 13 '12 at 12:38
You're right, I didn't notice that because I often use a custom style. I need to look at this again. –  Mr.Wizard Jun 13 '12 at 13:08
Okay, if this were my question I'd accept this answer. –  Mr.Wizard Jun 13 '12 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]
share|improve this answer
Thanks for the reply. However, I think I can't use your suggestion directly. See my edit above. –  Markus Roellig Jun 13 '12 at 10:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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