如何清理 LaTeX 输入?
我想接受用户输入(有时这将是大段落)并生成 LaTeX 文档。我正在考虑使用几个简单的正则表达式,将 \
的所有实例替换为 \textbackslash
以及 {
或 } 的所有实例
与 \}
或 \{
。
我怀疑这是否足够。我还需要做什么?注意:如果有为此制作的特殊库,我将使用 python。
澄清一下,我不希望任何内容被解析为 LaTeX 语法: $a$
应替换为 \$a\$
。
I'd like to take user input (sometimes this will be large paragraphs) and generate a LaTeX document. I'm considering a couple of simple regular expressions that replaces all instances of \
with \textbackslash
and all instances of {
or }
with \}
or \{
.
I doubt that this is sufficient. What else do I need to do? Note: In case there is a special library made for this, I'm using python.
To clarify, I do not wish anything to be parsed treated as LaTeX syntax: $a$
should be replaced with \$a\$
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的输入是纯文本并且处于正常的 catcode 体系中,则必须执行以下替换:
\
→\textbackslash{}
(注意空组!)&
→\&
#
→\#
^
→\textasciicircum{}
(需要textcomp
包)_
→\_
~
→\textasciitilde{}
%
→\%
此外,以下替换至少在使用 OT1 编码时很有用(并且在任何情况下都无害) case):
<
→\textless{}
>
\textgreater{}
|< /code> →
\textbar{}
这三个禁用大引号:
"
→\textquotedbl{}
'
→\textquotesingle{}
`
→\textasciigrave{}
If your input is plain text and you are in a normal catcode regime, you must do the following substitutions:
\
→\textbackslash{}
(note the empty group!){
→\{
}
→\}
$
→\$
&
→\&
#
→\#
^
→\textasciicircum{}
(requires thetextcomp
package)_
→\_
~
→\textasciitilde{}
%
→\%
In addition, the following substitutions are useful at least when using the OT1 encoding (and harmless in any case):
<
→\textless{}
>
→\textgreater{}
|
→\textbar{}
And these three disable the curly quotes:
"
→\textquotedbl{}
'
→\textquotesingle{}
`
→\textasciigrave{}