# Plots using TextAlignment with SwatchLegend to left align multi-line items

I have Legends that stretch over multi-lines and the only way I can get them left aligned is by adding white space to the end of the legend item. Does anyone know if there is a way to use TextAlignment with SwatchLegend?

gJ = Plot[Sin[x], {x, 1, 12},
PlotLegends -> {Placed[
SwatchLegend[{RGBColor[1, 0.5, 0], RGBColor[0., 0.329412, 0.],
RGBColor[0.619608, 0.25098, 1.],
RGBColor[0, 1, 1]}, {"it ends up centred", "but ",
"fox jumped over two lines that I want to \nalign to the left",
"A quick brown"},
LegendMarkers -> {Graphics[{EdgeForm[], Rectangle[]}],
Graphics[{EdgeForm[], Rectangle[]}],
Graphics[{EdgeForm[], Rectangle[]}],
Graphics[{EdgeForm[], Rectangle[]}],
Graphics[{EdgeForm[], Rectangle[]}]},
LegendLayout -> "ReversedColumn", LegendMarkerSize -> 16,
LabelStyle -> {FontFamily -> "Arial", 18},
Spacings -> {0.5, 0.25}], {0.4167, 0.7701}]}]


-
This is an interesting question. Is it also an issue for LineLegend? –  Verbeia May 14 '14 at 10:46

The fastest way is to use TextCell only there you can have full control over the text:

lbl = TextCell["fox jumped over two lines that I want to\nalign to the left",
TextAlignment -> Left];


So:

Plot[Sin[x], {x, 1, 12}, ImageSize -> 500, Frame -> True, Axes -> False,
PlotLegends -> {Placed[SwatchLegend[{Red}, {lbl},
LabelStyle -> {FontFamily -> "Arial", 18},
LegendMarkerSize -> 16],
{0.4167, 0.7701}]}]


Related usage of TextJustification on StarWars theme: 47273

-
Since I am programmatically generating the Legend command with a GUI your answer will be the easiest way. Thanks. –  WolframFan May 14 '14 at 6:24
@WolframFan I'm glad you find it useful. It looks simple but it took me a lot of health to realise that painless text formatting without TextCell is impossible :P –  Kuba May 14 '14 at 6:27
Would Pane work as well? @WolframFan -this should probably be automated in the underlying legend-creating code if possible. –  Verbeia May 14 '14 at 10:44
@Verbeia Yes, this can be done multiple ways. But I wanted to advertise TextCell since you can't use e.g. TextJustification without this. So we can use TextCell to text formatting or we can use shorter ways but not general. –  Kuba May 14 '14 at 10:47
@Kuba of course, but isn't Alignment and Pane equivalent to this? –  Verbeia May 14 '14 at 11:20

Adapting an example in the docs you could do it this way:

table[pairs_] := Grid[pairs, BaseStyle -> {TextAlignment -> Left}, Alignment ->
{Left, Automatic}]


Then use LegendLayout -> table, to give you:

Edit

To reverse it just use Reverse:

reversetable[pairs_] := Grid[Reverse@pairs, BaseStyle -> {TextAlignment -> Left},
Alignment -> {Left, Automatic}]


LegendLayout -> reversetabletable,

-
@WolframFan This one might be easier to automate, since the value of the LegendLayout Option can be set in any underlying package. –  Verbeia May 14 '14 at 10:45
I just realised you can't reverse this. Notice that the OP had LegendLayout -> "ReversedColumn" –  Verbeia May 14 '14 at 11:22
@Verbeia to reverse it why not use Reverse :) –  Mike Honeychurch May 14 '14 at 22:05