3

I want to wrap legend text after a certain amount of characters. Is there an automatic way to do that?

I don't want to enter a character (e.g. *) for each line as described in this post, that is too much work...

enter image description here

Edit: I tried to use an expression, but the button is greyed out...

enter image description here

Taras
  • 32,823
  • 4
  • 66
  • 137
Enzo Baldini
  • 2,327
  • 1
  • 15
  • 29

2 Answers2

4

You could use an expression string for your labels:

enter image description here

with_variable( 'column',37,
   if(length(@symbol_label) > @column,
      regexp_replace(left(@symbol_label,@column),'(.+)([\\s,;\\.])([^\\s,;\\.]+)$','\\1\\2\n\\3') + substr(@symbol_label,@column+1),@symbol_label))

Without expression string: enter image description here

With expression string: enter image description here

christoph
  • 5,605
  • 1
  • 20
  • 34
3

Since QGIS 2.4 you can use wordwrap function.

For 50 characters it would be like this:

wordwrap(@symbol_label, 50)
  • This is a solution for some projects, but to me, using qGIS 3.26-3.28, the wordwrap-expression does not work reliably for an atlas (https://github.com/qgis/QGIS/issues/51465). However, adding a special character to the legend, as shown in the initial post seems to be more reliable (https://gis.stackexchange.com/questions/158295/getting-legend-label-on-multiple-lines-in-qgis, and here https://gis.stackexchange.com/questions/297354/making-long-labels-visible-in-a-legend) – Beni Jan 13 '23 at 10:21