What function superseded SpaceForm?
According to this MathGroup post the function SpaceForm
was documented only via Information
(i.e. the SpaceForm::usage
Message
) even in Mathematica 3.0. With current version 10.4.1 the situation is still the same:
? SpaceForm
SpaceForm[n]
prints asn
spaces.
So you shouldn't worry: this function is in the current situation right from the start, nothing seems to be changed. It works as before and you can use it with the same confidence as earlier.
But if you need a solution based on well-documented functionality I recommend you instead of buggy undocumented SpaceForm[n]
use something like StringJoin[Table[" ", {n}]]
, and switch from Write
to WriteString
since the latter writes in OutputForm
what is convenient when you write a textual file:
str = OpenWrite[];
WriteString[str, 0, StringJoin[Table[" ", {10}]], FortranForm[4.658886145103398`*^-15], "\n"];
Close[str];
FilePrint[%]
0 4.658886145103398e-15
The following alternatives to StringJoin[Table[" ", {n}]]
also work well:
StringJoin[ConstantArray[" ", n]]
StringJoin[Array[" " &, n]]
Nest[" " <> # &, "", n]
Row[Table[" ", {n}]]
With Mathematica 10.1 or higher you can use StringRepeat[" ", n]
as a direct replacement of SpaceForm[n]
:
str = OpenWrite[];
WriteString[str, 0, StringRepeat[" ", 10], FortranForm[4.658886145103398`*^-15], "\n"]
Close[str];
FilePrint[%]
0 4.658886145103398e-15
And finally you also can remedy SpaceForm
as follows (for making the fix permanent you can add these lines into your Kernel init.m file):
Unprotect[SpaceForm];
Format[SpaceForm[n_Integer], InputForm] := OutputForm[StringJoin[ConstantArray[" ", n]]];
Format[SpaceForm[n_Integer], OutputForm] := StringJoin[ConstantArray[" ", n]];
Protect[SpaceForm];
Now the bug is fixed:
Write["file.txt", 0, SpaceForm[10], 1]
FilePrint["file.txt"]
0 1
f = OpenWrite["test.txt"];
nsp[n_] := OutputForm[StringJoin[ConstantArray[" ", n]]]
Write[f, 1, nsp[3], 2, nsp[1], 3];
Close[f]
FilePrint["test.txt"]
1 2 3