# Alignement of text

I adopted the following clever code from Mr. Wizard (see here ).

HoldForm[+##] & @@ RandomInteger[100, 2]


I want to prepare a practising table of addition for my son.

Something like:

Table[Text[
Style[ToString[HoldForm[+##] & @@ RandomInteger[100, 2]] <> " = ",
Italic, 20]], {100}] //
Column[#, Left, Background -> {{LightGray, GrayLevel[.9]}},
Frame -> True, ItemSize -> 10] &


However, I want the output to be "aligned". If the four first elements of the output is, for instance, 12+5=, 3+48=, 78+89=, and 7+9= I want them to be aligned as follows

12 +  5  =
3 + 48  =
78 + 89  =
7 +  9  =


How is it possible to achieve this?

Thanks.

• @Kuba. Thanks for properly modifying the tags. May 12 '15 at 9:41
• dimitris, speaking of practicing tables you might like this q/a
– kglr
May 12 '15 at 10:19
• @ kguler. Wow! Thank you! I think my son will need less hour to finish the table than the time I will need to fully grasp the codes:-)! May 12 '15 at 10:28
• I also think that the link below may be useful for relevant things. wolfram.com/mathematica/new-in-10/inactive-objects/… May 12 '15 at 10:29
• BTW, all the examples of Inactivate I have seen so far is with for Built-in functions like Plus or Times. Is it a reason for that? May 12 '15 at 10:33

Slightly more handy approach (unless you want to work with negative integers):

Grid[
{#, "+", #2, "=", Item["", ItemSize -> 3]} & @@@ RandomInteger[20, {10, 2}]
,
Alignment -> {{Right, Center, Right, Center, Center}},
BaseStyle -> {Italic, 20},
Frame -> True,
Background -> {{}, {{GrayLevel@.9, GrayLevel@.95}}}
]


A simplistic first pass at adapting this for negative integers in the second column:

SeedRandom[1]
ints = RandomInteger[{-200, 200}, {10, 2}];

lines = {#, If[#2 < 0, "-", "+"], Abs@#2, "=", Spacer[50]} & @@@ ints;

Grid[lines,
Alignment -> {{Right, Center, Right, Center, Center}},
Frame -> True,
Background -> {{}, {{GrayLevel@.9, GrayLevel@.95}}}]


Note: I ran into this problem while exporting the image above; that is something you may wish to be aware of if you suddenly see excess quotation marks in your images.

• Thank you very much! But, next scholar year, my son will indeed work with negative numbers:-)! May 12 '15 at 8:40
• Kuba: certainly this method can be adapted for negative numbers; would you like me to edit your post to do that or include it in my own? May 12 '15 at 9:40
• @Mr.Wizard sure if you want and have an idea, without clear specification I would waste half of a day thinking if + (-10) or -10 and similar things.
– Kuba
May 12 '15 at 9:47
• I added a simple method. Hopefully it works well enough to meet with your approval. May 12 '15 at 9:56
• @Mr.Wizard Thank you :)
– Kuba
May 12 '15 at 9:57

The purpose of using HoldForm and Plus is to allow automatic formatting rules for Plus to apply while preventing evaluation. Since you want custom formatting rules that method may be inapplicable.

To get alignment we can either use a tabular format like Grid (as Kuba did) or we can pad the numbers themselves. One automatic approach to the latter is PaddedForm:

SeedRandom[1]
ints = RandomInteger[{-200, 200}, {10, 2}];

PaddedForm[HoldForm[+##] == Spacer[1], 3] & @@@ ints // Column


Here I actually manged to keep HoldForm[+##] but you can see that the spacing is a little strange. I also had to specify the maximum integer length. If we try manual padding after converting to strings it will probably end up more complicated that just using Grid, although if padding with zeros is acceptable you may look at IntegerString.

• @ Mr Wizard: Thank you very much! (Superficially) simple things need sometimes a lot of thought and work:-)! May 12 '15 at 9:39