Display underscore rather than subscript in gnuplot titles - gnuplot

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.

Instead of foo_abc, write foo\\\_abc.

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

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

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.

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.

Related

How to obtain tilde or approx symbol in gnuplot legend?

I want to use either tilde (~) or approximate symbol (in latex \approx) in the legend of my gnuplot.
I am using epscairo terminal.
I tried seting either:
set encoding iso_8859_1
or
set encoding utf8
and using different commands, for example \176 as descbribed here (http://ayapin-film.sakura.ne.jp/Gnuplot/Docs/ps_guide.pdf), but nothing seems to work.
Thanks in advance! :)
It seems '\~' or "\\~" works for me for tilde. "{/Symbol \273}" produces ≈.

Gnuplot error on command "set data s l"

My program has gnuplot script and its first line is
set data s l
But this makes error
"cdia.GNUBAND", line 1: unrecognized option - see 'help set'.
I think it's the version problem and I have to change the "set data s l" command in right way. I searched on web but couldn't find it.. How do I solve this?
That is the shortcut for set data style lines, which is deprecated since version 4.0 (released in 2004). To replace this line, use
set style data lines
This sets the default plotting style for data set to lines, i.e. the commands
set style data lines
plot 'data.dat'
and
plot 'data.dat' with lines
are equivalent.

Multi-line plot titles in Pyxplot

Is it possible to make a multi-line plot title in Pyxplot? I do understand that the "set title" command accepts a LaTeX string and have tried to inject a newline in many ways.
Ultimately it seems that Pyxplot is just ignoring the newline directive. For example:
set title 'First line$\newline$Second line'
will generate a title that reads "First lineSecond line".
Is there a trick to make this work somehow?
I am using Pyxplot 0.9.2.
Per Christoph in the comments above:
You can try putting everything in a \parbox, see "10.15 LaTeX and Pyxplot": set title '\parbox{6cm}{\centering First line\newline Second line}'. I cannot test it, therefore I just post it as comment.
\parbox does seem to do the trick. I tested this and it did allow me to perform line breaks.
Thanks!

gnuplot generate plot command with sprintf?

I want to generate a gnuplot plot command programmatically, like:
plotline = sprintf("'datafile1.dat' using %d:3 with points, '%s' using %d:3 with points",i,targfile,i)
plot plotline
Where 'plotline' in the second line is expanded to produce and execute a full command like:
plot 'datafile1.dat' using 8:3 with points, 'datafile2.dat' using 8:3 with points
I want to do this in order to echo 'plotline' in the terminal and so be certain exactly what is being shown while cycling through a set of columns / datafiles / whatever inside a loop in a gnuplot script.
Is there / what is the syntax to do this, or can you suggest another approach to report the plot command as executed (without splitting into a plot command and a separate set of commands to report the current variable states).
Thanks!
In order to construct such a plot command from some strings, you can use eval to execute the commands contained in a string:
plotline = 'x title "mytitle"'
eval('plot '.plotline)
Alternatively you can use set macros:
set macros
plotline = 'x title "mytitle"'
plot #plotline
This replaces #plotline with the content of the string variable plotline before executing the command. Using plot plotline interpretes the content of plotline as file name. Note, that as of version 4.6 macros don't work properly in loops, but eval works fine.
BTW: If you don't specify your own title, then the actual plot statement is written in the plot legend. But that can't be written to the terminal output.

gnuplot - adjust horizontal spacing of subscript letters

I am trying to adjust the horizontal spacing of letters in subscript in the postscript (enhanced) terminal. The default is to align the spacing as you would for normal letters, but for big letters such as P, the subscripted letter appears too far away. Is there a way to adjust the spacing of subscripted letters?
Edit: minimal example, as requested. My use case is with Times-Italic font, so that's what I've done here, but the look is similar with Times-Roman
set term post enh eps font "Times-Italic"
set output "test.eps"
set title "{P_{/*0.75 C}}"
plot sin(x)
set output
Edit 2: I'm pretty sure the reason is that the typesetter is aligning the left side of the second letter at the right edge of the first letter, but for letters like P where there is a large space between the bottom left corner and furthest right point, it doesn't look very nice when a letter is subscripted next to the P (or T, etc.)
If you are picky about typography, then maybe you should use LaTeX. Gnuplot has a variety of LaTeX terminal types, such as tikz, epslatex, and cairolatex. The downside is that you must then pass the generated plot through latex or pdflatex in order to render it, so plotting is not interactive. Also, you must learn some basic LaTeX.
There is a nice tutorial on using the TikZ terminal. That page gives the following example gnuplot script (xlabel added by me):
set term tikz standalone color solid size 5in,3in
set output 'sin.tex'
set xlabel '$t_{\alpha\beta}$'
set xrange [0:2*pi]
plot sin(x) with lines
exit
Note that the exit is important, otherwise sin.tex will be incomplete. To turn this into a PDF, run pdflatex sin.tex.
You still cannot control the positioning of the subscript (well, probably LaTeX will let you do this if you are expert enough), however the defaults were chosen by typographic experts who probably have a better eye than you or me.

Resources