Group Abstract Group Abstract

Message Boards Message Boards

0
|
3K Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Help with Alignment option of Grid

Given a grid like this: enter image description here What is the correct argument for Alignment so that,...

  • column 1 will be flush left throughout,
  • row 1 columns 2 through 11 will be centered (unlike in the image), and
  • columns 2 through 11 in all succeeding rows (of undetermined number) will be decimal aligned (unlike numbers shown with a leading zero)?

I have discovered many wrong answers. The one used on the grid shown was,...

Alignment -> {
  {{Left, "."}},
  {{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1} -> Center}

In case it matters. I'm using version 13.0.1 on Windows. The numbers were formed using,...

NumberForm[Round[100 irrcf[numbervariable], .01], {8, 2}]

..., where irrc[] is a function too long to post here.

The list used to generate the grid was,...

{{S&P Idx Tot Rtrn,13.00,16.18,17.23,-0.94,-18.16,-9.53,-21.38,-5.83,260.48,5.06},
{S&P ETF,12.90,16.20,17.30,-0.95,-18.29,-9.61,-21.39,-5.84,260.62,5.06},
{Floating Rate ETF,1.42,0.74,0.54,-0.46,-0.94,-0.47,-2.08,-0.53,-3.23,-0.13}}
POSTED BY: Jay Gourley
4 Replies

Thanks again, Eric. Your last example is helpful. It and possibly a few others should be added to the Wolfram documentation for this option.

POSTED BY: Jay Gourley
Posted 3 years ago

There are so many details that you really need to just play with it. I have to look at the documentation for the special cases every time. Try different things and see what effect they have. But to get you started, I used this form: {spec-x, spec-y, rules}.

  • spec-x controls the horizontal alignments, and it applies column-wise (more on that in a bit).
  • spec-y controls the vertical alignments, and it applies row-wise.
  • rules allows you to specify overrides.

Now, for spec-x and spec-y, you can specify some initial settings, repeating settings in the middle, and some terminal settings. So, for example, {Left, {"."}} means "start with Left aligned, then repeat "." aligned thereafter (with no special terminal settings). This can get quite detailed.

For rules, you can specify single cells by their coordinates (row, col, same as Part uses), or you can specify ranges/blocks (row-range, col-range).

Examine (and experiment with) this example:

Grid[
 ArrayReshape[Alphabet[], {5, 5}],
 Dividers -> All,
 ItemSize -> {5, 5},
 Alignment ->
  {{Center, {Left, Right}, Center},
   {Center, {Top, Bottom}, Center}, 
   {{1, 1} -> {Left, Top}, {{4, 5}, {3, 4}} -> {Center, Bottom}}}]
POSTED BY: Eric Rimbey

Thanks, Eric. The corrected version works great. Just what I was looking for. But I don't understand why. I think the first argument to Alignment sets the default for all cells in the grid. Then the second two argument specify exceptions. But even after reading the documentation on Baseline I don't get what the arguments are saying to Alignment. I can probably figure it out on my own. But if you have time, please explain what arguments two and three are saying.

POSTED BY: Jay Gourley
Posted 3 years ago

Try something like this (random data for easy display):

Grid[
 {
  {"heading", Splice[RandomWord[10]]},
  {"row1", Splice[RandomReal[{-20, 20}, 10]]},
  {"row2", Splice[RandomReal[{-20, 20}, 10]]},
  {"row3", Splice[RandomReal[{-5, 5}, 10]]}},
 Dividers -> All,
 Alignment -> {{Left, {"."}}, {Center}, {{{1, 2}, {2, 11}} -> Center}}]

Edit

Sorry, I think I misunderstood. I think this is more like it.

Grid[
 {
  {"heading", Splice[RandomWord[10]]},
  {"row1", Splice[RandomReal[{-20, 20}, 10]]},
  {"row2", Splice[RandomReal[{-20, 20}, 10]]},
  {"row3", Splice[RandomReal[{-5, 5}, 10]]}},
 Dividers -> All,
 Alignment -> {{Left, {"."}}, {Baseline}, 
   {{{1, 1}, {2, 11}} -> {Center, Baseline}}}]
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard