how to generate correct single and double quotes in tex

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}

enter image description here


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.

enter image description here

\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}

\newcommand{\msqOn}{\directlua{  luatexbase.add_to_callback( 
   "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}