8

给定一个用普通引号写的文档,例如

Ben said "buttons, dear sir".
I replied "Did you say 'buttons'?" to him.

有什么方法可以将这些东西变成 LaTeX 引号,具有适当的语义。IE

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 引用正则表达式”似乎返回太多。

谢谢你。

4

7 回答 7

5

一般来说,这个问题比看起来要难。

最简单的情况可以用正则表达式处理,但对于更一般的情况,您几乎肯定需要构建一个递归解析器:正则表达式只有在没有嵌套的情况下才能工作。

最大的问题将与识别"'"未配对的单个 s 相关 - 就像收缩一样("'"in"don't"不应该更改,也不应该配对)。


让我们看看我们是否可以编写一个可用的 EBNF 描述:

input:       text+
text:        uquote|squote|dquote
squote       "'" text "'"
dquote       """ text """
uquote:      [contraction|.]+
contraction: [A-Za-z]+ "'" [A-Za-z]+

这仅限于"'"单词中间有的缩写。所有关联的操作只会回显输入,除了squotedquote术语会根据需要替换引号。


我使用正则表达式和人工修复来完成一个相当简单的一次性工作,但这对于正在进行的工作来说会是劳动密集型的。

于 2008-12-06T18:56:43.250 回答
2

这是我用于我的 Latex 文档的 python 正则表达式:

'([ \w-]+)'", " `\\1'

有一个 python 脚本将正则表达式应用于乳胶文件(here)。大部分时间都在工作。排版快乐!:)

于 2010-12-26T22:52:28.783 回答
1

以下是一些 Perl 正则表达式替换,它们可能足以满足您的需求。

s/"(\w)/``$1/g;
s/'(\w)/`$1/g;
s/([\w\.?!])"/$1''/g;

该代码假定单引号或双引号后跟一个字母数字字符开始一个引号。此外,它假定字母数字字符或标点符号后面的双引号结束引号。这些假设在大多数情况下可能是正确的,但也可能有例外。

于 2008-12-06T18:56:48.383 回答
1

感谢您的意见 - 有帮助和赞赏。

我也遇到过这个,来自 CPAN 的Latex::Encode.pm

    # A single or double quote before a word character, preceded
    # by start of line, whitespace or punctuation gets converted
    # to "`" or "``" respectively.

    $text =~ s{ ( ^ | [\s\p{IsPunct}] )( ['"] ) (?= \w ) }
              { $2 eq '"' ? "$1``" : "$1`" }mgxe;

    # A double quote preceded by a word or punctuation character
    # and followed by whitespace or end of line gets converted to
    # "''".  (Final single quotes are represented by themselves so
    # we don't need to worry about those.)

    $text =~ s{ (?<= [\w\p{IsPunct}] ) " (?= \s | $ ) }
              { "''" }mgxe
于 2008-12-06T19:17:05.097 回答
0

不要对这类任务使用正则表达式!

也许您可以从SmartyPants获得一些灵感?

于 2008-12-06T19:23:17.720 回答
0

我一直在寻找这个问题的答案,并决定今天学习一点 lisp。我把这个 lisp 函数放在我的 ~/.emacs 文件中,然后运行M-x tex-set-quotes

(defun tex-set-quotes ()  
  (interactive)  
  (latex-mode)  
  (while (search-forward "\"" nil t)  
   (replace-match "" nil t)  
   (tex-insert-quote nil)))
于 2010-12-18T02:48:28.067 回答
-4

简单地说,使用 `` 开始报价和 '' 结束

于 2011-12-13T01:16:50.860 回答