Given a document written with normal quotes, e.g.

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

What ways can one turn these sort of things into LaTeX quotes, with the appropriate semantics. i.e.

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

So that LaTeX produces:

Ben said “buttons, dear sir”.
I replied “Did you say ‘buttons’?”

My first thought is to turn to a regex. However, I'm not getting any hits from Google or the regex libraries for "LaTeX quotes regular expression", and of course "TeX quotes regular expression" seems to return too many.

Thank you.

+3  A: 

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:

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

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 the squote and dquote 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.

Besides, one can add that in editors like emacs, when you type a `"` it uses `Tex-insert-quote` to remember if your quotes are opened (no nesting), and you have to manually open single quotes.
Piotr Lesnicki
+1  A: 

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.

John D. Cook
This is similar to what I used in the past. Needs human intervention from time to time, but takes up most of the work...
+1  A: 

Thanks for the input - helpful and appreciated.

I've also come across this, from CPAN's

    # 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
Brian M. Hunt

Do not use regular expressions for this kind of task!

Maybe you can get some inspiration from SmartyPants?

... looking for a Python version, now. :)
Brian M. Hunt
+2  A: 

I want to take the opportunity to point to XƎTEX which is shipped with the (highly recommendend!) TeX Live distribution.

Amongst other things, XƎTEX directly supports Unicode. In your case this means that you don’t have to deal with these (sometimes tedious) replacement characters any more: instead of using ''´´ you can directly use “” in your LATEX code.

IMHO, that’s a big and important step. TEX is a great typesetting system but it’s lacking support of modern features such as Unicode has made many tasks arduous.

Konrad Rudolph
Nice, but if the question is "How do I programatically tell open- from close-quotes in an existing document?", than this isn't much help.
Yes, this wasn't meant as a direct answer, rather as a useful aside because many people don'd know XeTeX yet and feel restricted by TeX' missing Unicode support.
Konrad Rudolph
Fair enough. I hadn't heard of it, and I do LaTeX all the time.