6
$\begingroup$

Consider the following Grid

Grid[{
  {123456, ""},
  {123, 12},
  {3, 1234}
  }, Frame -> All, Alignment -> {{Right, Left}, Automatic}]

which yields

enter image description here

My goal is to get the grid in the following shape (manually edited)

enter image description here

that is, while the second and third rows are aligned according to Alignment -> {{Right, Left}, Automatic}, the first row has only one cell centred around the mid-vertical divider in the grid. Notice that it's not centred with respect to the cell, though this is also interesting.

How can I achieve this?

I'm aware of SpanFromLeft, which I tried in the following code

Grid[{
  {123456, SpanFromLeft},
  {123, 12},
  {3, 1234}
  }, Frame -> All, Alignment -> {{Right, Left}, Automatic}]

that is,

enter image description here

This gets me close, however the 123456 cell is not quite centred. Even closer is the code

Grid[{
  {123456, SpanFromLeft},
  {"", 123, 12},
  {"", 3, 1234}
  }, Frame -> All, Alignment -> {{Center, Right, Left}, Automatic}]

yielding

 C

But the grid has an "extra" undesired column.

Any ideas?

Edit: Following kglr's answer below, consider the case

Grid[{{Item[123456, Alignment -> Center], SpanFromLeft}, {123, 12}, {3, 1234567890}}, 
 Frame -> All, Alignment -> {{Right, Left}, Automatic}]

enter image description here

This indeed yields a centred 123456, within the respective cell. As mentioned before, however, is it possible to automatically centre it with respect to the mid-vertical divider? I want something like

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ See Grid documentation for specifying Alignment. Then try Alignment -> {{Right, Left}, {{Automatic}}, {{1, 1} -> Center}}. $\endgroup$ – Edmund Dec 30 '19 at 3:01
  • $\begingroup$ Thank you @Edmund, but that solves partially my question. I don't understand why my question was closed. I actually asked for two things, one of which seems actually trickier than that. Please see the 'edit' section for clarification and please reconsider its closure. $\endgroup$ – sam wolfe Dec 30 '19 at 14:44
10
$\begingroup$

Update 2: A function that takes a list of cell indices and a Grid object to interactively adjust the alignments and paste the input form of the selected configuration:

ClearAll[alignCells]
alignCells[items : {{_, _} ..}] := DynamicModule[{n = Length@items, g = #,
   a = Table[0, Length@items]}, 
  Panel @ 
   Column[{Grid[Table[With[{j = j}, {items[[j]], 
       Manipulator[Dynamic[a[[j]]], {-1, 1}, Appearance -> "Labeled"]}], {j, n}]], 
     Button["Paste", Manipulate`PasteSnapshot[Grid@g]], 
     Table[With[{j = j}, g[[1, ## & @@ items[[j]]]] = 
        Pane[g[[1, ## & @@ items[[j]]]], ImageSize -> {Full, All}, 
         Alignment -> {Dynamic@a[[j]], Center}]], {j, n}]; 
     MapAt[Item[#, Background -> LightYellow] &, g, Prepend[#, 1] & /@ items]}]]&;

Examples:

grid1 = Grid[{{123456, SpanFromLeft}, {123, 12}, {3, 1234567890}}, 
   Frame -> All, Alignment -> {{Right, Left}, Automatic}, ItemSize -> {{2, 7}, 1}]

enter image description here

alignCells[{{1, 1}}] @ grid1

enter image description here

Click Paste button to get

Grid[{{Pane[123456, ImageSize -> {Full, All}, 
    Alignment -> {Dynamic[FE`a$$860[[1]]], 
      Center}], \[SpanFromLeft]}, {123, 12}, {3, 1234567890}}, 
 Frame -> All, Alignment -> {{Right, Left}, Automatic}, 
 ItemSize -> {{2, 7}, 1}]

and evaluate to get

enter image description here

grid2 = Grid[{{123456, SpanFromLeft, 13}, {123, 12, 23}, {3, 
     1234567890, 33}, {41, 1234, SpanFromLeft}, {1, 2, 3}}, 
   Frame -> All, Alignment -> {{Right, Left, Left}, Automatic}, 
   ItemSize -> {{2, 7, 3}, 1}]

enter image description here

alignCells[{{1, 1}, {4, 2}, {5, 2}}] @ grid2

enter image description here

Click the Paste button and evaluate the resulting cell to get

enter image description here

Update: "is it possible to automatically center it with respect to the mid-vertical divider?"

I don't know of any automatic way to get the first row aligned with the column divider.

A semi-manual approach is possible:

  1. Specify the ItemSizes in grid options, and
  2. Wrap the first cell content with Pane with the options ImageSize -> {Full, All} and Alignment -> {a, Center} and adjust a to get the desired position.
  3. Use PasteSnapshot button in Manipulate and evaluate the pasted cell to get the desired output.

Manipulate[
 Grid[{{Pane[123456, ImageSize -> {Full, All}, 
     Alignment -> {a, Center}], SpanFromLeft}, 
    {123, 12}, {3, 1234567890}}, Frame -> All, 
  Alignment -> {{Right, Left}, Automatic}, 
  ItemSize -> {{Scaled[.05], Scaled[.1]}, 1}],
{{a, 0}, -1, 1}]

enter image description here

Use specific sizes, e.g., ItemSize -> {{2, 7}, 1}, if you do not want the cell sizes change with the notebook window size.

Original answer:

Grid[{{123456, SpanFromLeft}, {123, 12}, {3, 1234}}, Frame -> All, 
 Alignment -> {{Right, Left}, Automatic, {{1, 1} -> Center}}]

enter image description here

Alternatively, assign cell-specific alignments using Item:

Grid[{{Item[123456, Alignment -> Center], SpanFromLeft}, {123, 12}, {3, 1234}}, 
 Frame -> All, Alignment -> {{Right, Left}, Automatic}]

same picture

$\endgroup$
0

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.