# Add common string to LaTeX string as legend name

I want to add a variable, which is a common string, to LaTeX string:
Here’s the example:

import plotly.plotly as py
import plotly.graph_objs as go

a = 'ssss'

trace1 = go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 3, 6, 4, 5, 2, 3, 5, 4],
name = '$\pm$'+a
)
trace2 = go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 4, 7, 8, 3, 6, 3, 3, 4],
name=r'$\pm \text{{{}}}$'.format(a)
)
data = [trace1, trace2]

plotly.offline.plot(data, include_mathjax='cdn')


Here’s the result:

Using \text{{{}}} isn’t convenient, is there any other possible method to add them together?

Hi @zxdawn,

Right now LaTeX typesetting is preformed on the entire string, not on individual subsets so the \text approach is the best way as far as I know.

For cases where you only need special characters and super/subscripts, you could also use HTML formatting. For example, the HTML code for the “plus minus” symbol is &plusmn; (See https://www.toptal.com/designers/htmlarrows/math/plus-or-minus-sign/). So you could specify these strings as

name='&plusmn; ' + a


Hope that helps!
-Jon

1 Like