5
$\begingroup$

The docs for Dividers give an example featuring Center as the setting for the the Dividers option in a Grid expression:

Grid[Table[x, {3}, {7}], Dividers -> Center]

Mathematica graphics

...and also some examples where Graphics directives are given as the setting for this option; e.g.

Grid[Table[x, {4}, {7}],
     Dividers -> {{2 -> Red, -2 -> Blue}, {2 -> Red, -2 -> Blue}}]

Mathematica graphics

...but they say nothing about how to specify both Center and a Graphics directive for this option.


Q: Is there a way to specify both Center and Graphics directives for Grid's Dividers option?


FWIW, here are some of my failed attempts at guessing the answer to this question:

Grid[Table[x, {3}, {7}], Dividers -> {Center -> Red}]

Mathematica graphics

Grid[Table[x, {3}, {7}], Dividers -> {Center -> Red, Center -> Blue}]

Mathematica graphics

Grid[Table[x, {3}, {7}], Dividers -> {{Center -> Red}, {Center -> Blue}}]

Mathematica graphics

Grid[Table[x, {3}, {7}], Dividers -> {Center, Red}]

Mathematica graphics

(It should go without saying that the last result above is no good; the goal here is to apply both types of specifications to the same dividers simultaneously.)

Grid[Table[x, {3}, {7}], Dividers -> Directive[Center, Red]]

Fails with

An improperly formatted directive with head Symbol was encountered.

$\endgroup$
3
  • $\begingroup$ Would specifying color for all and removing first and last divider be of any interest? $\endgroup$
    – Stitch
    Commented Apr 13, 2017 at 12:51
  • $\begingroup$ @Stitch: sure (I see that one of gwr's solutions take that approach) $\endgroup$
    – kjo
    Commented Apr 13, 2017 at 15:31
  • $\begingroup$ Right, I was thinking about exactly this. This is not ideal relative to your question, but you can be pretty flexible there. $\endgroup$
    – Stitch
    Commented Apr 13, 2017 at 16:04

2 Answers 2

5
$\begingroup$

Single Color for Center Dividers

Grid[ Table[x, {3}, {7}], Dividers -> Center, FrameStyle -> Red ]

SingleColor

Different Colors for Center Dividers

With[
   {
       horizontalColor = Blue,
       verticalColor   = Red
   },
   Grid[ 
       Table[x, {3}, {7}], 
       Dividers -> {{None, {verticalColor}, None}, {None, {horizontalColor}, None}}
   ]
]

TwoColors

By adding another color right after verticalColor or horizontalColor you can make the colors for the center alternating. The specs for drawing frames can pretty much match anything, you want.

$\endgroup$
4
$\begingroup$

The expanded form of Center is {False, {True}, False} as seen by looking at Box form:

Grid[Table[x, {3}, {7}], Dividers -> Center] // ToBoxes // First // Options
{AutoDelete -> False, 
 GridBoxDividers -> {"Columns" -> {False, {True}, False}, 
   "Rows" -> {False, {True}, False}}, 
 GridBoxItemSize -> {"Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}}

We can replace True with style directives as desired. So we may write:

styleCenter[r_] := styleCenter[r, r];
styleCenter[r_, c_] := {False, {#}, False} & /@ {r, c}

Grid[Table[x, {3}, {7}], Dividers -> styleCenter[Red]]

enter image description here

Grid[Table[x, {3}, {7}], Dividers -> styleCenter[{Thick, Blue}, Red]]

enter image description here

$\endgroup$
1
  • $\begingroup$ @gwr Thanks. I voted for your answer as well, of course. $\endgroup$
    – Mr.Wizard
    Commented Apr 22, 2017 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.