31

Short question: How do I display the _ (underscore) character in a title in gnuplot that is assigned from a variable name in gnuplot?

Details: I have something like the following code:

items = "foo_abc foo_bcd bar_def"
do for [item in items] {
  set title item
  set output item.eps
  plot item."-input.txt" using 1:2 title item with linespoints
}

This works fine with gnuplot except that the title get changed from foo_abc to fooabc. I don't know if I want to use an escape character because I don't want that to be in the file name. I've tried a couple of different options with single vs. double quotes but I haven't found what I need yet.

choroba
  • 231,213
  • 25
  • 204
  • 289
Gabriel Southern
  • 9,602
  • 12
  • 56
  • 95
  • In your case, will the underscore always be at the same place in the name of the string? That is, will len('foo')==len('bar')? I may have a partial solution in that case. – andyras Dec 01 '12 at 00:55
  • no those just happened to be in the examples I gave. – Gabriel Southern Dec 01 '12 at 01:36
  • 1
    OK. Did you try using the `noenhanced` option to the postscript terminal? – andyras Dec 01 '12 at 01:52
  • I just tried it and that seems to work for now although I don't know what the difference between enhance and noenhanced is or if I'll need those features later. Either way your answer explains what's happening and I could use some other technique to get names with and without the escape character if I need to. thanks. – Gabriel Southern Dec 01 '12 at 02:01

5 Answers5

31

Instead of foo_abc, write foo\\\_abc.

Morteza
  • 441
  • 1
  • 6
  • 14
26

Most gnuplot commands which generate labels accept a noenhanced keyword which will prevent gnuplot from using enhanced text for just that string. In this case, it should be sufficient to just do:

set title item noenhanced

An alternative is to create a function which will remove the unwanted text from the string when passing it to set output:

remove(x,s)=(i0=strstrt(s,x),i0 ? remove(x,s[:i0-1].s[i0+strlen(x):]):s)
# Makes me wish gnuplot syntax was more pythonic :-p
#TODO:  Write a `replace` function :-).  These just might go into my ".gnuplot" file...

I use an inline function to find the index of the first occurrence of x in the string s. I then remove that occurrence via string concatenation and slicing and recursively call the function again to remove the next occurence. If the index isn't found (strstrt returns 0) then we just return the string that was put in. Now you can do:

set output remove('\',item)
set title item
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 1
    Excellent answer! I wasn't aware of the `noenhanced` flag for individual labels. I wanted to make something like your recursive function, but somehow I missed the `strstrt` function. – andyras Dec 01 '12 at 03:43
  • @andyras -- I was pretty proud of the `remove` function -- hence my comment on maybe putting it in my `~/.gnuplot` file :) – mgilson Dec 01 '12 at 04:35
18

The underscore comes from treating titles as "enhanced text". Turn that off using

set key noenhanced

Ethan
  • 13,715
  • 2
  • 12
  • 21
  • Perfect: straight to the point and doesn't clumsily remove enhancements anywhere else when the OP only talked about the titles. – underscore_d Jan 27 '18 at 22:31
17

If you are using the enhanced eps terminal, that is the reason you need to escape the underscore in the first place. There was another related question today which explains the issue a bit. When you set the terminal, try:

set terminal postscript noenhanced <whatever else here...>

That works for me (Arch linux, gnuplot 4.7.0). If the enhanced terminal is essential, below is a partial solution I found. The assumption is that the underscore always appears in the same place in the string.

set terminal postscript enhanced
items = 'foo\_abc foo\_bcd bar\_def'
do for [item in items] {
  set output item[1:3].item[5:*].'.eps'
  set title item
  plot sin(x)
}

This way you can escape the underscore and not have the \ appear in the filename. Note the use of single quotes for the 'items' string; see the previously linked question for details.

Community
  • 1
  • 1
andyras
  • 15,542
  • 6
  • 55
  • 77
  • 1
    You can remove the restriction of having ` \ ` in the same location by using an inline recursive function. (the inline part could be relaxed too ... but it helps to not evaluate the `strstrt` function repeatedly :). Good answer though. +1 for giving me an idea to take a little further. – mgilson Dec 01 '12 at 03:06
  • This also works with `set key autotitle columnhead noenhanced` when your CSV file provides the plot keys in the first row. – remcycles Mar 26 '21 at 22:50
0

I had the same problem about the underscore in the title: such as I needed to write 4_3 subframe and I needed the enhanced postscript. The SIMPLEST way turned out to be from the adjacent post: ``If you are using the enhanced eps terminal, that is the reason you need to escape the underscore in the first place. There was another related question today which explains the issue a bit." - How is @ produced in gnuplot? So, I followed their advice and this worked:

plot 'LC.stats' u 3:4 ti "{/=15 1350 stars in C18 4\_3 subframe}" - Double escape character before the underscore.

Community
  • 1
  • 1