7

How can I conditionally designate an active quote in csquotes.cfg so that no error is produced if the same character is made into an active quote in the .cfg and the document's preamble?

I have the following effective code in csquotes.cfg:

\MakeAutoQuote{‘}{’}
\MakeAutoQuote*{“}{”}

\endinput

Is it possible to activate this code at the end of the preamble and only if the \MakeAutoQuote and \MakeAutoQuote* have not already been set for these characters?

That is, I would like to be able to compile the following document notwithstanding csquotes.cfg:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}
\MakeAutoQuote{‘}{’}
\MakeAutoQuote*{“}{”}
\begin{document}
‘abc’
\end{document}

as well as

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}
\begin{document}
‘abc’
\end{document}
cfr
  • 198,882
  • By conditionally, do you mean you would be willing to use \ifs? That is, wrap the \Make...s in the .cfg in a conditional, and then add a \newif statement before the document class? Or are you looking for a cleverer version of \MakeAutoQuote? – jon Jul 19 '16 at 03:16
  • @jon I'm hoping for a test I can do in the .cfg which does not require altering the document preamble e.g. an existing \if of some kind, so I don't need to add it in the document preamble. – cfr Jul 19 '16 at 10:14
  • I suspected as much. So, something like a \ProvideAutoQuote command.... – jon Jul 20 '16 at 03:27
  • @jon Exactly. I tried looking at the source for csquotes and, after that, figured I'd ask here :(. – cfr Jul 20 '16 at 06:57
  • @cfr I posted a present for your great achievement. Congratulations for your 100K! – egreg Jul 25 '16 at 20:20
  • @egreg It isn't as if I did anything, so I'm not sure it counts as an achievement of any kind at all. Really, it just happened to me. If I did anything, it was certainly an accident! – cfr Jul 26 '16 at 00:30

2 Answers2

5

Upon seeing \MakeAutoQuote or \MakeAutoQuote*, csquotes defines \csq@string@<number> for both arguments in a suitable way. The <number> represents the decimal ASCII codes of the UTF-8 representation of the character, separated by periods if multibyte.

However, the package already provides the infrastructure for the conversion.

\begin{filecontents}{csquotes.cfg}
\MakeAutoQuote{‘}{’}
\MakeAutoQuote*{“}{”}
\end{filecontents}

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}


\makeatletter
\def\ConditionalMakeAutoQuote#1#{%
  \def\cfr@star{#1}\CMAQ@
}
\def\CMAQ@#1#2{%
  \csq@ifutfchar{#1}
    {\csq@ifvalidutf{#1}
       {\ifcsundef{csq@string@\csq@number{#1}}
          {\expandafter\MakeAutoQuote\cfr@star{#1}{#2}}
          {}
       }%
       {\csq@err@utf}}
    {\csq@ifvalidchar{#1}
       {\ifcsundef{csq@string@\number`#1}
          {\expandafter\MakeAutoQuote\cfr@star{#1}{#2}}
          {}}%
       {\csq@err@char}}}
\makeatother

\ConditionalMakeAutoQuote{‘}{’}
\ConditionalMakeAutoQuote*{“}{”}

\begin{document}

‘abc’

“abc”

‘abc “abc” abc’

\end{document}

I get the same result, namely

enter image description here

with or without the csquotes.cfg file, which is read just after csquotes.def, so before any possible definition of quoting characters in the document.

egreg
  • 1,121,712
  • Thank you! I wish I understood this. I understand it just enough to figure out how to modify it very slightly for my purposes, but I don't understand it. However, the slightly adapted version works OK in the .cfg file, which is good. (I don't want this in the document itself for various reasons related to teaching.) Thanks very much again. – cfr Jul 26 '16 at 00:29
  • 1
    @cfr I don't fully understand it either. I took the code whereby the error message is issued, in case the shorthand is already defined, and modified it to do \MakeAutoQuote if possible. – egreg Jul 26 '16 at 07:36
1

OK. Since nobody seems to be biting, I did a little more investigation and came up with the following for csquotes.cfg.

\AtEndPreamble{%
  \DeleteQuotes
  \MakeAutoQuote{‘}{’}%
  \MakeAutoQuote*{“}{”}%
}

\endinput

However, this means that I cannot override my defaults in the preamble of a particular document because it works by zapping the existing definitions - whatever they happen to be - and then adding the definitions I want as defaults.

This is compatible with both of the MWEs I posted in the question i.e. both will compile.

However, it is not very satisfactory. I would prefer to add to document-level definitions, if possible, or to do nothing, otherwise. I did try checking whether the cat codes for the characters are active or not, but I couldn't get this to work, probably because I know so little about managing cat codes.

cfr
  • 198,882