6
$\begingroup$

For example,

Print["1\t55
33\t44"]

outputs

enter image description here

Why tab is not aligned? If tab is not aligned, why is it called tab? Is there a way to make tabs aligned?

$\endgroup$
0

2 Answers 2

9
$\begingroup$

Print isn't really meant to do text-processor-like formatting. It most common use it to display intermediate results where people don't care much about pretty-printing. It produces an output cell where a tab character in a string is replaced with four spaces as is illustrated below.

 Print["12345678\n1\t55\n33\t44"]

output

There are lots of ways to get the kind of formatting you want. Here is one of them, which requires only a little modification of your code.

CellPrint[TextCell["1\t55\n33\t44", "Text"]]

text

$\endgroup$
3
  • 1
    $\begingroup$ (+1) Is it documented anywhere that in Print output "a tab character in a string is replaced with four spaces"? $\endgroup$ Commented Apr 20, 2017 at 15:45
  • $\begingroup$ @AlexeyPopkov. Not that I know of. I just counted the spaces. $\endgroup$
    – m_goldberg
    Commented Apr 20, 2017 at 23:33
  • $\begingroup$ @m_goldberg Thank you so much! +1 $\endgroup$
    – matheorem
    Commented Apr 21, 2017 at 11:31
3
$\begingroup$

StandardForm does not support tabs or newlines like regular text editor. It doesn't make sense to do so considering variety of 2D typesetting available there.

One can't even type \t unless it is a beginning of the line.

tutorial / NewlinesAndTabsInStrings:

You should realize that even though it is possible to achieve some formatting of Wolfram Language output by creating strings which contain raw tabs and newlines, this is rarely a good idea. Typically a much better approach is to use the higher-level Wolfram Language formatting primitives [...Column etc]. These primitives will always yield consistent output, independent of such issues as the positions of tab settings on a particular device.

And Print output is sent via $Output which FormatType is set to StandardForm.

In practice this means that your input goes through Cell @ BoxData @ ToBoxes @ input (roughly).

From what I've tried only TextForm respects tabs. It is used in styles like Code / Program / Text etc. Those cells contain TextData or just plain string inside instead of BoxData.

So the quick fix is:

Print @ TextCell["1\t55\n33\t44"]

or

Print @ RawBoxes @ Cell["1\t55\n33\t44"]

By the way, what documentation meant by "higher-level Wolfram Language formatting primitives" is something like:

Print @ Grid[ImportString["a\t55\n33\t44", "Table"], Alignment -> Left]
$\endgroup$
2

Your Answer

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

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