如何将常规引号(即 ', ")转换为 LaTeX/TeX 引号(即 `', ``'')
给定一份用普通引号编写的文档,例如,
Ben said "buttons, dear sir".
I replied "Did you say 'buttons'?" to him.
如何通过适当的语义将此类内容转换为 LaTeX 引号。 即
Ben said ``buttons, dear sir''.
I replied ``Did you say `buttons'?'' to him.
LaTeX 产生:
Ben said “buttons, dear sir”.
I replied “Did you say ‘buttons’?”
我的第一个想法是转向正则表达式。 但是,我没有从 Google 或正则表达式库中得到任何“LaTeX 引用正则表达式”的点击,当然“TeX 引用正则表达式”似乎返回了太多。
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一般来说,这个问题比看起来更难。
最简单的情况可以用正则表达式处理,但对于更一般的情况,您几乎肯定需要构建一个递归解析器:正则表达式仅在没有嵌套的情况下才起作用。
最大的问题将与识别未配对的单个
"'"
相关——就像收缩一样("don 中的
不应更改,和 不应配对)。"'"
't"让我们看看是否可以编写一个可用的 EBNF 描述:
它仅限于单词中间有
"'"
的缩写。 所有关联的操作都只会回显输入,但squote
和dquote
术语会根据需要替换引号。我使用正则表达式,然后进行人工修复,以进行相当简单的一次性操作,但这对于正在进行的工作来说将是劳动密集型的。
In general, this problem is harder than it looks.
The simplest cases can be treated with regular expressions, but for more general situations you will almost certainly need to build a recursive parser: regular expression will only work if there is no nesting.
The big problem is going to be associated with identifying single
"'"
s that are not paired---as is contractions (the"'"
in"don't"
should not be changed, and should not be paired).Lets see if we can write a usable EBNF description:
which is limited to contractions that have the
"'"
in the middle of the word. All the associated action will just echo the input, except that thesquote
anddquote
terms replace the quotes as appropriate.I used regular expressions followed by human fix-ups for a fairly simple one-off, but that would be labor intensive for on-going work.
这是我用于 Latex 文档的 python 正则表达式:
有一个 python 脚本将正则表达式应用于 Latex 文件(此处)。 大部分时间都有效。 排版愉快! :)
Here is the python regex that I use for my Latex documents:
There is a python script that applies the regex on a latex file (here). Works most of the time. Happy typesetting! :)
这里有一些 Perl 正则表达式替换,可能足以满足您的需要。
该代码假定单引号或双引号后跟字母数字字符开始引号。 此外,它还假定字母数字字符或标点符号后面的双引号结束引号。 这些假设在大多数情况下可能都是正确的,但也可能有例外。
Here are some Perl regular expression substitutions that might be good enough for what you want to do.
The code assumes that a single or double quote followed by an alphanumeric character begins a quote. Also, it assumes that a double quote following an alphanumeric character or punctuation mark ends a quote. These assumptions are probably true most of the time but there may be exceptions.
感谢您的投入 - 很有帮助并且值得赞赏。
我也遇到过这个,来自 CPAN 的 Latex::Encode.pm:
Thanks for the input - helpful and appreciated.
I've also come across this, from CPAN's Latex::Encode.pm:
不要使用正则表达式来完成此类任务!
也许您可以从SmartyPants中获得一些灵感?
Do not use regular expressions for this kind of task!
Maybe you can get some inspiration from SmartyPants?
我一直在寻找这个问题的答案,并决定今天学习一点 lisp。 我将此 lisp 函数放入 ~/.emacs 文件中,然后使用
Mx tex-set-quotes
运行:I was looking for an answer to this problem and decided to learn a little lisp today. I put this lisp function in my ~/.emacs file and then run with
M-x tex-set-quotes
:简单地说,使用 `` 来开始报价,使用 '' 来结束报价
Simply, use `` for opening quotations and '' for closing