Forgot password
 Register account
View 172|Reply 11

[regex] 在TeX中把| |替换成\abs{}

[Copy link]

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2022-9-25 22:10 |Read mode
为了在TeX中把| |替换成\abs{},但是不要替换\left| \right|
初步解决方案:
Replace
  1. (^|[^t])\|([^\|]*)\|
复制代码
with
  1. $1\abs{$2}
复制代码
其中(^|[^t])处理了两种情况:在行首或者在非t字符的后面.
但是比如|t|这种以t结尾的无法匹配到(从而把第2个|作为abs的开始,导致后面全错了)
如何解决呢?
需要3步的方法,见3#
如果你的regex engine支持Regex lookahead则可以使用4#的方法,只需1步解决.

Comment

怎样实现“若第二个|前面是\right就不匹配”  posted 2022-9-25 22:17
现在是“若第二个|前面是t就不匹配”  posted 2022-9-25 22:17

3211

Threads

7832

Posts

52

Reputation

Show all posts

original poster hbghlyj posted 2022-9-25 22:26

把\left| \right|都替换成|

还有一种方案:
把\left| \right|都替换成|
然后Replace
  1. \|([^\|]*)\|
Copy the Code
with
  1. \abs{$1}
Copy the Code
但是这样会把\left|1+|x|\right|替换成\abs{1+}x\abs{}

3211

Threads

7832

Posts

52

Reputation

Show all posts

original poster hbghlyj posted 2022-9-25 22:47
hbghlyj 发表于 2022-9-25 15:26
但是这样会把\left|1+|x|\right|替换成\abs{1+}x\abs{}
把\left|替换成\left\vert,把\right|替换成\right\vert,然后再替换| |为\abs{}即可

673

Threads

110K

Posts

218

Reputation

Show all posts

kuing posted 2022-9-25 23:17
  1. (?<!\\left *|\\right *)\|(.*?)(?<!\\left *|\\right *)\|
Copy the Code
替换为
  1. \abs{$1}
Copy the Code

673

Threads

110K

Posts

218

Reputation

Show all posts

kuing posted 2022-9-25 23:22
hbghlyj 发表于 2022-9-25 22:47
把\left|替换成\left\vert,把\right|替换成\right\vert,然后再替换| |为\abs{}即可
...
3# 4# 都需要满足的前提:就是 | 必须只用于绝对值,成对出现,还不能嵌套。

万一前面出现比如 {x|x>0} 那后面就全错了,好在,只要不用修饰符 s ,那 (.*?) 不会匹配换行符,错也只限于一行内,不至于全文错。

Comment

嗯嗯. {x|x>0}这里我一般用\mid, 就不会错了👌  posted 2022-9-25 23:54

673

Threads

110K

Posts

218

Reputation

Show all posts

kuing posted 2022-9-26 00:00
如果还会出现 \| 的话这个也得排除

Comment

还有\verb|  posted 2022-9-26 00:01

673

Threads

110K

Posts

218

Reputation

Show all posts

kuing posted 2022-9-26 00:03
回复楼上点评:我习惯用 \verb"..."

3211

Threads

7832

Posts

52

Reputation

Show all posts

original poster hbghlyj posted 2023-1-9 07:21

TeXmacs define macro

仿照 Chapter II.4 Quicker_20230108_220956.png

⟨assign|abs|⟨macro|body|⟨around|||body||⟩⟩⟩
长的竖线是Tab输入的分隔符, 短的竖线是绝对值
创建style文件

Quick Reply

Advanced Mode
B Color Image Link Quote Code Smilies
You have to log in before you can reply Login | Register account

$\LaTeX$ formula tutorial

Mobile version

2025-7-20 05:58 GMT+8

Powered by Discuz!

Processed in 0.017674 seconds, 29 queries