I have created the following style in a private stylesheet that applies to gridbox structures for matrices. So far I have

Cell[StyleData["DisplayMatrix", StyleDefinitions -> StyleData["DisplayFormula"]],
 GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {Baseline}},
 GridBoxSpacings->{"Columns" -> {Offset[0],{.9},Offset[0]}, "Rows" ->  

As you can see I have GridboxAlignment set to "Center" for columns which is what I want. However, the issue I have is that there is an exception to what I want. I don't want negative signs considered when centering. For example, using the style above I can create a matrix that looks like this

enter image description here

However, I want it to look like this

enter image description here

Notice the second column. The items are centered on the k2 part and not the entire -k2. That is, the k2 and the k2 are aligned. This is my desired behavior. I would like to create a style that always centers columns except for the case when a negative appears. In that case, I want it to still center but do so in a way that ignores the negative.

My motivation for this is that all textbooks I see follow this behavior and it looks nicer. I can't seem to figure out how to create a style that handles this "hiccup" with negatives automatically. Yes, I know I can fix these issues manually per entry, but I would like to create a style that handles negative terms automatically as I am creating a ton of these matrix structures. Any ideas?

Edit*: A boxobject equivalent to the NumberSigns option for the function NumberForm[] might work, if there is such a thing. Notice

    Grid[{{NumberForm[{1, -1}, 5, NumberSigns -> {"-", " "}]}}, 
 Alignment -> Center]

puts a space (" ") placeholder in front of positive numbers whenever they appear. I could exploit this to get my desired result. Unfortunately, I can't find a boxobject equivalent as I intend to use GridBox.


  • 1
    $\begingroup$ I would not even know how to do it in LaTeX, except manually tweaking each matrix. Actually, you may ask at tex.stackexchange.com on what they are thinking about this formatting style. $\endgroup$
    – yarchik
    Nov 14, 2021 at 7:08
  • $\begingroup$ @yarchik Thank you. Unfortunately, I'm not familiar with LaTex, and don't know enough to even pose the question well there. I did find the NumberSigns option for the NumberForm function which could do what I want but it doesn't work on boxobjects. See edit above. $\endgroup$
    – B flat
    Nov 14, 2021 at 8:13
  • $\begingroup$ something like Style[Grid[{{-Subscript[m, 1] - Subscript[m, 2], Subscript[m, 123]}, {Item[Subscript[m, 2], Alignment -> Center], -Subscript[m, Row[{2, Invisible["23"]}]]}}, Alignment -> m], "DisplayFormula"]? $\endgroup$
    – kglr
    Nov 17, 2021 at 22:18
  • $\begingroup$ @kglr yes except I’m looking for a stylesheet solution that handles negative terms automatically. $\endgroup$
    – B flat
    Nov 18, 2021 at 0:30
  • $\begingroup$ How about setting a CellEvaluationFunction to automatically insert spaces or \[AlignmentMarker] in the GridBox when the cell is evaluated? $\endgroup$
    – rnotlnglgq
    Nov 21, 2021 at 12:16


Your Answer

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

Browse other questions tagged or ask your own question.