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.
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.
Thanks for the input - helpful and appreciated.
I've also come across this, from CPAN's Latex::Encode.pm:
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! :)
Simply, use `` for opening quotations and '' for closing
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.
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
: