5
$\begingroup$

I'm having an issue making the second row in this grid space out evenly:

Grid[{{Slider[ImageSize -> 300], SpanFromLeft, SpanFromLeft}, {"l", 
   "c", "r"}}, 
 Alignment -> {{2, 1} -> Left, {2, 2} -> Center, {2, 3} -> Right}, 
 Frame -> All, Spacings -> Automatic]

enter image description here

Updates:

Response to @MichaelE2: There is still too much space to the right of the slider:

enter image description here

Response to @m_goldberg: At higher magnifications the slider runs outside the grid:

enter image description here

| improve this question | | | | |
$\endgroup$
  • $\begingroup$ add the option ColumnsEqual -> True or the option ItemSize -> All? $\endgroup$ – kglr Jan 15 at 23:27
  • $\begingroup$ It says ColumnsEqual isn't an option for Grid $\endgroup$ – user5601 Jan 15 at 23:48
  • $\begingroup$ ColumnsEqual makes the columns equally spaced, but it also makes the grid a lot bigger for some reason. ItemSize does the same thing, too big. $\endgroup$ – user5601 Jan 15 at 23:54
  • $\begingroup$ @user5601 I believe it make all columns equal in width to the item in position {1, 1}, the slider. The SpanFromLeft seems to be ignored in considering column widths. $\endgroup$ – Michael E2 Jan 16 at 13:07
  • $\begingroup$ About your response to me: you didn't use my code. What do you expect me to say? -- If it is somehow connect to my comment, my comment only explained how the widths of the columns are calculated when you use ItemSize -> All. The widths is set to the widest column, which is the width of the slider. $\endgroup$ – Michael E2 Jan 19 at 0:32
4
$\begingroup$
Pane[
 Grid[{
   {Slider[ImageSize -> 300], SpanFromLeft, SpanFromLeft},
   {"l", "c", "r"}}, 
  Alignment -> {Center, Baseline,
    {{2, 1} -> Left, {2, 2} -> Center, {2, 3} -> Right}}, 
  Frame -> All, ItemSize -> Scaled[1/3]],
 First@ImageDimensions@
   Rasterize@Grid[{{Slider[ImageSize -> 300]}}, Frame -> All]]

enter image description here

| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ Is there a way to tweak this to get rid of the added space? $\endgroup$ – user5601 Jan 19 at 5:17
  • $\begingroup$ @user5601 What added space? You specify the slider should have an image size (width) of 300. There is a small margin around each element plus a frame. IIRC, the overall image size is 313. Do you want to get rid of the margins? If so, set the width of Pane (the second argument) to 300. $\endgroup$ – Michael E2 Jan 19 at 13:40
  • $\begingroup$ Thanks that does it. $\endgroup$ – user5601 Jan 19 at 22:44
3
$\begingroup$

Maybe this will work for you.

With[{size = 300},
  Grid[
    {{Slider[ImageSize -> size], SpanFromLeft}, {"l", "c", "r"}},
    ItemSize -> {{{1 + size/3/CurrentValue["FontMWidth"]}}, 1},
    Alignment -> {{Left, Center, Right}},
    Frame -> All]]

grid

The above code is concise and works for your specific example because, although the alignment specification affects all rows, the slider can't move, so the alignments are only seen in the 2nd row. If in real work, you want only the 2nd row to be affected, then use:

With[{size = 300},
  Grid[
    {{Slider[ImageSize -> size], SpanFromLeft}, {"l", "c", "r"}},
    ItemSize -> {{{1 + size/3/CurrentValue["FontMWidth"]}}, 1},
    Alignment -> {Center, Automatic, {{2, 1} -> Left, {2, 3} -> Right}},
    Frame -> All]]

Notes

  1. In the specification of the 1st row of the grid only one SpanFromLeft need be given.
  2. In the 2nd version of code, {2, 2} -> Center, is not needed in the 3rd element of the alignment specification because the specification of Center in the 1st element sets that as the default.
| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ See my updates - try changing the notebook magnification. $\endgroup$ – user5601 Jan 18 at 22:23
  • $\begingroup$ @user5601. Yes, it doesn't play well with changes in notebook magnification. Michael E2 approach is better in that it not affected by magnification changes. $\endgroup$ – m_goldberg Jan 19 at 1:57

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.