# Can anyone explain this strange behavior of InputForm?

Bug introduced in 3 or earlier, persisting through 12, fixed on Wolfram cloud only.

In Mathematica 9.0.0, when to display the input form of two numbers, I come across maybe a puny problem. In order to focus on the problem, I will put up a simplified version.

In[1]:=InputForm[E]
InputForm[Pi]


and the output is

Out[2]//InputForm=
Pi


The input form of E is eaten by the Pi one. Why would this happen? And if I add a semicolon to the later one, the input form of E will show up. And what's more, if I enter InputForm and StandardForm consecutively like this

In[7]:=InputForm[E]
StandardForm[Pi]


I get

Out[8]//StandardForm=
π

E


the E being placed in a separate cell without the InputForm label. IMO, the output is made in the wrong sequence.

So my question is, what does InputForm really do? Why does it "eat" the previous output?

• I don't know what is going on. You are presenting a rather pathological way of entering expressions. However, if you place your inputs within parentheses, Mathematica will treat them more in the manner you seem to expect. That is,(InputForm[E]\nStandardForm[Pi]) will produce E π. In this case, Mathematica is treating the input as Times[InputForm[E], StandardForm[Pi]]. I admit I think your input should be treated as multiplcation, too. – m_goldberg Mar 22 '13 at 15:26
• I've observed the same behavior in version 7 but I've never really questioned it. I just assumed that only one formatting wrapper should be used. +1 for inquisitiveness. – Mr.Wizard Mar 22 '13 at 15:26
• @m_goldberg Unfortunately this is not correct. The Mathematica parser is a bit special when it parses several lines of code which are not separated by a ;. If the first line is a complete expression then the line break is not treated as multiplication. Instead it is treated as it were separated by ;. – halirutan Mar 22 '13 at 16:01
• @m_goldberg It's of course not always treated as multiplication. The parser just tries to read more input until the expression is complete. In your case the line break was considered as multiplication. – halirutan Mar 22 '13 at 16:08
• This is really an old bug, it's introduced at least since v3!: i.stack.imgur.com/mvZK2.png – xzczd Dec 7 '19 at 12:59

I consider this a bug in the front end. Very odd it is, that not all forms eat up the first result. Consider this simple example

m = {1};
MatrixForm@m
MatrixForm@m


which gives 2 outputs as expected. If we look on the traffic between kernel and front end, then we see, that the kernel indeed sends 2 outputs back. No matter which kind of form we use:

FE <--- K: OutputNamePacket["Out[3]//TeXForm= "]
FE <--- K: ReturnInputFormPacket["\\{1\\}"]
FE <--- K: OutputNamePacket["Out[4]//TeXForm= "]
FE <--- K: ReturnInputFormPacket["\\{1\\}"]


Unfortunately, when using TeXForm or InputForm or CForm (I haven't tried all) the first (!) output cell is always lost. This can be seen, if you watch the In[] and Out[] numbers. For In[n] you get Out[n+1], where it should be both Out[n] and Out[n+1].

If you don't have LinkSnooper set up like I have, you can set $Post to see, that really two outputs are created but they are just not displayed. Try this $Post = (Print[#]; #) &
InputForm@m
InputForm@m

• Thanks a lot! That's really helpful. – luyuwuli Mar 23 '13 at 5:40

I suspect that this has something to do with the code that produces the InputForm cell tags, or rather tagged cells. In addition to the last tagged cell replacing the prior ones you can observe strange behavior when combining CellPrint and InputForm:

InputForm[1 // CellPrint]
InputForm[2 // CellPrint]
InputForm[3 // CellPrint]

1

Out[1]//InputForm= 2

Out[2]//InputForm= 3

Out[3]//InputForm=
Null


The actual return from CellPrint is Null, so the fact that 2 and 3 end up on an InputForm-tagged cell may be a bit of a surprise.

You can also observe that the InputForm-tagged cells are not produced in the normal output process as they end up at the bottom, and further observe that the tagging is missing in this case:

1
2
InputForm[3]
InputForm[4]
5

Out[1]= 1

Out[2]= 2

Out[5]= 5

4

• Yes, the problem is strange, although it is not practically useful. – luyuwuli Mar 23 '13 at 6:06

As a workaround, you can proceed in a fashion similar to my answer to Multiple TeXForm returns just single output:

InputForm[E] //SequenceForm
InputForm[Pi] //Defer


E

Pi

It is possible to create a new *Form so that the form wrapper gets stripped from Out, as I describe in my answer to How to make the result of InputForm balance the bracket:

Unprotect[$OutputForms]; AppendTo[$OutputForms, myInputForm];
Protect[$OutputForms]; myInputForm /: MakeBoxes[myInputForm[expr_], StandardForm] := MakeBoxes[ InputForm[expr] ]  Then: $Line=0;

InputForm[E] //SequenceForm
Row[{a, b}] //myInputForm
SparseArray[{}, 3] //myInputForm


E

Row[{a, b}]

SparseArray[Automatic, {3}, 0, {1, {{0, 0}, {}}, {}}]

Let's check the FullForm of the outputs:

FullForm[{Out[1], Out[2], Out[3]}]


List[SequenceForm[InputForm[E]],Row[List[a,b]],SparseArray[Automatic,List[3],0,List[1,List[List[0,0],List[]],List[]]]]

As you can see, the myInputForm wrapper does get stripped, unlike the first example. Using the myInputForm wrapper also allows you to use double click to select parts of the expression, unlike the usual InputForm wrapper.