3
$\begingroup$

I can write a cell with the text alignment on the right:

NotebookWrite[InputNotebook[], 
 Cell["On the right", "Subsection", TextAlignment -> Right]]

or likewise on the Left or in the Center.

As I change the display width of the cell, the alignment behaves as expected. Great.

My request, though, is to place text at the same elevation or line in a SINGLE cell, some of it left justified, some of it center justified, and some of it right justified, which retains the justification as I interactively change the width of the notebook window, and hence of the cell. (This requirement of being on the same line precludes "solutions" that place three separate rows atop each other, each with a separate TextAlignment.)

This attempt doesn't work (for fairly obvious reasons), but it may clarify what I'm seeking:

NotebookWrite[InputNotebook[],
 BoxData[RowBox[
   {Cell["make left justified", "Subsection", TextAlignment -> Left],
    Cell["make center justified", "Subsection", TextAlignment -> Center], 
    Cell["make right justified", "Subsection", TextAlignment -> Right]}]]]
$\endgroup$

2 Answers 2

4
$\begingroup$
expr = Column[{Pane["make left justified", Full, Alignment -> Left], 
    Pane["make center justified", Full, Alignment -> Center], 
    Pane["make right justified", Full, Alignment -> Right]}];
NotebookWrite[InputNotebook[], Cell[BoxData[ToBoxes@expr], "Subsection"]]

screenshot


The lines must be on the same elevation, not one beneath the other.

expr = Overlay[{Pane["left", Full, Alignment -> Left], 
    Pane["center", Full, Alignment -> Center], Pane["right", Full, Alignment -> Right]}];
NotebookWrite[InputNotebook[], Cell[BoxData[ToBoxes@expr], "Subsection"]]

screenshot

Alternatively,

expr = Grid[{{Pane["left", Full, Alignment -> Left], 
     Pane["center", Full, Alignment -> Center], Pane["right", Full, Alignment -> Right]}}];
NotebookWrite[InputNotebook[], Cell[BoxData[ToBoxes@expr], "Subsection"]]

screenshot

$\endgroup$
3
  • $\begingroup$ Thanks but no. The lines must be on the same elevation, not one beneath the other. (I'll clarify in the question.) $\endgroup$ Aug 19, 2022 at 4:40
  • $\begingroup$ @DavidG.Stork I updated the answer. $\endgroup$ Aug 19, 2022 at 4:43
  • $\begingroup$ Hah! Very clever. OVERLAP three cells. Wish I had thought of that. Thanks. ($\checkmark$) $\endgroup$ Aug 19, 2022 at 4:44
3
$\begingroup$

Another possibility is:

Pane[
    Grid[
        {{left, center, right}},
        ItemSize->Fit,
        Alignment->{{Left, Center, Right}, Baseline}
    ],
    ImageSize->Full
]
$\endgroup$
4
  • $\begingroup$ Oh... I was unaware of ItemSize->Fit. Very useful. (+1) $\endgroup$ Aug 19, 2022 at 17:37
  • 1
    $\begingroup$ Removing the Pane I get the same output. $\endgroup$ Aug 19, 2022 at 19:44
  • 1
    $\begingroup$ ItemSize->Fit seems to be undocumented. I cannot find it on either the Docs page for Grid or ItemSize. $\endgroup$ Aug 20, 2022 at 0:41
  • $\begingroup$ So basically instead of having to type ItemSize -> {Scaled[1/3], Scaled[1/3], Scaled[1/3]}, like I have doing for a long time, I can now just type ItemSize -> Fit. This is nice, reminds me of LaTeX hfill. By the way, ItemSize -> "Fit" also works... $\endgroup$ Aug 22, 2022 at 14:56

Your Answer

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

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