10

I am writing a book and the publisher wants me to use guillemets instead of ``'' quotes. I have read quite a few questions with good answers on this stack exchange, but I haven't found an exact answer to my question.

I am wondering whether there is a way to make LaTeX automatically make « when I write ``, and » when I write ''. The alternative, which I am not very keen on, is to go through an entire book consisting of more than 15 different .tex documents and do it manually.

Any help is very appreciated.

12
  • 2
    changing the source is simpler, doesn't really matter how many files there are for i in *.tex; do sed -i -s 's/``/«/g' $i; done probably does what is needed if it survives comment markdown Commented May 1 at 11:37
  • 1
    Can you please post a minimalistic example ready to compile? E.g. I'd like to see a simple and a tough case within the text, the documents language etc. Thank you // Same as Mico: can it be that simple as telling babel to use french?
    – MS-SPO
    Commented May 1 at 11:52
  • 1
    Just out of curiosity: Which language is your book written in?
    – Mico
    Commented May 1 at 11:53
  • 2
    Thanks for replies! Yes, the reason I don't want to touch the source is that I'm using Overleaf and can't simply do a sed on the entire project. It is written in Norwegian using \usepackage[norsk]{babel}. I could get you a minimal viable example, but there's like 100 lines of setup in my main file, so it might be hard. And thanks for being so welcoming and friendly! I'm so glad to see a nice stackexchange. I'm on StackOverflow a lot, and this is different :) Commented May 1 at 12:08
  • 2
    overleaf will let you apply a regex to the whole project so it comes to the same thing Commented May 1 at 12:59

2 Answers 2

11

It's good news that you are on the Overleaf platform, as it lets you use Lua(La)TeX to compile your document. :-)

In case you're not (yet) familiar with LuaLaTeX: Nowadays, it's largely a superset of pdfLaTeX. This means, in particular, that if your document compiles correctly under pdfLaTeX, you won't have to make too many changes to get it to compile properly under LuaLaTeX as well. The main change you'll have to make is to stop loading the inputenc and fontenc packages. If your document uses any special text or math fonts, it may also be necessary to load the fontspec package and issue suitable \setmainfont, \setsansfont etc instructions. Do please indicate if you use any non-default fonts.

I suggest the following solution, which proceeds in two steps. First, be sure to load the csquotes package with the option norwegian=guillemets; this package provides a macro called \enquote, which surrounds its argument with a matched pair of guillemets. Second, you'll need to define and activate a Lua function that acts as a pre-processor on the input stream, converting all instances of `` to \enquote{ and all instances of '' to } "on the fly". With the Lua function acting as a pre-processor, before LaTeX starts its usual work, LaTeX actually never "sees" any instances of `` and ''; instead, LaTeX only gets to see and operate on instances of \enquote directives.

The following test file provides such a Lua function, called use_enquote, along with two utility macros that activate and, if need be, deactivate the Lua function.

enter image description here

%% this document must be compiled with LuaLaTeX.
\documentclass{book} % or some other suitable document class
\usepackage{fontspec} % don't load the 'inputenc' and 'fontenc' packages

\usepackage[norsk]{babel} % or '\usepackage[norwegian]{babel}'
\usepackage[norwegian=guillemets]{csquotes} % customize appearance of quoted material

%% A Lua function that performs all of the work:
\directlua{
  function use_enquote ( s )
    s = s:gsub ( "``" , "\\enquote{" )
    return ( s:gsub ( "''" , "}" ) )
  end
}

% Two LaTeX utility macros:
\newcommand\EnquoteOn{\directlua{luatexbase.add_to_callback(
   "process_input_buffer", use_enquote, "use_enquote" )}}
\newcommand\EnquoteOff{\directlua{luatexbase.remove_from_callback(
   "process_input_buffer", "use_enquote" )}}
  
\begin{document}

\EnquoteOn % activate the Lua function 
``Hei Verden''

\EnquoteOff % deactivate the Lua function
``Hei Verden''

\end{document}
1
  • 2
    Very nice code +1
    – MS-SPO
    Commented May 1 at 14:23
7

As you are using babel, you can stick to pdflatex with the help of shorthands. They can be sometimes dangerous, but they are usually fine:

\documentclass{article}

\usepackage[T1]{fontenc}

\usepackage[norsk]{babel}

\useshorthands{`}
\useshorthands{'}
\defineshorthand{``}{«}
\defineshorthand{''}{»}

\begin{document}

Text ``Quoted'' Text

\end{document}

With lualatex you can resort to a couple of transforms:

\documentclass{article}

\usepackage[norsk]{babel}

\babelprehyphenation{norsk}{ `` }{ string = «, remove }
\babelprehyphenation{norsk}{ '' }{ string = », remove }

\begin{document}

Text ``Quoted'' Text

\end{document}

In both cases the result is: enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .