6

There are some other questions on this side addressing related issues, but I haven't found any which cover exactly my issue.

I have a text (converted from XML) which uses the apostrophe ' with three different meanings: Begin quote, end quote, and for abbreviations and genitive (can't, cats', etc.). Is there a way to keep the apostrophe ' the way it is in all cases but the begin quotes, where it must be replaced by `? For your information, I'm actually using XeLaTeX in my original document, but I'm not sure if it's going to matter.

\documentclass{memoir}

\begin{document}

He said, 'the cats' hats can't fit me'.

\end{document}

I guess the way of determining which one to use is:

  • If the apostrophe is preceded by a text character, keep it
  • If it is preceded by anything else (including space and nothing at all), change it to `

Note: Abbreviation apostrophes in the beginning of words (like in 'tis) are unlikely to appear in my documents.

Gaussler
  • 12,801
  • What kind of editing program do you use? Can it handle regular expressions? – Mico Dec 21 '14 at 10:24
  • What do you mean by "regular expressions"? – Gaussler Dec 21 '14 at 10:30
  • 2
    Would it be better to somehow identify what is quote, and then replace them with a macro call instead? See for example the csquotes package, so that you end up with \somemacro{the cats' hats can't fit med}, which would give you a better structure plus the ability to control the quoting style from the preamble. – daleif Dec 21 '14 at 10:38
  • The problem is that my document is an XML document that I haven't written myself, and I kinda cannot control how it is encoded. It just uses the apostrophe for everything. – Gaussler Dec 21 '14 at 10:41
  • \MakeOuterQuote from csquotes would be the perfect solution if it in fact supported apostrophes. – Gaussler Dec 21 '14 at 10:45
  • @Mico, I'm using XSLT to convert an XML document to XeLaTeX, then naturally, I compile it using the latter. Did that answer your question? :-) – Gaussler Dec 21 '14 at 10:52
  • 1
    https://en.wikipedia.org/wiki/Regular_expression – Júda Ronén Dec 21 '14 at 11:07
  • Well, if XSLT supports that, I guess it could be encoded into that. But about how to do that I have no idea. – Gaussler Dec 21 '14 at 11:18
  • When the input is bad, the best thing is to fix it. Abusing the same character for different purposes is wrong to begin with. – egreg Dec 21 '14 at 11:19
  • I'm afraid the authors of my XML document most likely aren't going to do that. – Gaussler Dec 21 '14 at 11:21
  • I was wondering: Would \MakeOuterQuote work on apostrophes if the apostrophe was made into an inactive character? In that case, how do you do that? – Gaussler Dec 21 '14 at 11:22
  • As well as contractions such as 'tis, mentioned in the question, there are also other edge cases: years and decades referred to as, e.g., '54 and the '90s; the Dutch city of 's-Hertogenbosch. – David Richerby Dec 21 '14 at 12:37

3 Answers3

6

If the apostrophe and end-quote needn't to be distinguished, then you can try:

\def\leftquote{`}\def\rightquote{'}

\catcode`\'=13
\def'{\ifvmode\leftquote \else \ifdim\lastskip=0pt \rightquote \else \leftquote\fi\fi}

He said, 'the cats' hats can't fit me'.

If the document uses such "double quotes" and you need to typeset them, then you can use following definition:

\def\leftqquote{``}\def\rightqqoute{''} % or another definition, can depend on fonts
                                        % put this definition before \catcode`\'=13
\catcode`\"=13
\def"{\bgroup\def"{\rightqqoute\egroup}\leftqquote}

The normal "quotes" are here.

Edit: Because there were comments about ' in math mode, like `$f'$, I add this functionality here:

\def\leftquote{`}\def\rightquote{'}

\catcode`\'=13
\def'{\ifmmode \expandafter\mathprime \else\ifvmode \leftquote \else
        \ifdim\lastskip=0pt \rightquote \else \leftquote\fi\fi\fi}
\def\mathprime{^\bgroup\catcode`\'=12 \csname prim@s\endcsname}

Unlike the accepted answer, I don't rewrite the \pr@m@s macro, only set the catcode temporary back to 12. This solution works in both plain TeX and LaTeX, because \prim@s macro is used in both macro packages with the same meaning.

wipet
  • 74,238
  • Beware of ' in math mode, e.g. $f'(x)$ – karlkoeller Dec 21 '14 at 11:36
  • OK, if the document uses $f'$ the we can simply add \ifmmode in this definition. – wipet Dec 21 '14 at 11:37
  • This method won't handle instance of 'tis, 'round, and similar start-of-word elisions correctly. – Mico Dec 21 '14 at 11:43
  • 3
    Of course, start-word elisions cannot be recognized without automaton fully understand the text. But if these occurrences are only sparse then proofreading of the document and individual correcting can eliminate this. – wipet Dec 21 '14 at 11:51
6

I think that you want (open quote) only when ' is preceded by space or nothing at all.

This also copes with math mode.

\documentclass{memoir}

\makeatletter
\let\apostrophe'
\catcode`'=\active
\protected\def'{%
  \ifvmode
    `%
  \else
    \ifmmode
      \expandafter\expandafter\expandafter\active@math@prime % for math
    \else
      \ifdim\lastskip>0pt
        `%
      \else
        \apostrophe
      \fi
    \fi
  \fi
}
% redefine \pr@m@s to look for an active '
\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}
\makeatother

