I have a lot of quotes in my document and I have decided to make them all italic. So for each quote, I would have to go through and change "Hello" to \textit{"Hello"}. Is there a quick way to do this using replace. So I can add text to the left and right of the quotation marks but ignore the text in the middle?

I am using OverLeaf by the way.

Any help would be much appreciated!

  • 1
    Maybe (in your editor) replace ‘‘ with ‘‘\itshape and replace ’’ with \upshape’’. – Steven B. Segletes Jun 27 '18 at 12:04
  • Please clarify whether you employ " (dumb quotes) or smart quotes to denote quoted material. – Mico Jun 27 '18 at 13:43
  • 1
    Hi Mico. I am using dumb quotes. – Nick Broom Jun 27 '18 at 14:58
  • @NickBroom - Please see the answer I posted a short while ago. The proposed solution can handle both smart and dumb quotes. – Mico Jun 27 '18 at 15:07
  • 1
    You could use regular expressions for this! I recommend looking into them if you find you need to do these kinds of things more often. I think most editors support them, but the precise implementation varies. – Circumscribe Jun 27 '18 at 18:46

Here's a LuaLaTeX-based solution which works with both smart and dumb quotes. The Lua function italic_quotes performs two string substition operations, one for each type of quotation method. The solution below also sets up two LaTeX macros; \iqOn switches the operation of the Lua function on, and \iqOff switches it off.

The only input syntax requirement is that there must be no line breaks between the opening and closing quotes.

enter image description here

\usepackage{luacode} % for "luacode" environment
%% Lua-side code:
function italic_quotes ( s )
   s = s:gsub ( "``..-''", "\\textit{%0}" )     -- smart quotes
   s = s:gsub ( '"(..-)"', "\\textit{``%1''}" ) -- dumb quotes
   return s
%% LaTeX-side code
\newcommand\iqOn{\directlua{luatexbase.add_to_callback (
   "process_input_buffer", italic_quotes, "italic_quotes" )}}
\newcommand\iqOff{\directlua{luatexbase.remove_from_callback (
   "process_input_buffer", "italic_quotes" )}}

"Hello" ``Hello'' Hello  ``World'' Hello

"Hello" ``Hello'' Hello  ``World'' Hello

Maybe not much good when it's all written, but for next time, this is precisely the sort of thing that LaTeX and its emphasis on logical structure is there for.

Simply define a command, say, \qte (don't use \quote it's already defined):




\qte{Who's the more foolish: the fool, or the fool who follows him?}


enter image description here

And then you can change the definition as much as you like, whenever you like:




\qte{Who's the more foolish: the fool, or the fool who follows him?}


enter image description here


This isn't really a (La)TeX question, but here it goes.

Regular expressions are pretty much made to do this kind of thing. Regular expressions are strings that describe search patterns, similar to the wildcards * and ? that most people are familiar with, but a lot more powerful. They can also be used to perform search-and-replaces with replacements strings that contain (parts of) the original text, which is what you are asking for.

I believe most TeX editors support regular expressions, but the precise implementation can vary. Most noticeably, some characters have a special meaning in regular expressions and whether they need to be escaped (i.e., preceded by a \) often depends on the "flavour" of regular expressions that is being used. Additionally, some more advanced types of regular expressions may not be supported by all implementations.

Here is a link to a tutorial about regular expressions, but there are many more out there. Beware though that not every type of regular expression works in every editor and that you may need to add or remove a few \s to make a regular expression that you found on the internet work in yours.

As for your specific problem: any search query that both starts and ends with a / is interpreted by Overleaf as a regular expressions. To find all "-delimited quotes you can search for /"[^"]*"/. Overleaf's regex engine will then try to find a sequence of characters that matches the following criteria:

  • It should start and end with a " because these are the first and last characters in the regex (the /s just tell Overleaf that this is a regular expression)
  • Between these "s there should be a sequence of characters that matches [^"]*.

    [^"] matches any character that is not a " ([abc] means "an a, b or c" and [^abc] means "any character that is not an a, b or c"). You could have instead used [^"\n] if you want to match any character that is not a quote or a newline if none of your quotes contain newlines. And if your quotes contain only letters and numbers you could use [0-9a-zA-Z] instead (- indicates a range).

  • * means that the previous subexpression can be repeated any number of times (including 0 times), so [^"]* matches any sequence of non-quote characters (including newlines). Note that * is greedy: it tries to include as many matching characters as it can find. In some implementation you can use *?, which does the same thing but includes the minimum number of characters.

Note that the characters [, ] and *have a special meaning. If you want to search for these characters using regular expressions you will need to escape them by typing \[, \] or \* respectively. I think the full list of characters that may need to be escaped is .^$*+?()[]{}\|, but it depends on the implementation (otherwise you will need to prepend a \ to invoke the special meaning of these characters).

The parts of a regular expression that are enclosed in () (or sometimes \(\)) can be used in the replacement string. Often \1 will denote the part between the first pair of parentheses, \2 the part between the second pair etc., but in Overleaf it is $1, $2 and so on. The parentheses themselves don't match anything, they only group things together.

So you could do a search-and-replace for /"([^"]*)"/ and replace it by \textit{``$1''}. This will look through your document for sequences of the form "<quote>" and replace them by \textit{``<quote>''} (I have taken the liberty of replacing your "" by ``''). You can also define a macro \qte as suggested by Au101 and then use \qte{$1} as the replacement string instead.

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.