I'm LaTeX-ifiying a document produced by exporting an MS Word document into plain text. I'm wondering if there's some automatic method for replacing all the double quotes, so that

"quoted text"

is replaced with

``quoted text''

I can't do a simple search and replace, as the replacement obviously has to be done in pairs: a different replace for the beginning and the end of a quote. I'm using emacs with auctex, but if this is better handled by an external package or a shell script I can do that instead.

  • something like: sed -e '/"([^"]*)*"/``\1\'\'/' – albert Jun 29 '18 at 10:48
  • Are you free to use LuaLaTeX? – Mico Jun 29 '18 at 16:06

I have a rather unorthodox solution by macros. Create a macro and then repeat it over the text. The advantage is that it is somehow interactive and you can check if there are missing pairs etc. A not so optimized version would be something like this:

F3 C-s " RET Ctrl-SPACE C-s " RET Ctrl-b Ctrl-w Ctrl-b Ctrl-d Ctrl-d `` Ctrl-y '' F4

Which reads like this:

Start the macro (F3) then search for " with C-s " RET. Start selecting (Ctrl-SPACE) until the next " (C-s " RET). Then go back one character (Ctrl-b) to exclude the closing " and kill the selection by Ctrl-w. Go back and delete both quotation marks (Ctrl-b Ctrl-d Ctrl-d) write LaTeX style quotations ``, yank the killed selection (Ctrl-y), close the LaTeX quotations and end the macro with F4.

If you are still following (!), by repetitively pressing F4 emacs would go through the whole documents and replace the quotations. I copied some Jack London text and tested the macro. It looks like below. Note the pauses are me trying to see if macro has done correct job...

enter image description here

  • I think query-replace with hitting 'y' or 'n' is also sufficiently interactive... but still this is good. – ShreevatsaR Jun 29 '18 at 15:34
  • @ShreevatsaR, defiantly! But I've kind of a fetish for macros ;) – Pouya Jun 29 '18 at 15:59


C-M-% "\([^"]*\)" RET ``\&'' RET !


(query-replace-regexp "\"\\([^\"]*\\)\"" "``\\1''" nil (if (use-region-p) (region-beginning)) (if (use-region-p) (region-end)) nil nil)
  • The interactive one is nice but it keeps the original " I think... – Pouya Jun 29 '18 at 16:02

Here's an entirely different approach, which is available to you if you can use LuaLaTeX to compile your document: Don't do anything about the " characters in the input file. Instead, let LuaLaTeX replace them "on the fly" with smart quotes.

The code shown below provides a Lua function, called convert_dumb_quotes_to_smart_quotes, which does all of the replacement work. The code also provides two LaTeX macros, called \QuoteConvOn and \QuoteConvOff respectively, which switch the operation of the Lua function on and off. Being able to switch off the Lua function is useful if your document contains verbatim-type material that contains " characters -- those characters should presumably not be converted to smart quotes, right? It's also useful if some of your bibliographic entries contain URL strings which feature, you guessed it, " characters, as those shouldn't be converted to smart quotes either.

The only input requirements are (a) that all dumb quotes are properly paired and (b) that no pairs of dumb quotes are broken up across lines. I trust that these requirements are too burdensome.

enter image description here

% !TEX TS-program = lualatex
\usepackage{luacode} % or 'luacode' environment
%% Lua-side code:
function convert_dumb_quotes_to_smart_quotes ( s )
   return ( s:gsub ( '"(..-)"' , "``%1''" ) )
%% TeX-side code:
  luatexbase.add_to_callback ( "process_input_buffer" , 
  convert_dumb_quotes_to_smart_quotes , "dumb_to_smart" )}}
  luatexbase.remove_from_callback ( "process_input_buffer" , 
  "dumb_to_smart" )}}
\AtBeginDocument{\QuoteConvOn} % switch function on by default

"Hello" World. Hello "World."

\QuoteConvOff  % switch off the Lua function
\verb+"Hello" World. Hello "World."+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.