\begin{document}

He said, 'the cats' hats can't fit me'.

'Ouch,' he said.

Some 'derivatives' $f'+f''+f'''$.

\end{document}

enter image description here

Note that the second line would have an open quote after the comma, under your specification.

Notes

  • \protected is used or \section{A 'quoted'} would fail
  • \section{'quoted'} will fail and there's not much to do about this

The following seems to fix the problem with section and chapter titles, when a trailing quote appears.

Compiling with both pdflatex and xelatex the result is the same.

\documentclass{memoir}

\usepackage{ifxetex}
\ifxetex
  \usepackage{fontspec}
%  \setmainfont{Linux Libertine O}
\fi

\makeatletter
% fix latex's habit of resetting ' during \write
\begingroup
\obeylines\obeyspaces%
\catcode`\'\active%
\gdef\@resetactivechars{%
\def^^M{\@activechar@info{EOL}\space}%
\def {\@activechar@info{space}\space}}%
\endgroup

\renewcommand{\cftchapteraftersnumb}{\hskip1sp\relax}
\let\ORIM@sect\M@sect
\def\M@sect#1#2#3#4#5#6[#7][#8]#9{%
  \ORIM@sect{#1}{#2}{#3}{#4}{#5}{#6}[\hskip1sp\relax#7][\hskip1sp\relax#8]{\hskip1sp\relax#9}%
}
\catcode`'=\active
\protected\def'{%
  \ifvmode
    `%
  \else
    \ifmmode
      \expandafter\expandafter\expandafter\active@math@prime % for math
    \else
      \relax
      \ifdim\lastskip=1sp
        `%
      \else
        \ifdim\lastskip>0pt
          `%
        \else
          \rq
        \fi
      \fi
    \fi
  \fi
}
% redefine \pr@m@s to look for an active '
\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}
\makeatother

\begin{document}
\tableofcontents
\chapter{'Chapter'}

\section{'Quoted'}

He said, 'the cats' hats can't fit me'.

'Ouch,' he said.

Some 'derivatives' $f'+f''+f'''$.

\end{document}
egreg
  • 1,121,712
4

If I understand your workflow correctly, it looks like (a) there's a process that creates a compilable .tex file that has some issues and (b) you need to address these issues before you run the .tex file through LaTeX. Moreover, the main issue appears to be that "dumb" (single) quotes are used throughout the tex file and that you wish to find a way to replace some of the single-quote characters (aka apostrophe characters) with back-quote characters (aka backticks). You haven't indicated explicitly which language your document is set in, so I'll assume for now that it's in English.

  • If the problematic apostrophe characters occur only in mid-sentence positions (and are thus preceded by space characters) and not at the beginnings of lines, I suppose you could fire up an editor and replace all instances of space-apostrophe with space-backtick.

  • If the problematic apostrophe characters can occur at the beginnings of lines, you'll need to check if your editor can perform a search-and-replace of newline_char-apostrophe for newline_char-backtick. Since you haven't indicated which editor (if any) you choose, I can't offer more-specific advice on how to go about this task.

Caveat: Both of these methods (and similarly automated methods) will not produce correct results if you have "literary" expressions such as 'tis -- short for " it is" -- in your input text. (The apostrophe indicates an elision rather than the start of a quote in such circumstances, and the apostrophe should therefore not be replaced with a backtick.)

Mico
  • 506,678