Wrap title in Gnuplot

gnuplot

It's all in the subject.
I use a (long) line from the data file as a title, and Gnuplot doesn't wrap it.

I don't know the contents of the line in advance,
so I don't know how I can just insert a \n.
How can I make Gnuplot wrap the title?

Best Answer

You should edit the data in your file and insert a \n in the field where the title is parsed from.

Gnuplot will recognize the \n and break the line at that point. Otherwise you need to manually enter the title and add the \n at your desired position.

It is important to note, that \n is only considered by gnuplot if you have "string" instead of 'string'.

If you can't edit the data in your file, you could use the string functions in gnuplot. With strlen("string") you can find the length of your string and then with substr("string",begin,end) you could split your strings.

Here is a complete list of gnuplot functions:
http://gnuplot.sourceforge.net/docs_4.2/node53.html

In summary you would have something like

foo = "my data"
length = strlen(foo)                   #get string length
length2 = sprintf("%1.d", length / 2)  #round halved down string length to find center char
foo1 = substr(foo, 0, length2)         #get first half of string
foo2 = substr(foo, length2, length)    #get second half of string
titlestring = foo1.\n.foo2             #make new title string with newline character 

For more details refer to the string variable demo script of gnuplot:
http://gnuplot.sourceforge.net/demo/stringvar.html

where examples like these are given:

#
# Miscellaneous neat things you can do using the string variables code
#
set print "stringvar.tmp"
print ""
print "Exercise substring handling"
print ""
beg = 2
end = 4
print "beg = ",beg," end = ",end
foo = "ABCDEF"
print "foo           = ",foo
print "foo[3:5]      = ",foo[3:5]
print "foo[1:1]      = ",foo[1:1]
print "foo[5:3]      = ",foo[5:3]
print "foo[beg:end]  = ",foo[beg:end]
print "foo[end:beg]  = ",foo[end:beg]
print "foo[5:]       = ",foo[5:]
print "foo[5:*]      = ",foo[5:*]
print "foo[:]        = ",foo[:]
print "foo[*:*]      = ",foo[*:*]
print "foo.foo[2:2]  = ",foo.foo[2:2]
print "(foo.foo)[2:2]= ",(foo.foo)[2:2]
print ""
print "foo[1:1] eq 'A' && foo[2:2] ne 'X' = ", \
      (foo[1:1] eq 'A' && foo[2:2] ne 'X') ? "true" : "false"

unset print

set label 1 system("cat stringvar.tmp") at graph 0.1, graph 0.9
unset xtics
unset ytics
set yrange [0:1]
plot 0

Together with the function strlen you should be able to build your solution to automatically process your data in gnuplot and place the newline character where you want it (e.g. after every 20th character).

If you want to break at words, you would use:

wordnumber = word(foo)                           #get number of words
wordernumber2 = sprintf("%1.d", wordnumber / 2)  #round down half of #words
do for [i=0:wordnumber2] {                       #add half of words to string
    string = string.word(foo,i)
}
string = string.\n                               #add newline to string
do for [i=wordnumber2:wordnumber] {              #add 2nd half of words
    string = string.word(foo,i)
}

This requires gnuplot 4.4+

Related Question