6

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 : enter image description here

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.

mezda
  • 279

3 Answers3

6

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

user187802
  • 16,850
  • 3
    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 '19 at 18:53
  • 1
    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 '19 at 04:05
  • 1
    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 '19 at 04:48
4

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, called \msqOn and \msqOff, which activate resp. deactivate the Lua function. By "activating the Lua function", I mean assigning it to LuaTeX's process_input_buffer callback, where it acts as a preprocessor on the input stream.

enter image description here


Addendum, posted 1 March 2023: You should not use this answer to replace "dumb" with "smart" double-quotes if (a) your document uses " to denote hexadecimal numbers (e.g., "FF=256) and if you have two more more hex numbers in an input line, (b) if you use " in babel shorthand expressions, or (c) if you employ " to denote file names that include spaces; e.g., xxx"aaa bbb"ccc.tex to denote the filename xxxaaa bbbccc.tex. Please see @DavidCarlisle's recent answer for more information. Of course, if one of these issues arises just in a well-delimited part of your document, you could "protect" it from the action of the msq preprocessor by executing \msqOff at the start of the part in question (and, later on, executing \msqOn to restart the preprocessor action).

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

Mico
  • 506,678
  • What happens when you're quoting " 'Twas brillig and the slithy toves ..."? – barbara beeton Jul 15 '19 at 01:10
  • @barbarabeeton - Actually, that case is handled just fine by my code. :-) I'll edit my MWE to demonstrate it. – Mico Jul 15 '19 at 04: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 '19 at 04: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 '19 at 04:20
  • @mezda - To answer the question in the second sentence: The code shown above absolutely requires LuaLaTeX. It will not work under XeLaTeX. – Mico Jul 15 '19 at 04:21
  • Actually, I didn't mean for the outside double quotes to be included. What if this is in a British environment, and it begins with the apostrophe? – barbara beeton Jul 15 '19 at 13:07
  • @barbarabeeton - I honestly have no idea if the OP uses British, i.e., single-quote, quotation marks. :-) Let's see if he/she speaks up and voices an opinion... – Mico Jul 15 '19 at 13:13
  • 2
    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 '19 at 13:26
2

You can set " and ' as active characters and do appropriate definitions:

\catcode`\"=13 \def"{\bgroup \def"{”\egroup}“}
\catcode`\'=13 \def'{\bgroup \def'{\char`\'\egroup}`}

"test double quotes" and 'test double quotes'

\bye

wipet
  • 74,238
  • Try this with French or Italian, where the apostrophe is part of grammar and orthography. – egreg Mar 01 '23 at 08:59