Forgot password
 Register account
original poster: abababa

能不能单独开一个版块,专门存各种定理命题之类的

[Copy link]

414

Threads

1641

Posts

15

Reputation

Show all posts

original poster abababa posted 2025-6-5 11:35
hbghlyj 发表于 2025-6-5 11:03
Pandoc默认将视为()[]的转义,若要Pandoc将视为LaTeX数学模式分界符,需启用tex_math_single_backslash选 ...
我不明白那些东西。
现在什么都不改,正确的方式应该是(第一套方案):
1.写好含有latex代码的文本。
2.把以上文本复制到pandoc里转换。
3.复制转换后的文本粘贴到本论坛的wiki。
4.在本论坛保存。

那现在把第2步和第3步合并,换成本论坛的wiki里的一个函数(第二套方案),不就行了?
1.写好含有latex代码的文本。
2'和3'.把以上文本在函数里转换并输出为待保存的文本。
4.在本论坛保存。

或者直接在点保存按钮时,就先做2'和3',然后保存。

就是这个函数要怎么写我不懂,它实现的不就是pandoc的那个功能吗?这和第一套方案有什么区别?难道现在去pandoc里转换完之后还不行,还要改什么其它东西,然后粘贴保存才行?

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 11:41
abababa wrote at 2025-6-5 04:35
还要改什么其它东西
只需将LaTeX数学模式分界符\( \) \[ \]分别改为$ $ $$ $$

414

Threads

1641

Posts

15

Reputation

Show all posts

original poster abababa posted 2025-6-5 11:57
hbghlyj 发表于 2025-6-5 11:39
在pandoc在线测试,将从Markdown转换为LaTeX,结果为

这是因为Pandoc默认将视为()[]的转义,若要Pandoc将 ...
那你这个不是转反了吗?我们需要的是从latex转成markdown啊。因为我们已经打好的文本不就是latex的吗?而论坛wiki需要的是markdown,那不是应该从latex转成markdown才行?

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:06
abababa wrote at 2025-6-5 04:57
那你这个不是转反了吗?我们需要的是从latex转成markdown啊 ...
这个例子只是为了说明Pandoc默认将\(\)\[\]视为()[]的转义,若要Pandoc将\(\)\[\]视为LaTeX数学模式分界符,需启用tex_math_single_backslash选项

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:13
在Markdown中,字符[ ]需要转义,所以\[ \]的输入为\\[ \\]

若用 $$ 代替\[ \]就不需要转义了

您可以使用 bash 一行命令
  1. find . -type f -name "*.tex" -print0 | while IFS= read -r -d $'\0' file; do sed -e 's/\\(/$/g' -e 's/\\)/$/g' -e 's/\\\[/$$/g' -e 's/\\]/$$/g' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file"; echo "Processed $file"; done; echo "All .tex files processed."
Copy the Code
对当前文件夹中的所有 .tex 文件执行替换

414

Threads

1641

Posts

15

Reputation

Show all posts

original poster abababa posted 2025-6-5 12:13
hbghlyj 发表于 2025-6-5 12:12
在Markdown中,字符需要转义,所以的输入为

若用 $$ 代替就不需要转义
我不会用这个bash,只有在线的那个pandoc能用一下,还总忘了用。你不能什么都以你为标准吧,要想想我们这些不会用的人怎么方便。

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:16
abababa wrote at 2025-6-5 05:13
我不会用这个bash
下面是一个与提供的 Bash 脚本等效的 Python 脚本。
  1. import os
  2. import re
  3. def process_tex_files():
  4.     """
  5.     Finds all .tex files in the current directory and its subdirectories,
  6.     and replaces LaTeX inline and display math delimiters with standard
  7.     dollar sign delimiters.
  8.     """
  9.     script_dir = os.getcwd()
  10.    
  11.     # Regex to find \(, \), \[, \]
  12.     # We use re.escape to handle special characters in the search patterns
  13.     # and then craft the replacement string.
  14.     # The order of replacements matters if there are overlapping patterns,
  15.     # but here \( and \) are distinct from \[ and \].
  16.     replacements = [
  17.         (re.compile(r'\\\('), '$'),  # Replace \( with $
  18.         (re.compile(r'\\\)'), '$'),  # Replace \) with $
  19.         (re.compile(r'\\\[', ), '$$'), # Replace \[ with $$
  20.         (re.compile(r'\\\]'), '$$')   # Replace \] with $$
  21.     ]
  22.     processed_count = 0
  23.    
  24.     for root, _, files in os.walk(script_dir):
  25.         for file_name in files:
  26.             if file_name.endswith(".tex"):
  27.                 file_path = os.path.join(root, file_name)
  28.                
  29.                 try:
  30.                     with open(file_path, 'r', encoding='utf-8') as f:
  31.                         content = f.read()
  32.                     new_content = content
  33.                     for pattern, replacement in replacements:
  34.                         new_content = pattern.sub(replacement, new_content)
  35.                     if new_content != content:  # Only write if changes were made
  36.                         temp_file_path = file_path + ".tmp"
  37.                         with open(temp_file_path, 'w', encoding='utf-8') as f_tmp:
  38.                             f_tmp.write(new_content)
  39.                         
  40.                         os.replace(temp_file_path, file_path) # Atomically replace the original file
  41.                         print(f"Processed {file_path}")
  42.                         processed_count += 1
  43.                     # else:
  44.                         # print(f"No changes needed for {file_path}")
  45.                 except Exception as e:
  46.                     print(f"Error processing {file_path}: {e}")
  47.                
  48.     print(f"All {processed_count} .tex files processed.")
  49. if __name__ == "__main__":
  50.     process_tex_files()
