You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在gnuplot增强模式下自动为xtics名称添加三重反斜杠处理下划线

GNUplot增强模式下自动处理xtics名称的下划线转义

在GNUplot的增强模式(enhanced mode)里,下划线_会被识别为下标语法的起始标记,要是想让下划线正常显示,必须在它前面加三个反斜杠\\\_。如果你的xtics名称存在selected_indices变量里,且包含下划线,不用手动逐个修改,直接通过字符串替换就能自动完成转义。

实现代码

# 原始带下划线的索引名称
selected_indices = "A_a B_b"
n_indices = words(selected_indices)

# 递归替换函数:把字符串里的单个下划线换成\\\_
replace_underscore(str) = ( \
    pos = strstrt(str, "_"), \
    pos == 0 ? str : \
    substr(str, 1, pos-1) . "\\\\_" . replace_underscore(substr(str, pos+1)) \
)

# 处理所有索引名称
processed_indices = ""
do for [i=1:n_indices] {
    processed_indices = processed_indices . " " . replace_underscore(word(selected_indices, i))
}
processed_indices = substr(processed_indices, 2)  # 移除开头多余的空格

# 设置带加粗效果的xtics
set xtics ('' 1)
set for [i=1:n_indices] xtics add ( sprintf("{/:Bold %s}", word(processed_indices, i)) i )

# 打印验证处理结果
print "原始字符串: ", selected_indices
print "处理后字符串: ", processed_indices

说明

  • 递归函数replace_underscore会遍历字符串中的每一个下划线,将其替换为\\\_(GNUplot里字符串的反斜杠需要转义,所以写四个反斜杠最终实际输出三个)
  • 循环处理selected_indices里的每个单词,拼接成处理后的processed_indices
  • 最后设置xtics时用处理后的字符串,就能在增强模式下正常显示下划线,同时保留加粗格式

内容的提问来源于stack exchange,提问作者magfan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 16:27:10