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?

  • 1
    Would you please add an example?
    – egreg
    Jul 10, 2012 at 8:41
  • 3
    At the moment, this sounds like it's not related to (La)TeX at all. Could you specify how the question is relevant to the site?
    – Jake
    Jul 10, 2012 at 8:45
  • @Jake: oops, this sounds like you're right ! how to remove this question ? Where does it belong ? StackOverflow ?
    – ExpertNoob
    Jul 11, 2012 at 17:22
  • 1
    Use \n; thus set title "this is a\n two line title".
    – Ian Thompson
    Jul 12, 2012 at 22:37
  • @Jake: thanks. Yes I'm looking for automatic wrapping. I don't know the length of the line in advance (or even it's contents). So, Ian, I wouldn't know where to put the \n ! An example : plot sin(x) with title = "ExpertNoob " 15 times !
    – ExpertNoob
    Jul 13, 2012 at 10:00

1 Answer 1


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:

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:

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+

  • Actually, this solution was raised in a comment, and the OP said, "I'm looking for automatic wrapping. I don't know the contents of the line in advance, so I wouldn't know where to put the \n!" Nov 15, 2015 at 20:53
  • I've refined my answer, which should include a solution to cutting, pasting and wrapping strings. Nov 15, 2015 at 21:13
  • Much better.  But, if I understand your answer correctly, it would break "The Hitchhiker's Guide to the Galaxy" into "The Hitchhiker's G" and "uide to the Galaxy".  Word wrapping generally means breaking at a space. Nov 15, 2015 at 21:50
  • You can break with words as follows:<br> words("string") returns the number of words of a string. word("string",n) returns the nth-word in the string. However, long words (especially in german, but also compound words in english) could potentially exceed the number of characters allowed in a single line. Therefore cutting based on characters is better for an unknown string. With words this would result in a complex program, which I would rather attempt in script languages like perl/python. Pre-processing the files, inserting newlines (additional to other formatting) in a simpler way. Nov 15, 2015 at 22:22
  • I've added an example which would process words and add a newline after half the words. It has problems if half of the words is still to long for a single line, but you should get the idea on how to expand that ;-) Nov 15, 2015 at 22:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .