如何在混合字符串中仅制作较低的案例字符斜体

发布于 2025-02-08 16:24:04 字数 965 浏览 3 评论 0原文

这个问题是通过试图解决这个问题如何使用ggplot2在斜体上写下x标签的部分字符串?

我想知道我们如何只能在字符串中斜体字符降低情况:

string <- "wbfV/wcvB"
[1] "wbfV/wcvB"

所需的输出:

  • wbf v/ wcv b

背景: 我想将其用于图中标记。

我想这样做是这样做的,但是显然它不起作用:

library(stringr)

expression(str_detect(string, '[a-z]'~italic(str_detect(string, '[A-Z]'))))

我试图标记

plot(1, xlab=expression(str_detect(string, '[a-z]'~italic(str_detect(string, '[A-Z]')))))

“

This question raised by trying to solve this one How to write partial string of X labels in italics using ggplot2?:

I want to know how we could only italicize characters in a string that are lower case:

string <- "wbfV/wcvB"
[1] "wbfV/wcvB"

Desired output:

  • wbfV/wcvB

Background:
I would like to use it then for labelling in a plot.

I thought to do it like this way, but obviously it is not working:

library(stringr)

expression(str_detect(string, '[a-z]'~italic(str_detect(string, '[A-Z]'))))

which I tried to label

plot(1, xlab=expression(str_detect(string, '[a-z]'~italic(str_detect(string, '[A-Z]')))))

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2025-02-15 16:24:04

我并不真正熟悉直接在R中使用表达式,我只使用过lastx2exp,所以我也在这里使用它。此任务的关键是对Lookarounds进行正确的分开。然后,您可以轻松地制作其他所有底带斜体。

library(latex2exp)
library(stringr)
library(purrr)

"wbfV/wcvBa" |> 
  str_split("(?<=[a-z])(?![-a-z])|(?<![-a-z])(?=[a-z])") |>
  unlist() |> 
  imap_chr(\(x,i) ifelse(i %% 2, x, str_c("\\textit{", x, "}"))) |> 
  str_c(collapse = "") |> 
  TeX() %>%
  plot(1, xlab = .)

“”

在2022-07-30创建的 preprex软件包(v2.0.1)

the Regex由两个部分组成,每个部分都有两个lougharounds:

Split between either 
(?<=[a-z])   lowercase letter 
(?![-a-z])   followed by non-lowercase
|            OR 
(?<![-a-z])  non-lowercase
(?=[a-z])    followed by lowercase

I'm not really familiar with using expressions in R directly, I have only ever used latex2exp, so I'll be using it here as well. The key to this task is doing the right split with lookarounds. Then you can easily make every other substring italic.

library(latex2exp)
library(stringr)
library(purrr)

"wbfV/wcvBa" |> 
  str_split("(?<=[a-z])(?![-a-z])|(?<![-a-z])(?=[a-z])") |>
  unlist() |> 
  imap_chr(\(x,i) ifelse(i %% 2, x, str_c("\\textit{", x, "}"))) |> 
  str_c(collapse = "") |> 
  TeX() %>%
  plot(1, xlab = .)

Created on 2022-07-30 by the reprex package (v2.0.1)

The regex consists of two parts with two lookarounds each:

Split between either 
(?<=[a-z])   lowercase letter 
(?![-a-z])   followed by non-lowercase
|            OR 
(?<![-a-z])  non-lowercase
(?=[a-z])    followed by lowercase
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文