CellDingbat is below the first line of the Cell
According to the Documentation, "Dingbats are placed to the left of the main contents of a cell, aligned with the first line of the contents." But when I evaluate
CellPrint@Cell[BoxData["Text"], CellDingbat -> "CellDingbat"]
in a Notebook with the "Default"
stylesheet I get
One can see that the CellDingbat
is placed lower than the fi开发者_如何学编程rst line of the Cell
. Why this happens? Is there a way to align CellDingbat
with the first line of the Cell
?
It looks like the baseline of the dingbat and cell contents don't change in the same way when the cell contents are wrapped in BoxData
. As Sjoerd pointed out, removing the BoxData
from the cell contents makes things line up, but adding BoxData
to the CellDingbat
does not seem to change anything.
The baseline can be shifted using an AdjustmentBox
and after playing around for a while, I found that the baseline is out by CurrentValue["FontAxisOffset"]/CurrentValue["FontMWidth"]
.
Both of these terms change with the Magnification
but it turns out that the observed offset is the constant value obtained when Magnification->1
. In this case, the ratio is 0.20833 == 5/24
.
I'm not sure if the problem really is with the baseline and whether there is a good underlying reason for the behaviour. I'm also not sure why the required shift does not depend on the magnification of the Cell, Notebook or $FrontEnd. But it seems to work and I've spent enough time playing around with it!
So to test that everything is (basically) ok, try
Do[CellPrint@Cell[BoxData["TxyT"],
CellDingbat -> BoxData[AdjustmentBox["TxyT", BoxBaselineShift -> -5/24]],
Magnification -> mag], {mag, 1, 5}]
And also test how
CellPrint@Cell[BoxData["TxyT"],
CellDingbat -> BoxData[AdjustmentBox["TxyT", BoxBaselineShift -> -5/24]],
Magnification -> Inherited]
looks for various magnifications of the containing notebook and frontend, e.g.,
SetOptions[EvaluationNotebook[], Magnification -> 4.]
and/or
SetOptions[$FrontEnd, Magnification -> 4.]
CellLabel instead of CellDingbat
Dingbats are normally little icons, not text. By design, they are positioned somewhat under the baseline of the contents of the first line of the cell. To my eye, they look fine that way.
If you want a label for the cell, instead of a Dingbat, use CellLabel
:
CellPrint@Cell[BoxData["Text"], CellLabel -> "Example"]
You can handle the left allignment through CellMargins
.
If you prefer the label on the left, have the cell formatted as Output
, but notice that the alignment will depend on the screen magnification as the following 3 examples show:
Provided you don't want the CellDingbat
on a standard "Input" or "Code" cell style, then as pointed out by Sjoerd, the cell's contents and dingbat automatically align. It's only when you wrap the cell contents in BoxData
that they become misaligned.
If the cell you want is a "Text" or "Section" (etc) cell, then you can still have formatted text provided the cell contents are contained in TextData[...]
. For example
Note that I've started the text and ended the dingbat with a capital T
so that the alignment is clear.
If you hand make your "Input" cells to have their contents wrapped in TextData
(which does not happen by default) then the code will run, but the styling is not quite right. The spacing is wrong and syntax highlighting not active. Compare the following
The first printed cell has syntax highlighting but the cell dingbat is misaligned, while the second printed cell has no syntax highlighting and poor spacing, but the dingbat is perfectly aligned!
Finally, I should have realised that a solution like this would work, because I've been using cell dingbats for automatic section numbering for ages and have never had alignment problems. E.g., here's the outline of a notebook I was working on last week that contains supporting code for a chapter in my thesis:
The section and subsection cells are given dingbats in the stylesheet, e.g.,
CellDingbat->TextData[{CounterBox["Section"], ". "}]
.I noticed that if you replace BoxData
with TextData
(or remove BoxData
, leaving "Text") the dingbat and the cell contents are aligned.
Does specifying the CellBaseline
have any effect?
As in
CellPrint[
Cell[BoxData["Text"], CellDingbat -> "CellDingbat",
CellBaseline -> Bottom]]
rather than the default CellBaseline -> Baseline
精彩评论