3
$\begingroup$

How to put a label on a cell frame or align cell frame label with parent's cell frame?

Like that:

enter image description here

I did it but the solution is ugly not very responsive. I will post it unless better solutions appear.

Here is a starting point:

CellPrint[
  Cell["Test content", "Text"
  , CellFrame -> True
  , CellFrameLabelMargins -> 0
  , CellFrameLabels -> { {None, None}, {None, Cell[
      "Test label", "Text"
    , CellFrame -> 2
    , CellMargins -> 0
    , CellFrameMargins -> {{50, 50}, {5, 5}}
    ] }}
  ]
]

One can propose completely different approach of course.

Requirements in such case are:

  • such label needs to work from a stylesheet

  • label and the parent cell should give an impression of being the same element, one can't delete one of them etc.

  • it needs to work well on Win and Mac

  • the label is not editable and the cell work as a regular cell

  • should work well when Magnification changes

$\endgroup$
  • $\begingroup$ Unfortunately there is not a CellFrameLabelAlignment option $\endgroup$ – Mike Honeychurch Jan 4 '18 at 22:13
  • $\begingroup$ @MikeHoneychurch a lot of similar problems could be solved if there was CellDisplayFunction or something similar. $\endgroup$ – Kuba Jan 4 '18 at 22:20
  • $\begingroup$ If you've only got a certain set of labels/appearances you want you can also try nine-patch images $\endgroup$ – b3m2a1 Apr 15 at 2:14
1
$\begingroup$

Fun example:

Here's both a header and a footer using Mathematica's built in nine-patch images:

