![](/img/trans.png)
[英]How can I sanitize the standard input in python to fixes the sonarqube issue
[英]How do I sanitize LaTeX input?
我想接受用戶輸入(有時這將是大段落)並生成LaTeX文檔。 我正在考慮一些簡單的正則表達式來替換\\
with \\textbackslash
所有實例以及{
或}
所有實例和\\}
或\\{
。
我懷疑這是否足夠。 我還需要做什么? 注意:如果有一個特殊的庫,我正在使用python。
為了澄清,我不希望任何解析被視為LaTeX語法: $a$
應替換為\\$a\\$
。
如果您的輸入是純文本並且您處於正常的catcode狀態,則必須執行以下替換:
\\
→ \\textbackslash{}
(注意空組!) {
→ \\{
}
→ \\}
$
→ \\$
&
→ \\&
#
→ \\#
^
→ \\textasciicircum{}
(需要textcomp
包) _
→ \\_
~
→ \\textasciitilde{}
%
→ \\%
此外,以下替換至少在使用OT1編碼時是有用的(在任何情況下都是無害的):
<
→ \\textless{}
>
→ \\textgreater{}
|
→ \\textbar{}
這三個禁用卷曲引號:
"
→ \\textquotedbl{}
'
→ \\textquotesingle{}
`
→ \\textasciigrave{}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.