Copy the Code
此脚本会查找当前目录及其子目录中的所有 .tex 文件,然后分别将 \\(、\\)、\\[ 和 \\] 替换为 \$ 和 \$\$。它通过写入临时文件并重命名来安全地处理文件。

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:18
abababa wrote at 2025-6-5 05:13
只有在线的那个pandoc能用一下,还总忘了用 ...
这样处理后就不需要通过pandoc了

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:17
abababa wrote at 2025-6-5 05:13
你不能什么都以你为标准吧
字符[ ]需要转义是Markdown的标准,不是我制定的标准啊

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:21
abababa wrote at 2025-6-5 05:13
我不会用这个bash
一个等效的 Windows 命令提示符 (CMD) 脚本
  1. @echo off
  2. setlocal enableDelayedExpansion
  3. echo Searching for .tex files and processing them...
  4. rem Loop through all .tex files found using 'dir /s /b'
  5. for /f "tokens=*" %%f in ('dir /s /b "*.tex"') do (
  6.     set "filepath=%%f"
  7.     set "tempfile=%%f.tmp"
  8.     echo Processing !filepath!
  9.     rem Create a temporary file with the substitutions
  10.     rem Note: CMD's 'findstr' and 'more' are not as powerful as 'sed'.
  11.     rem We'll use a combination of 'findstr' and redirection for basic replacement.
  12.     rem For more complex sed-like operations, PowerShell or a dedicated 'sed' port is recommended.
  13.     rem This example handles the specific replacements requested.
  14.     rem Read the file line by line and perform replacements
  15.     (for /f "usebackq delims=" %%l in ("!filepath!") do (
  16.         set "line=%%l"
  17.         rem Replace \( with $
  18.         set "line=!line:\(=$!"
  19.         rem Replace \) with $
  20.         set "line=!line:\)=$!"
  21.         rem Replace \[ with $$
  22.         set "line=!line:\\[=$$!"
  23.         rem Replace \] with $$
  24.         set "line=!line:\\]=$$!"
  25.         echo !line!
  26.     )) > "!tempfile!"
  27.     rem Move the temporary file back to the original
  28.     if exist "!tempfile!" (
  29.         move /y "!tempfile!" "!filepath!" > nul
  30.         echo Processed !filepath!
  31.     ) else (
  32.         echo Error: Could not create temporary file for !filepath!
  33.     )
  34. )
  35. echo All .tex files processed.
  36. endlocal
  37. pause
Copy the Code

414

Threads

1641

Posts

15

Reputation

Show all posts

original poster abababa posted 2025-6-5 12:39
hbghlyj 发表于 2025-6-5 12:16
下面是一个与提供的 Bash 脚本等效的 Python 脚本。

此脚本会查找当前目录及其子目录中的所有 .tex 文件 ...
那把这一大套东西做成一个函数,不要我来运行它,让论坛的wiki来运行它,这不是一样的吗?困难在哪里?为什么必须让我们这些用户来运行才行?

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:42
abababa wrote at 2025-6-5 05:39
那把这一大套东西做成一个函数,不要我来运行它,让论坛的wiki来运行它,这不是一样的吗?困难在哪里?为 ...
这样可以兼容 LaTeX 与 Markdown

如果 \[ 自动替换为 $$,就无法使用 Markdown 的 [ 转义了,见
8.21.14 Extension: tex_math_single_backslash
Causes anything between \( and \) to be interpreted as inline TeX math, and anything between \[ and \] to be interpreted as display TeX math. Note: a drawback of this extension is that it precludes escaping ( and [.

414

Threads

1641

Posts

15

Reputation

Show all posts

original poster abababa posted 2025-6-5 12:42
hbghlyj 发表于 2025-6-5 12:17
字符需要转义是Markdown的标准,不是我制定的标准啊
但你选的wiki是用markdown啊,为什么不直接选支持latex的呢?用户的水平本来就不一样,本论坛还有很多连latex都不常用的用户呢,他们不是更难使用这些东西?

Comment

现在大多数AI工具默认输出markdown格式,如ChatGPT、GitHub Copilot等。此外,几乎所有主流论坛软件都支持markdown,包括Reddit、GitHub、Stack Overflow等。  posted 2025-6-12 08:50
虽然LaTeX更强大,但对于日常使用来说,markdown可以满足大部分文档需求。  posted 2025-6-12 08:52
而且现在许多markdown编辑器也支持基本的LaTeX语法来插入公式。  posted 2025-6-12 08:52

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:44
abababa wrote at 2025-6-5 05:42
但你选的wiki是用markdown啊,为什么不直接选支持latex的呢?用户的水平本来就不一样,本论坛还有很多连la ...
考虑到用户分不清语法,所以目前的语法是同时兼容 HTML 与 Markdown 与 LaTeX 的,只需要将 LaTeX 的 \[ 替换为 $$ 这一处修改,来保持与 Markdown 兼容。其它的都没有发现问题。

414

Threads

1641

Posts

15

Reputation

Show all posts

original poster abababa posted 2025-6-5 12:44
hbghlyj 发表于 2025-6-5 12:42
这样可以兼容 LaTeX 与 Markdown

如果 \[ 自动替换为 $$,就无法使用 Markdown 的 [ 转义了,见 ...
举个例子吧,就像下面这样:
  1. 测试一下
  2. [[引用]]
  3. \[a^2\]
  4. $b^2$
  5. \(c^2\)
Copy the Code

我要是用转换,是不是“引用”的那个方括号不会被转换,只有a^2的那个方括号会被转换?那在保存之前替换一下不就行了吗?只替换数学公式外面的方括号,别的方括号不替换,这样不就行了吗?

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:46
abababa wrote at 2025-6-5 05:44
只替换数学公式外面的方括号,别的方括号不替换,这样不就行了吗?
但是在 Markdown 语法中,\[ 表示 [ 的转义。
如果自动将 \[ 替换为 $$ 就失去了对 Markdown 的兼容

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:47
字符[]在Markdown的链接语法和脚注语法中用到,所以在写[]时需要转义
  1. Here is a simple footnote[^1]. With some additional text after it.
  2. [^1]: My reference.
Copy the Code

414

Threads

1641

Posts

15

Reputation

Show all posts

original poster abababa posted 2025-6-5 12:48
hbghlyj 发表于 2025-6-5 12:46
但是在 Markdown 语法中,\[ 表示 [ 的转义。
如果自动将 \[ 替换为 $$ 就失去了对 Markdown 的兼容 ...
如果是latex的用户,根本就不会出现\[这个符号吧?出现\[这种符号不就是在数学公式里?要不然编译出错啊。

3211

Threads

7832

Posts

52

Reputation

Show all posts

hbghlyj posted 2025-6-5 12:52
abababa wrote at 2025-6-5 05:48
如果是latex的用户,根本就不会出现\[这个符号吧?出现\[这种符号不就是在数学公式里?要不然编译出错啊。 ...
是的,所以只要将 LaTeX 出现的\[ \]替换为 $$,用 LaTeX 与 Markdown 编译就都会不出错了。

414

Threads

1641

Posts

15

Reputation

Show all posts

original poster abababa posted 2025-6-5 12:52
hbghlyj 发表于 2025-6-5 12:50
是的,所以只要批量将 LaTeX 公式的替换为 $$ 就可以兼容 LaTeX 公式与 Markdown 了。 ...
所以为什么不能让论坛wiki直接替换?你说的引用转义,我都没有看到过。我编辑“拓扑基”那个,前面就是
  1. 设 $(X,\mathscr{T})$ 是[[拓扑空间]]
Copy the Code
用的就是方括号,不带那个反斜线,那替换的时候替换的是带反斜线的方括号,和这个引用的有什么关系?

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:47 GMT+8

Powered by Discuz!

Processed in 0.013986 seconds, 22 queries