CellPrint[
 Cell[ExampleData[{"Text", "LoremIpsum"}]~Snippet~5, "Text",
  CellFrame -> {{1, 1}, {1, 1}},
  FrontEnd`CellFrameStyle -> GrayLevel[.8],
  CellFrameLabelMargins -> {{0, 0}, {-1, -1}},
  CellFrameLabels -> {
    {None, None},
    {
     ToBoxes@
      Panel["Cell footer",
       BaseStyle -> {
         (*turn this into an actual stylesheet style for better \
customizability/inheritance*)
         "Text",
         FontColor -> Hue[0, .5, .5],
         ShowStringCharacters -> False,
         PanelBoxOptions -> {
           FrameStyle -> Black,
           ImageSize -> {
             FEPrivate`Switch[
               FEPrivate`$OperatingSystem,
               "MacOSX", 18,
               "Window", 0,
               "Linux", 0,
               _, 0] + Scaled[1],
             Automatic
             },
           FrameMargins -> {{10, 10}, {5, 5}},
           Appearance -> {
             "Default" ->

              FrontEnd`FileName[{"Ribbons", "Common"}, 
               "ContentAreaGradient.9.png"]
             }
           }
         }
       ],
     ToBoxes@
      Panel["Cell header",
       BaseStyle -> {
         (*turn this into an actual stylesheet style for better \
customizability/inheritance*)
         "Text",
         FontColor -> Hue[0, .5, .5],
         ShowStringCharacters -> False,
         PanelBoxOptions -> {
           Appearance -> {
             "Default" ->

              FrontEnd`FileName[{"Dialogs", "CloudDialogs"}, 
               "AttachedCell.9.png"]
             },
           ImageSize -> {
             FEPrivate`Switch[
               FEPrivate`$OperatingSystem,
               "MacOSX", 18,
               "Window", 0,
               "Linux", 0,
               _, 0] + Scaled[1],
             Automatic
             },
           FrameMargins -> {{10, 10}, {5, 5}}
           }
         }
       ]
     }
    }
  ]
 ]

enter image description here

Original

Here are two options:

Boxes

This one works really simply:

CellPrint[
 Cell["Test content",
  "Text",
  CellFrame -> True,
  CellFrameLabelMargins -> {{0, 0}, {0, -2}},
  CellSize -> {Automatic, 100},
  CellFrameLabels -> {
    {None, None},
    {None,
     ToBoxes@
      Framed["asdasd", 
       BaseStyle -> {
         (* 
         turn this into an actual stylesheet style for better customizability/inheritance *)
         "Text",
         FontColor -> White, 
         ShowStringCharacters -> False,
         FrameBoxOptions -> {
           FrameStyle -> Black,
           ImageSize ->
            {
             FEPrivate`Switch[
               FEPrivate`$OperatingSystem, 
               "MacOSX", 18,
               "Window", 0(* Windows fudge factor *),
               "Linux",  0(* Linux  fudge factor *),
               _, 0
               ] + Scaled[1], 
             Automatic
             },
           FrameMargins -> {{10, 10}, {5, 5}},
           Background -> Blue
           }
         }
       ]
     }
    }
  ]
 ]

enter image description here

Outside of the weird 18 pixel fudge factor which it turns out has nothing to do with the CellMargins, "VerticalScrollBar", CellBracketOptions, ShowCellBracket or any of the other things I tested this one takes the cake for convenience.

If you substitute in PanelBox you can leverage the paneled appearance really nicely. Here's what Panel looks like with FrameBoxOptions changed to PanelBoxOptions and the addition of

Appearance -> {
  "Default" ->
   FrontEnd`FileName[{"Ribbons", "Common"}, 
    "ContentAreaGradient.9.png"]
  }

enter image description here

Cells

This is much trickier but here's what I got:

CellPrint[
 Cell["Test content", "Text",
  CellFrame -> True,
  CellFrameLabelMargins -> {{0, 0}, {0, 0}},
  CellMargins -> {{25, 55}, {5, 5}},
  CellBracketOptions -> {
    "Widths" -> {0, 15},
    "OverlapContent" -> True
    },
  CellFrameLabels ->
   {
    {None, None},
    {None,
     Cell["Test label", "Text",
      Background -> Blue,
      FontColor -> White,
      CellFrame -> {{1, 1}, {0, 1}},
      CellMargins -> 0,
      CellFrameMargins -> {{50, 50}, {5, 5}},
      CellSize -> {
        FEPrivate`Part[

           FrontEnd`AbsoluteCurrentValue[
            FrontEnd`EvaluationNotebook[], WindowSize],
           1
           ]/
          FrontEnd`AbsoluteCurrentValue[
           FrontEnd`EvaluationNotebook[], Magnification] - (
          FEPrivate`Part[
            FrontEnd`AbsoluteCurrentValue[
             FrontEnd`ParentCell@FrontEnd`EvaluationCell[], 
             CellMargins
             ],
            1, 1
            ] +
           FEPrivate`Part[
            FrontEnd`AbsoluteCurrentValue[
             FrontEnd`ParentCell@FrontEnd`EvaluationCell[], 
             CellMargins
             ],
            1, 2
            ] +
           (* dunno where the fudge factors here are coming from...*)

                      FEPrivate`If[
            FEPrivate`SameQ[
             FrontEnd`AbsoluteCurrentValue[
              FrontEnd`ParentCell@FrontEnd`EvaluationCell[], 
              {CellBracketOptions, "OverlapContent"}
              ],
             True
             ],
            0,
            FEPrivate`Part[
              FrontEnd`AbsoluteCurrentValue[
               FrontEnd`ParentCell@FrontEnd`EvaluationCell[], 
               {CellBracketOptions, "Widths"}
               ],
              2
              ] +

             3/FrontEnd`AbsoluteCurrentValue[
               FrontEnd`EvaluationNotebook[], Magnification]
            ] +
           FEPrivate`If[
            FEPrivate`MemberQ[
             FrontEnd`AbsoluteCurrentValue[
              FrontEnd`EvaluationNotebook[], 
              WindowElements
              ],
             "VerticalScrollBar"
             ],

            15/FrontEnd`AbsoluteCurrentValue[
              FrontEnd`EvaluationNotebook[], Magnification],
            0
            ]
          ),
        Automatic
        }
      ]
     }
    }
  ]
 ]

You'll notice there are many more fudge factor here to tweak, but it seems to again handle the many settings I've tried and it looks like:

enter image description here

$\endgroup$
0
$\begingroup$

Maybe you can use CellLabel instead of CellFrameLabels. The tricky part is that CellLabel doesn't seem to be settable from a stylesheet. To workaround that issue, you can use CellDynamicExpression. Also, the default font for the "CellLabel" style seems to get clipped at the top, so I changed the font. I think the following modification to the "Text" style does what you want:

Cell[StyleData["Text"],
    CellDynamicExpression :> SetOptions[
        EvaluationCell[],
        {CellLabel->"Test label", CellDynamicExpression :> None}
    ],
    CellLabelStyle->Directive["CellLabel", FontFamily->"Verdana"],
    CellMargins -> {{66,10},{7,8}},
    CellLabelMargins -> {{66,0},{0,0}},
    CellLabelPositioning -> Above,
    CellLabelAutoDelete -> False,
    CellFrame -> True
]

Example:

enter image description here

If you need the label framed as well, then I don't think you can use this approach.

$\endgroup$

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.