I try to interact with mathematica using MathLink for symbolic computations. By default it returns strings such as 2\012-1 + E\012-------\012 3\012 E if the input is Integrate[Exp[-x],{x,1,3}]. For better usability I would like it to print all output in InputForm which I could achieve by wrapping all input by a InputForm[] command (would this be bullet-proof for all possible input?). However it would be more elegant if I could define this once for all at the beginning of a session. Is there a way to do this? Furthermore the output seems to include the Out[#] labels which I know from Notebook files. I would prefer to suppress it as well. Obviously I could do it by cutting it manually from the returned string but there might be an elegant solution as well.

  • $\begingroup$ You could use $Post or you could transfer structured expressions through MathLink instead of strings. Transferring the expressions directly instead of their string-representation is bullet-proof. What are you using the output for? Is the consumer a human reader or a computer program? $\endgroup$
    – Szabolcs
    Feb 8, 2014 at 15:51
  • $\begingroup$ It turns out that defining $Post produces two lines of output for each command like ` a=1 Out[7]//InputForm= 1 Out[8]//InputForm= , which I would want to avoid. I wrote a little frontend with MathLink in C starting from the publicly available frontend.c` which pipes the output into vim, so the consumer is me, but I prefer the InputForm over pseudo math equations in ascii-art. $\endgroup$
    – highsciguy
    Feb 8, 2014 at 16:19
  • $\begingroup$ So I would probably append // InputForm to each block of input, but the input might consist of multiple lines and several commands. To parse it beforehand would be too difficult. To cut the produced Out[1]//InputForm= by hand would not be elegant, but surely possible. $\endgroup$
    – highsciguy
    Feb 8, 2014 at 16:25
  • $\begingroup$ For the first part it helps to call once SetOptions["stdout", FormatType->InputForm]. But Out[#] still gets printed. $\endgroup$
    – highsciguy
    Feb 8, 2014 at 17:49
  • $\begingroup$ For me doing SetOptions["stdout", FormatType->InputForm] just gets me output like this: 1\n-\n2 (when doing e.g. Print[1/2]), which doesn't look like InputForm to me. I can't get the $Post thing working either, doing $Post=//InputForm and variants I only get syntax errors, except for $Post=InputForm; which doesn't seem to do anything. Maybe I'm doing it wrong? Anyway still cannot find how to achieve what the OP wants, so if someone knows would be great to get this question answered. $\endgroup$
    – Ben Farmer
    Feb 10, 2016 at 9:21


Your Answer

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

Browse other questions tagged or ask your own question.