# Can anyone explain this strange behavior of InputForm?

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=
\[Pi]

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

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