4
$\begingroup$

I'm using a book style sheet but this question applies to the standard notebook interface as well.

I'm writing a sequence of cells in different styles (text, equation, figure, figure caption, ...) in a special section (e.g., "Example") and would like to color the background of all content in the Example. I group the cells, click-select the associated grouping bar and select Format>Background color to be pink (for example). This changes the background in each cell to be pink. Great!

Almost...

This procedure colors the background of the cells, but not of the "page" behind. This looks a bit choppy and ununified, as here:

two cells with same background color

Merging cells forces them to be the same style, which of course is not appropriate (as I have text, equations, ...).

I'm seeking a way to color the background page as well, but of course just between the beginning and end of the chosen cells. I'm not interested in the global option of setting the background color of the entire document.

There are several questions here about setting the backgrounds of multiple cells with a single command, but none that I could find that colors the "page" as well.

$\endgroup$
3
  • $\begingroup$ There's an option in preferences under Advanced>Open Option Inspector>Notebook Options>Display Options>Background that changes the whole page background. But it sounds like you just want it behind the certain cells. Maybe there's an option to reduce the padding at the top and bottom of these special cells to 0, so adjacent cells appear as one long cell? $\endgroup$
    – N.J.Evans
    Jun 14 at 19:53
  • $\begingroup$ @N.J.Evans: Indeed... as I pointed out I want to highly just the "Example" (or other) section. I'll edit my post to clarify. $\endgroup$ Jun 14 at 20:24
  • 1
    $\begingroup$ @DavidG.Stork the trick is to set CellMargins -> {{r, l}, {0, 0} and CellFrameMargins->{{0, 0}, {bottom, top}} which visually merges the two but retains the padding $\endgroup$
    – b3m2a1
    Jun 14 at 21:44
3
$\begingroup$

So I can show the pic:

CurrentValue[{StyleDefinitions, "Text", CellMargins}]

{{66, 10}, {7, 8}}

CurrentValue[{StyleDefinitions, "Text", CellFrameMargins}]

8

Cell[
  CellGroupData[
   {
    Cell[
     "top",
     "Text",
     FontColor -> White,
     Background -> Pink,
     CellMargins -> {{66, 10}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {7, 8}}
     ],
    Cell[
     "bottom",
     "Text",
     FontColor -> White,
     Background -> Pink,
     CellMargins -> {{66, 10}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {7, 8}}
     ]
    },
   Open
   ]
  ] // CellPrint

enter image description here

With different colors

Cell[
  CellGroupData[
   {
    Cell[
     "top",
     "Text",
     FontColor -> White,
     Background -> Pink,
     CellMargins -> {{66, 10}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {7, 8}}
     ],
    Cell[
     "bottom",
     "Text",
     FontColor -> Black,
     Background -> LightBlue,
     CellMargins -> {{66, 10}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {7, 8}}
     ]
    },
   Open
   ]
  ] // CellPrint

enter image description here

This obviously admits a simple stylesheet solution too

Per the comment, Mathematica doesn't resize content in the CellFrameLabels (you can look up on the site the many, many attempts to make that content resize properly). This is a problem with numbered equations. There are three straightforward solutions. The first is less flexible but works with documents where the cell heights can be parametrized

Cell[CellGroupData[{
    Cell["blah", "DisplayFormulaNumbered", FontColor -> White, 
     Background -> Pink,
     CellMargins -> {{66, 3}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {4, 4}},
     CellFrameLabelMargins -> 0,
     CellFrameLabels -> {
       {None,
        Cell[
         TextData[{"(", CounterBox["DisplayFormulaNumbered"], ")"
           }], "DisplayFormulaEquationNumber",
         CellMargins -> {{0, 0}, {0, 0}},
         CellFrameMargins -> {{5, 5}, {4, 4}},
         TextAlignment -> {Center, Center},
         CellSize -> {Automatic, 26}
         ]
        },
       {None, None}
       }
     ],
    Cell["top", "FigureCaption", FontColor -> Black, Background -> LightBlue,
     CellMargins -> {{66, 3}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {4, 4}}
     ]
    }, Open]
  ] // CellPrint

enter image description here

You'll notice it spans fine, but this is fragile because I hard-coded the height and it also floats oddly to the top-right corner and this is basically unfixable.

The second option is to remove the background on the labels and make sure the label widths are identical

Cell[CellGroupData[{
    Cell["blah", "DisplayFormulaNumbered", FontColor -> White, 
     Background -> Pink,
     CellMargins -> {{66, 3}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {4, 4}},
     CellFrameLabels -> {
       {None,
        Cell[
         TextData[{"(", CounterBox["DisplayFormulaNumbered"], ")"}], 
         "DisplayFormulaEquationNumber",
         FontColor -> Pink,
         Background -> None,
         CellSize -> {25, Automatic},
         CellMargins -> {{0, 0}, {0, 0}}
         ]
        },
       {None, None}
       }
     ],
    Cell["top", "FigureCaption", FontColor -> Black, Background -> LightBlue,
     CellMargins -> {{66, 3}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {4, 4}},
     CellFrameLabels -> {
       {None,
        Cell[
         "",
         "DisplayFormulaEquationNumber",
         CellSize -> {25, Automatic},
         Background -> None,
         CellMargins -> {{0, 0}, {0, 0}}
         ]
        },
       {None, None}
       }
     ]
    }, Open]
  ] // CellPrint

enter image description here

This is a much more robust solution. It is worth noting that the width is hard coded.

A final possibility, again, where the label width is hard coded, is to use a negative value for CellFrameLabelMargins. To do this, again, both the upper and lower cells need a label.

Cell[CellGroupData[{
    Cell["blah", "DisplayFormulaNumbered", FontColor -> White, 
     Background -> Pink,
     CellMargins -> {{66, 3}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {4, 4}},
     CellFrameLabelMargins -> -20
     ],
    Cell["top", "FigureCaption", FontColor -> Black, Background -> LightBlue,
     CellMargins -> {{66, 3}, {0, 0}},
     CellFrameMargins -> {{8, 8}, {4, 4}},
     CellFrameLabelMargins -> -20,
     CellFrameLabels -> {
       {None,
        Cell[
         "",
         "DisplayFormulaEquationNumber",
         Background -> None,
         CellMargins -> {{0, 0}, {0, 0}}
         ]
        },
       {None, None}
       }
     ]
    }, Open]
  ] // CellPrint

enter image description here

If you're interested in using these cells, make sure the starting content will allow you to type what you want (i.e. "" for text, BoxData[""] for code, and BoxData@FormBox["", TraditionalForm] for equations). Here's an example of this with equations

Cell[
  CellGroupData[{
    Cell[
     BoxData@FormBox["", TraditionalForm],
     "DisplayFormulaNumbered",
     TextAlignment -> Center, FontColor -> White, Background -> Pink, 
     CellMargins -> {{66, 3}, {0, 0}}, CellFrameMargins -> {{8, 8}, {4, 4}}, 
     CellFrameLabelMargins -> -20], 
    Cell["top", "FigureCaption", FontColor -> Black, Background -> LightBlue, 
     CellMargins -> {{66, 3}, {0, 0}}, CellFrameMargins -> {{8, 8}, {4, 4}}, 
     CellFrameLabelMargins -> -20, 
     CellFrameLabels -> {{None, 
        Cell["", "DisplayFormulaEquationNumber", Background -> None, 
         CellMargins -> {{0, 0}, {0, 0}}]}, {None, None}}]},
   Open]
  ] // CellPrint

then I can type math in the top cell

enter image description here

$\endgroup$
8
  • $\begingroup$ This is a great step in the right direction, thanks (+1). But I couldn't modify your code to get other cell types, such as EquationNumbered (complete with automatic numbering and such), FigureCaption, and so on. Could you repost with a mixture of cell types? $\endgroup$ Jun 14 at 22:48
  • $\begingroup$ @DavidG.Stork it's harder to be general for this case, but option three in what I just posted mostly gets you there $\endgroup$
    – b3m2a1
    Jun 14 at 23:13
  • $\begingroup$ @bwm2a1: This is so close... but I still cannot get a true equation (e.g., x^2 + y^2) in the DisplayFormulaNumbered cell, where the exponents are properly set, fractions, integral signs, ... $\endgroup$ Jun 15 at 0:30
  • $\begingroup$ @DavidG.Stork use BoxData[""] instead of "blah". That ought to fix it. $\endgroup$
    – b3m2a1
    Jun 15 at 0:31
  • 1
    $\begingroup$ Got it! Thanks so very much! ($\checkmark$) $\endgroup$ Jun 15 at 0:37

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.