# How to quickly fix all the wrong quotation marks?

My entire document is filled with quotation marks like ”this”. It was my mistake adopting

"this"


this''


Since the document is very long, is there a way to fix that with a macro or a regex instead of doing it manually? I use TexStudio.

• Is it guaranteed that the " double-quote marks always occur in pairs, without intervening line breaks? Moreover, are you open to using a LuaLaTeX-based solution?
– Mico
Apr 22, 2020 at 14:58
• If you have used spaces before your quote marks, use Ctrl+R in Texstudio and search « ''» (with a space before) and replace by « » (with a space before). Then, click the double down arrow to catch all results.
– user193767
Apr 22, 2020 at 15:08
• @JairoADelRio - The condition "if you have used spaces before your quote marks" will exclude all instances of the opening " character occurring at the start of a line. If the OP's document contains a lot of dialogue material rendered with quotation marks, the case I just outlined may be the rule rather than the exception.
– Mico
Apr 22, 2020 at 17:57

Assuming you can use LuaLaTeX, the following solution may be of interest to you. It sets up (a) a Lua function called fixquotes that performs a non-greedy pattern match and an on-the-fly substitution operation and (b) two LaTeX utility macros, called \FixquotesOn and \FixquotesOff, that serve to activate and deactivate the Lua function. The only assumptions that must be satisfied are that the " characters always occur in pairs and that the opening and closing instances of the " characters are not separated by line breaks in the input stream. Hopefully, these assumptions are satisfied trivially by your code.

\documentclass{article}
\usepackage{luacode} % for "\luaexec" macro
%% Define a Lua function called "fixquotes":
\luaexec{function fixquotes ( s )
return ( s:gsub ( '"(..-)"' , "\%1''" ) )
end}
%% Two utility macros to activate/deactivate the Lua function:
"process_input_buffer" , fixquotes , "fixquotes" )}}
\newcommand\FixquotesOff{\directlua{luatexbase.remove_from_callback (
"process_input_buffer" , "fixquotes" )}}

\begin{document}
"this" "there" "that" "Who, me?" "Yes, you."

\FixquotesOn
"this" "there" "that" "Who, me?" "Yes, you."

\FixquotesOff
"this" "there" "that" "Who, me?" "Yes, you."
\end{document}

• What if there is a line break in the quotation? What if it contains inner quotes? The most natural starting point is to interpret " as “ if there is a space or new line in front and ” otherwise. However, one also has to consider cases like "neo"-liberalism, neo-"liberalism", and She said, "He is ultra-aggressive, ultra-". "-violent", he interrupted her.. Sep 5 at 14:22
• @Gaussler - I provided an answer to the question that was posed above. If you want to ask a series of new questions, please post new queries.
– Mico
Sep 5 at 19:15
• I know, I was simply making some related considerations. Sep 5 at 21:04
• @Gaussler - Line breaks will break things [pun intended]; that's why I asked "Is it guaranteed that the " double-quote marks always occur in pairs, without intervening line breaks?" initially. (In a reply, since deleted, the OP assured me there wouldn't be intervening line breaks.) The OP asked nothing about inner quotes. All other examples you provided are handled just fine by the Lua code shown above, so I'm not sure what to remark.
– Mico
Sep 6 at 1:32