how to generate correct single and double quotes in tex

I am writing a tex document using book document class. I have already typed close to 100 pages, but i did a mistake with typing single and double quotes. I have done it as follows : "test double quotes" and 'test double quotes' which generated the output of the form :

Is there a way i can generate the correct output without changing the quotes in all the places ? I saw some similar answers @ Isn't there any other way of doing double quotes in LaTeX besides  + ''? but none seemed to work.

Here's a LuaLaTeX-based solution, which doesn't require you to modify the existing "..." and '...' pairs of quotes. It consists of a Lua function called msq (short for 'make smart quotes') and two utility LaTeX macros which switch the Lua function on and off.

\documentclass{book}
\usepackage[english]{babel} % or some other suitable language choice
\usepackage[autostyle]{csquotes}

\usepackage{luacode}
\begin{luacode}
-- msq: "make smart quotes"
function msq ( s )
s = s:gsub ( '"(.-)"' , "\\enquote{%1}" )
s = s:gsub ( "'(.-)'" , "%1'" )
return s
end
\end{luacode}

"process_input_buffer", msq , "msq" )}}
\newcommand{\msqOff}{\directlua{ luatexbase.remove_from_callback(
"process_input_buffer", "msq" )}}

\begin{document}
\msqOn
"test double quotes" and 'test single quotes'

"\,'Twas brillig and the slithy toves \ldots"

\medskip
\msqOff
"test double quotes" and 'test single quotes'
\end{document}

• What happens when you're quoting " 'Twas brillig and the slithy toves ..."? – barbara beeton Jul 15 at 1:10
• @barbarabeeton - Actually, that case is handled just fine by my code. :-) I'll edit my MWE to demonstrate it. – Mico Jul 15 at 4:00
• @Mico : Thanks for your answer. I need to use xeLatex (as i am using fontspec pkg). And while compiling this, it needs luaLatex. Is there a way, i can compile this code with xeLatex as well ? – mezda Jul 15 at 4:16
• @mezda - The fontspec package works equally well under both LuaLaTeX and XeLaTeX. Using fontspec in no way precludes the use of LuaLaTeX. – Mico Jul 15 at 4:20
• Oh, I'm just being difficult. But that is a plausible scenario, so I'm suggesting that generality is a "good thing". – barbara beeton Jul 15 at 13:26

Replace at first the last quote with the editor and "Search and Replace"

"<space>  -> }


Then repcae the first quote with

" -> \enquote{


and then use always:

\documentclass{article}
\usepackage[french,ngerman,english]{babel}
\usepackage[autostyle]{csquotes}

\begin{document}

\enquote{quote}
\enquote*{quote}

\enquote{quote \enquote{quote in quote}}

\foreignquote{ngerman}{quote}
\foreignquote*{ngerman}{quote}

\foreignquote{ngerman}{quote \foreignquote{ngerman}{quote in quote}}

\foreignquote{french}{quote}
\foreignquote*{french}{quote}

\foreignquote{french}{quote \foreignquote{ngerman}{quote in quote}}

\end{document}


• The OP asked whether "there [is] a way i can generate the correct output without changing the quotes in all the places?" – Mico Jul 14 at 18:53
• Your substitution rule, "<space> -> }, doesn't catch the cases when the closing " character occurs at the very end of a line or is followed by a punctuation character. – Mico Jul 15 at 4:05
• The significant part of the question is that the user doesn't want to go and fix all their straight quotes " to the proper “ (or  in traditional TeX) and ” (or '' in traditional TeX), i.e. determine which straight quote should be changed to which. Obviously, determining which ones should be \enquote{ and which ones should be }` is the same problem; so this isn't any simpler and doesn't answer anything. Sure, using enquote helps if using multiple languages and quotation styles, but the question mentions nothing about using multiple quotation-mark styles. – ShreevatsaR Jul 15 at 4:48