7
$\begingroup$

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}]}] 

enter image description here

$\endgroup$
1
  • $\begingroup$ This is an interesting question. Is it also an issue for LineLegend? $\endgroup$ Commented May 14, 2014 at 10:46

2 Answers 2

5
$\begingroup$

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}]}] 

enter image description here

Related usage of TextJustification on StarWars theme: 47273

$\endgroup$
6
  • $\begingroup$ Since I am programmatically generating the Legend command with a GUI your answer will be the easiest way. Thanks. $\endgroup$ Commented May 14, 2014 at 6:24
  • 1
    $\begingroup$ @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 $\endgroup$ Commented May 14, 2014 at 6:27
  • $\begingroup$ Would Pane work as well? @WolframFan -this should probably be automated in the underlying legend-creating code if possible. $\endgroup$ Commented May 14, 2014 at 10:44
  • $\begingroup$ @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. $\endgroup$ Commented May 14, 2014 at 10:47
  • $\begingroup$ @Kuba of course, but isn't Alignment and Pane equivalent to this? $\endgroup$ Commented May 14, 2014 at 11:20
5
$\begingroup$

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:

enter image description here

Edit

To reverse it just use Reverse:

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

LegendLayout -> reversetabletable,

enter image description here

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.