gnuplot xtics with underscores in enhanced mode

64 Views Asked by At

I need to print named xtics in extended mode. The problem is that the names stored in "selected_indices" contain underscores. To print them as underscores in extended mode, the underscores must be preceded by three backslashes. How can I insert these three backslashes automatically when I add the xtics to a plot?

original:

n_indices = words(selected_indices)
selected_indices = "A_a B_b"
print selected_indices

wanted:

selected_indices = "A\\\_a B\\\_b"
print selected_indices

set xtics ( '' 1 )
set for [i=1:n_indices] xtics add ( sprintf("{/:Bold %s}", word( selected_indices, i ) ) i )
1

There are 1 best solutions below

3
theozh On BEST ANSWER

The first thing which comes to my mind is simply to replace the _ by \_. Mind the difference with single and double quotes, either "\\\_"or '\_'.

In the following script, a function is defined to replace the first underscore (_) in a string by backslash + underscore (\_). With this, you can "un"enhance the underscore for subscript, but still use ^ for subscript. Maybe there are better solutions.

Script:

### unenhance underscore in enhanced textmode
reset session

$Data <<EOD
A_a^1   1
B_b^2   2
C_c^3   3
EOD

set offset 1,1,1,1
set key noautotitle
set style fill solid 0.4

ueus(s) = s[1:c=strstrt(s,'_')-1].'\_'.s[c+2:]   # unenhance underscore

plot $Data u 0:2:xtic(ueus(strcol(1))) w boxes
### end of script

Result:

enter image description here

Addition:

In case you have multiple underscores in your string, you can replace them all by the following function:

# replace all `_` with `\_`
rpus(s) = (c='', sum[j=1:strlen(s)] (c=c.(s[j:j] eq '_' ? '\_' : s[j:j]), 0), c)
print rpus("A_b_cde_fgh_12^3")

Result:

A\_b\_cde\_fgh\_12^3