4
$\begingroup$

Bug introduced in 10 and persisting through 11.1.0
TextString is new in 10.0


According to the Documentation

TextString[TableForm[list]] formats a list of lists as space-separated columns.

and

TextString@TableForm@{{1,2},{3,4}}

outputs expected result

"1 2
3 4"

However,

TextString@TableForm@{{1},{3,4}}

outputs

"TableForm[{{1}, {3, 4}}]"

Is this behavior intended? To me, this is inconsistent.

$\endgroup$
  • $\begingroup$ Looks like a bug to me. $\endgroup$ – Alexey Popkov Apr 23 '17 at 5:23
  • $\begingroup$ According to the Docs, "TextString[expr] gives a human-readable string representation of expr." I don't think that the output "TableForm[{{1}, {3, 4}}]" is "human-readable" in the sense of the Docs for TextString. So tag this as a bug. $\endgroup$ – Alexey Popkov Apr 23 '17 at 6:27
  • $\begingroup$ @AlexeyPopkov Very likely a bug : ) $\endgroup$ – matheorem Apr 23 '17 at 11:23
  • $\begingroup$ I voted to close this since the question "is this behavior intended?" is not something that we can answer. $\endgroup$ – C. E. Apr 23 '17 at 15:00
2
$\begingroup$

Presumably, "list of lists" was intended to mean "full array". And, from the documentation for ArrayQ: "A ragged collection of nested lists is not a full array."

expr = {{1}, {3, 4}};

ArrayQ[expr]

(*  False  *)

Pad the sublists with empty strings to make them all the same length and form a full array.

TextString@TableForm@(PadRight[#, Max[Length /@ expr], ""] & /@ expr)

enter image description here

TextString@TableForm@(PadLeft[#, Max[Length /@ expr], ""] & /@ expr)

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Thank you very much for solution. PadRight could be made simpler TextString@TableForm@PadRight[{{1}, {3, 4}}, Automatic, ""] +1 $\endgroup$ – matheorem Apr 23 '17 at 4:05
2
$\begingroup$

You can use the old method :

st=OpenWrite["fileName",FormatType-> OutputForm,PageWidth-> Infinity];
Write[st,TableForm@{{2},{3,4}}];
Close[st];
FilePrint["fileName"]

enter image description here

You can remove the extra line with the option TableSpacing of TableForm :

st=OpenWrite["fileName",
FormatType-> OutputForm,
PageWidth-> Infinity];
Write[st,TableForm[{{2},{3,4}},TableSpacing->{0, 1}]];
Close[st];
FilePrint["fileName"]

enter image description here

$\endgroup$
  • $\begingroup$ Hi, andre, thank you for answering. But the output has extra blank line in between $\endgroup$ – matheorem Apr 23 '17 at 11:24
  • $\begingroup$ @matheorem That's not a problem . You can do anything you want with the option TableSpacing of TableForm. In your case TableForm[{{2},{3,4}},TableSpacing->{0, 1}], I think. $\endgroup$ – andre314 Apr 23 '17 at 12:27
  • $\begingroup$ I think this proves that the behavior shown in the question isn't intended. $\endgroup$ – Alexey Popkov Apr 23 '17 at 17:19

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.