# Programatically align text in the Front End

One can select a cell and using the Front End menu perform

Format -> Text Alignment -> Align at 25%

Other options are given as well.

I have learned from Stack Exchange how to create a button and modify some text. For example I can select some text and click the button created by this code

CreatePalette[
Button["Highlight",
FrontEndTokenExecute["FontFamily", "Arial"];
FrontEndTokenExecute["FontColor", Blue];
FrontEndTokenExecute["FontSlant", "Italic"];
FrontEndTokenExecute["FontWeight", "Bold"];
],
WindowTitle -> "Highligh Text"
]


in order to change its font to blue and face to italic.

I would like to do the same thing except aligning a cell. I would also like the ability to control the alignment percentage.

I have searched the Mathematica Stack Exchange and can't find a similar question.

When I look at all the possibilities as described in What is the complete list of valid Front End Tokens I come across some alignment properties (e.g., AlignCentersHorizontally) but they do not work on a cell.

Anybody have a clue as to how to do this?

An example of how to set the cell option Styles can be found here.

The question is what is the token used to set the cell alignment?

To answer this create some five cells and then manually set the cell alignment to Align Left, Align at 25%, Align Center, Align at 75% and Align Right using the Format menu and Text Alignment row.

Now select a cell and go to Option Inspector... -> Formatting Options -> Text Layout Options.

In that group one Option is TextAlignment.

Studying the values for the example cases shows:

It is clear from this data that Left alignment corresponds to -1 and Right alignment to +1. Linear interpolation gives the rest of the results (e.g., Align at 25% corresponds to -0.5).

Thus to create a palette with buttons to align the text one could use:

CreatePalette[
Column[
{
Button[
"Align at 10%",
FrontEndTokenExecute[InputNotebook[],
"TextAlignment", -0.8]
],
Button[
"Align at 25%",
FrontEndTokenExecute[InputNotebook[],
"TextAlignment", -0.5]
]
}
],
WindowTitle -> "Align Cells"
]


Now select a cell and click the appropriate button and the text is aligned.

Note the -0.8 value to align text at 10%.

It is OK to explicitly use the tokens Left, Center and Right. One gets the same result if -1 is used for Left, 0 for Center and 1 for Right.