3
$\begingroup$

In mathematica-output how can one removes the leading spaces from each line of the output.

Here is a sample code

SetDirectory[Directory[]] 
result=(4*(5*y^7*(-1 + 4*z) + y^6*(15 - 83*z + 80*z^2) - 5*z^4*(-2+4*z
 - 3*z^2 + z^3) + y^5*(-20 + 147*z - 273*z^2 + 140*z^3) + y*z^3*(40 
 - 154*z + 147*z^2 - 83*z^3 + 20*z^4) + y^2*z^2*(60 - 254*z + 363*z^2 
 - 273*z^3 + 80*z^4) + y^3*z*(40 - 254*z + 462*z^2 - 439*z^3 + 140*z^4) 
 + y^4*(10 - 154*z + 363*z^2 - 439*z^3 + 160*z^4)) - 9*(y + z)^4*(2  
 + y^2*(3 - 9*z) - 4*z + 3*z^2 - z^3 + y^3*(-1 + 4*z) + y*(-4 + 12*z 
 - 9*z^2 + 4*z^3))*H[0, y] ); 
 result >> output.m;
 Quit[]

where the output is

cat output.m

(4*(5*y^7*(-1 + 4*z) + y^6*(15 - 83*z + 80*z^2) -
    5*z^4*(-2 + 4*z - 3*z^2 + z^3) + y^5*(-20 + 147*z - 273*z^2 + 140*z^3) +
    y*z^3*(40 - 154*z + 147*z^2 - 83*z^3 + 20*z^4) +
    y^2*z^2*(60 - 254*z + 363*z^2 - 273*z^3 + 80*z^4) +
    y^3*z*(40 - 254*z + 462*z^2 - 439*z^3 + 140*z^4) +
    y^4*(10 - 154*z + 363*z^2 - 439*z^3 + 160*z^4)) -
  9*(y + z)^4*(2 + y^2*(3 - 9*z) - 4*z + 3*z^2 - z^3 + y^3*(-1 + 4*z) +
    y*(-4 + 12*z - 9*z^2 + 4*z^3))*H[0, y] )

from the 2nd line of the output there are 4 or 2 spaces in front of each line. For some larger expression this looks worse.
Is there any way to remove those leading spaces in the output in mathematica? Note: This is a question of beautification of the output, nothing wrong with the output, its still right. I just want to remove the leading spaces? Is there any smart way to output so that this problem does not appear?

$\endgroup$
5
  • 1
    $\begingroup$ Why are the spaces a problem? The output is still syntactically valid Mathematica code. Or perhaps it is not a Mathematica expression that you want to output? If so, please explain. $\endgroup$
    – Szabolcs
    Aug 13, 2016 at 16:17
  • $\begingroup$ what do you mean by "not a Mathematical expression" ! I have posted the result. Mathematica writes it as string I presume for the above mentioned command. The problem is it does not look nice. And when the expression is very big the leading spaces are not regular meaning in some lines there are 5 spaces in some other it has 2 spaces. Finally I want to put this expression in latex as it is but I don't want to use TexForm. It's just a question of beautification. $\endgroup$
    – BabaYaga
    Aug 13, 2016 at 16:22
  • $\begingroup$ He means "not a Mathematica expression that you want to output". $\endgroup$
    – Pirx
    Aug 13, 2016 at 19:59
  • $\begingroup$ What is in result? Where did it come from? $\endgroup$
    – m_goldberg
    Aug 13, 2016 at 21:33
  • $\begingroup$ @m_goldberg , the result is an expression.one can try to run this example SetDirectory[Directory[]] result= (4*(5*y^7*(-1 + 4*z) + y^6*(15 - 83*z + 80*z^2) - 5*z^4*(-2 + 4*z - 3*z^2 + z^3) + y^5*(-20 + 147*z - 273*z^2 + 140*z^3) + y*z^3*(40 - 154*z + 147*z^2 - 83*z^3 + 20*z^4) + y^2*z^2*(60 - 254*z + 363*z^2 - 273*z^3 + 80*z^4) + y^3*z*(40 - 254*z + 462*z^2 - 439*z^3 + 140*z^4) + y^4*(10 - 154*z + 363*z^2 - 439*z^3 + 160*z^4)) - 9*(y + z)^4*(2 + y^2*(3 - 9*z) - 4*z + 3*z^2 - z^3 + y^3*(-1 + 4*z) + y*(-4 + 12*z - 9*z^2 + 4*z^3))*H[0, y] ); result >> output.m; Quit[] $\endgroup$
    – BabaYaga
    Aug 14, 2016 at 1:35

1 Answer 1

2
$\begingroup$

This is fairly manual in the sense that one has to take charge of where the line breaks are to occur, but it will output a text file with the line breaks aligned on the left.

The strategy is to convert the expression to a string and then make a list of the string where line breaks are set at user specified expressions.

result=(4*(5*y^7*(-1 + 4*z) + y^6*(15 - 83*z + 80*z^2) - 5*z^4*(-2+4*z
 - 3*z^2 + z^3) + y^5*(-20 + 147*z - 273*z^2 + 140*z^3) + y*z^3*(40 
 - 154*z + 147*z^2 - 83*z^3 + 20*z^4) + y^2*z^2*(60 - 254*z + 363*z^2 
 - 273*z^3 + 80*z^4) + y^3*z*(40 - 254*z + 462*z^2 - 439*z^3 + 140*z^4) 
 + y^4*(10 - 154*z + 363*z^2 - 439*z^3 + 160*z^4)) - 9*(y + z)^4*(2  
 + y^2*(3 - 9*z) - 4*z + 3*z^2 - z^3 + y^3*(-1 + 4*z) + y*(-4 + 12*z 
 - 9*z^2 + 4*z^3))*H[0, y] ); 

Convert to a string

resultString = ToString[result, InputForm]

Locate the positions where we have y raised to a power or the multipliers y*z^3 or 9*(y + z)^4.

resultPositions = StringPosition[resultString,
  {"+ y^" ~~ _, "+ y*z^3", "- 9*(y + z)^4*"}]

(* {{21, 25}, {81, 85}, {121, 127}, {170, 174}, {222, 
  226}, {273, 277}, {323, 336}, {340, 344}, {376, 380}} *)

Add the beginning and end to the positions

resultPositions = Join[{{1, 20}}, resultPositions, {{433, 433}}]

Now make a list of the substrings where we want line breaks to occur

resultStringsList = Map[StringTake[resultString,
     {resultPositions[[#, 1]], resultPositions[[# + 1, 1]] - 1}] &, 
      Range[10]]

This is what it looks like in a column

resultStringsList // Column

Mathematica graphics

Export it to a text file

Export["output.m", resultStringsList, "Text"]

The file "output.m" appears similar to the column above.

$\endgroup$

Your Answer

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

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