Replacing “#”, “$”, “%”, “&”, and “_” with “\#”, “\$”, “\%”, “\&”, and “\_”

前端 未结 4 1861
臣服心动
臣服心动 2020-12-31 10:02

I have a plain text document, which I want to compile inside LaTeX. However, sometimes it has the characters, \"#\", \"$\", \"%\", \"&\", and \"_\". To compile properly

4条回答
  •  甜味超标
    2020-12-31 10:38

    You can do the replacement with a single call to sed:

    sed -i -E 's/([#$%&_\])/\\&/g' file.txt
    

    The & in the replacement text fills in for whichever single character is enclosed in parentheses. Note that since \ is the LaTeX escape character, you'll have to escape it as well in the original file.

提交回复
热议问题