Adding a tab to label via label expression

2829
7
Jump to solution
07-18-2014 09:25 AM
JasonTaylor
New Contributor

Adding new lines to a python label expression is trivial - just build a string with "\n" where you want the new line.

In a similar way, using "\t" doesn't add a tab. Does anyone know a work around? How can I add tabs to a label via python label expression.

Thanks,

0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

You could use string formatting to make sure that all labels have the same spacing if you are adding multiple fields to your string.

  1. def FindLabel():  
  2.     x = "cats"  
  3.     y = "dogs"  
  4.     a = "cat"  
  5.     b = "dog"  
  6. return x.ljust(10) + y.ljust(10)  + "\n" + a.ljust(10) +   b 

So for any length of string for x, y and a, it would add spaces after to make the entire string length 10, and be left justified.  This way all your labels would have same length and be lined up.

I haven't tried yours out yet, but this does add the spaces in my own sample map I made.

7.1. string — Common string operations — Python v2.7.8 documentation

EDIT:

Actually both using multiple spaces and using tabs makes wider spaces in my labels in ArcMAP then using a single space. "\t" looks about 4 spaces as opposed to a single " ".  I'm on ArcGIS 10.2.0.3348

View solution in original post

0 Kudos
7 Replies
IanMurray
Frequent Contributor

It does add tabs, you have to remember though if you have a small font, that a single tab won't make much of a space between characters in your string(or an indent at the beginning).  If you need more spacing, use multiple tabs.

0 Kudos
JasonTaylor
New Contributor

I don't think that's how tabs are supposed to work. To illustrate my example further try the following function in a python window.

def FindLabel():

x = "cats"

y = "dogs"

a = "cat"

b = "dog"

return x + "\t" + y  + "\n" + a + "\t" + b

print FindLabel()

See how the spacing is as you would expect, where the row elements justify along tab values.

Try this label express against any feature class. See how it doesn't behave the same. It treats the tab value as a space.

def FindLabel():

x = "cats"

y = "dogs"

a = "cat"

b = "dog"

return x + "\t" + y  + "\n" + a + "\t" + b

0 Kudos
CharlieFrye
Esri Contributor

Similarly, you could add five spaces "     ".  Using that notion, you could more flexibly manage columns of text within a label by introducing the best number of spaces to ensure alignment within columns based on the length of the string in the first column.

0 Kudos
JasonTaylor
New Contributor

Ah yes, repeated spaces. Like you, I thought that it would work, however try the following code in the python window and in a feature class's label expression. Comparing the two you will see that it does not.

Python window:

def FindLabel():

    x = "cats"

    y = "dogs"

    a = "cat"

    b = "dog"

    return x + " " + y  + "\n" + a + "     " + b

print FindLabel()

Python label expression:

def FindLabel():

    x = "cats"

    y = "dogs"

    a = "cat"

    b = "dog"

    return x + " " + y  + "\n" + a + "     " + b

I would love to write a function that does the tab work for me, by reading the length of the string before it then applying spaces according to it's remainder of the length divided by 5. It's a lot of work but I would do it if I thought it would work. It won't because the python label expression interpreter seems to ignore repeated spaces.

I think this is another short coming of python label expressions that I imagine will get worked out in later versions - hopefully since we are discussing it here. Adding columns to labels is huge in my work, I've been wanting this feature for years.

Just curious, are folks getting a different behavior then I described running the above code?

0 Kudos
IanMurray
Frequent Contributor

You could use string formatting to make sure that all labels have the same spacing if you are adding multiple fields to your string.

  1. def FindLabel():  
  2.     x = "cats"  
  3.     y = "dogs"  
  4.     a = "cat"  
  5.     b = "dog"  
  6. return x.ljust(10) + y.ljust(10)  + "\n" + a.ljust(10) +   b 

So for any length of string for x, y and a, it would add spaces after to make the entire string length 10, and be left justified.  This way all your labels would have same length and be lined up.

I haven't tried yours out yet, but this does add the spaces in my own sample map I made.

7.1. string — Common string operations — Python v2.7.8 documentation

EDIT:

Actually both using multiple spaces and using tabs makes wider spaces in my labels in ArcMAP then using a single space. "\t" looks about 4 spaces as opposed to a single " ".  I'm on ArcGIS 10.2.0.3348

0 Kudos
JasonTaylor
New Contributor

Couple of things here:

  • Ian your solution works, only when the Maplex engine is off. Turn maplex on and the justifications go away.
  • Maplex is also ignoring the multiple spaces too. Turn Maplex off and the print as expected, as my example above.
  • Finally the "\t" is still treated as a space with Maplex off, so no dice there.

I think we have our solution. You can get tab like effects using the string.ljust() method, but only with Maplex off. Also, as you alluded to earlier, you have to use a even spaced font, something like courier. Ian, if you change your answer to include the Maplex caveat I will mark it as the answer!

0 Kudos
IanMurray
Frequent Contributor

You can also center or right justify with string.center or string.rjust.  Glad you found a solution that works, though its not entirely ideal.  Maplex tends to do what it wants to make your label "fit best" so it doesn't surprise me that it remove extra spaces. 

0 Kudos