In older versions of Mathematica you could use SpaceForm to leave a certain amount of spaces when writing to a file. For example, if we have the following data:

data = Table[{x, N[Exp[2 - x]]}, {x, 0, 40, 5}];

We can then write it to a file

MapThread[Write["file.txt", FortranForm[#], SpaceForm[4], FortranForm[#2]]&, Transpose[data]]

You can still use SpaceForm in the current version of Mathematica but it seems there is no documentation for it. I'm just wondering if that function was replaced by a newer one or how to do the same thing with a documented function.

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Apr 24, 2016 at 22:04
  • $\begingroup$ You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful $\endgroup$
    – Michael E2
    Apr 24, 2016 at 22:04
  • 1
    $\begingroup$ FWIW in v10 SpaceForm[n] puts one space regardless of the argument. I cant find it documented for any old version either.. $\endgroup$
    – george2079
    May 25, 2016 at 13:46
  • $\begingroup$ hmm it turns out Print[1, SpaceForm[n], 2] gives n spaces, but it doesn't work the same with Write $\endgroup$
    – george2079
    May 25, 2016 at 14:07
  • 1
    $\begingroup$ there is some interaction with other formatting.. this Write["file.txt", 0, SpaceForm[10], OutputForm[1]] gives 10 spaces, while Write["file.txt", 0, SpaceForm[10], 1] gives just one. $\endgroup$
    – george2079
    May 25, 2016 at 19:13

3 Answers 3


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 as n 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"];

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"]

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):

Format[SpaceForm[n_Integer], InputForm] := OutputForm[StringJoin[ConstantArray[" ", n]]];
Format[SpaceForm[n_Integer], OutputForm] := StringJoin[ConstantArray[" ", n]];

Now the bug is fixed:

Write["file.txt", 0, SpaceForm[10], 1]

0          1

  • $\begingroup$ Have you seen Spacer? $\endgroup$
    – yode
    Jun 24, 2016 at 11:32
  • $\begingroup$ @yode Spacer doesn't fit the OP's needs because in OutputForm it isn't converted into the appropriate number of whitespace characters like SpaceForm: try Spacer[3] // OutputForm and compare to {SpaceForm[3]} // OutputForm. $\endgroup$ Jun 24, 2016 at 11:42
f = OpenWrite["test.txt"];
nsp[n_] := OutputForm[StringJoin[ConstantArray[" ", n]]]
Write[f, 1, nsp[3], 2, nsp[1], 3];

1   2 3


I would use:

 Map[FortranForm, data, {2}],
 "Table", "FieldSeparators" -> "    "

(* File contents:
    0    7.38905609893065
    5    0.049787068367863944
    10    0.00033546262790251185
    15    2.2603294069810542e-6
    20    1.522997974471263e-8
    25    1.026187963170189e-10
    30    6.914400106940203e-13
    35    4.658886145103398e-15
    40    3.1391327920480296e-17
  • $\begingroup$ Export will not be good enough for what I'm trying to do. I would like to use Write or WriteString $\endgroup$
    – JRad
    Apr 24, 2016 at 22:02
  • $\begingroup$ Why won't Export work for you in specific? $\endgroup$
    – MarcoB
    Apr 24, 2016 at 22:38
  • $\begingroup$ If for example I want to leave different number of spaces between different columns. $\endgroup$
    – JRad
    Apr 24, 2016 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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