3
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ @Kuba. Thanks for properly modifying the tags. $\endgroup$ – Dimitris May 12 '15 at 9:41
  • $\begingroup$ dimitris, speaking of practicing tables you might like this q/a $\endgroup$ – kglr May 12 '15 at 10:19
  • $\begingroup$ @ 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:-)! $\endgroup$ – Dimitris May 12 '15 at 10:28
  • $\begingroup$ I also think that the link below may be useful for relevant things. wolfram.com/mathematica/new-in-10/inactive-objects/… $\endgroup$ – Dimitris May 12 '15 at 10:29
  • $\begingroup$ 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? $\endgroup$ – Dimitris May 12 '15 at 10:33
3
$\begingroup$

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

enter image description here


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

enter image description here

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.

|improve this answer|||||
$\endgroup$
  • 1
    $\begingroup$ Thank you very much! But, next scholar year, my son will indeed work with negative numbers:-)! $\endgroup$ – Dimitris May 12 '15 at 8:40
  • $\begingroup$ 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? $\endgroup$ – Mr.Wizard May 12 '15 at 9:40
  • $\begingroup$ @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. $\endgroup$ – Kuba May 12 '15 at 9:47
  • 1
    $\begingroup$ I added a simple method. Hopefully it works well enough to meet with your approval. $\endgroup$ – Mr.Wizard May 12 '15 at 9:56
  • $\begingroup$ @Mr.Wizard Thank you :) $\endgroup$ – Kuba May 12 '15 at 9:57
2
$\begingroup$

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

enter image description here

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.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ @ Mr Wizard: Thank you very much! (Superficially) simple things need sometimes a lot of thought and work:-)! $\endgroup$ – Dimitris May 12 '15 at 9:39

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.