2

I would like to obtain a straight quote, ', when I type ' in my LaTeX input, and a straight double quote, ", when I type " in my LaTeX input. This, everywhere, in every case: normal text, section title, inside a \texttt or a \verb command, … I want to have “nice” quotes only when I input such with my keyboard (which I’ll do myself, thank you).

This questions lists several similar question, but I have not found this specific question: most questions are in the case of XeLaTeX or are about getting straight quotes only in some special case.

I have tried to adapt an answer to the above question to my case. Here is a MWE.

\documentclass{article}
\usepackage{textcomp}
\begin{document}
\newcommand{\algoupquote}{\mbox{\textquotesingle}}
\newcommand{\algoupquotes}{\mbox{\char`\"}}

\begingroup\lccode`\~=`\'\lowercase{\endgroup\let~}\algoupquote
\begingroup\lccode`\~=`\"\lowercase{\endgroup\let~}\algoupquotes
\catcode`\'=\active\catcode`\"=\active

A "“'straight'” quoting".

\texttt{A "'straight' quoting".}
\end{document}

I have several questions.

  • Is this code “dangerous”, in the sense that it would be doing something (e.g., declaring new active characters) that will strongly increase the probability of incompatiblity (or strange behavior) with some package that I might want to use one day or another? (I’d rather give up my dreams of taking care of my quotes myself than risking all sorts of problems and long debugging sessions in the future.)
  • How can I suitably simplify the above code? In particular, I’d like to replace the \algoupquote command by its definition, but if I do that, I get insulted by LaTeX (if I dare report it, it says “Argument of \UTFviii@three@octets has an extra }”).
  • Is there a risk of sensibly enlarging the compilation time, for big documents?
  • Can I / should I rather declare ' and " active and use something like \DeclareUnicodeCharacter{0027}{\textquotesingle}? What would be the advantage and drawback?

(I know that XeLaTeX and other LaTeX variants have been designed for treating nicely Unicode input, but I prefer to keep maximal compatibility and use the standard LaTeX that most people use.)

  • TeXnicCenter converts " into `` and '' automatically (context dependent), which is a real pain when I need a ". – John Kormylo Dec 22 '19 at 23:47
  • It's an editor preference in TeXworks, Format | Smart Quotes | None; TeX ligatures, TeX commands, Unicode characters; Apply to selection. – Cicada Dec 23 '19 at 00:59
  • \textquotesingle and \textquotedbl work without any packages; they must be in the kernel. Possibly related: https://tex.stackexchange.com/questions/7735/how-to-get-straight-quotation-marks – Cicada Dec 23 '19 at 02:12
  • Correction: pdflatex still generates the error; the commands are defined in xelatex and lualatex. – Cicada Dec 23 '19 at 05:51
  • @Cicada these commands require \textcomp. I want to use ' in my source, not \textquotesingle (less readable, does not copy-paste to other document formats…). The question you refer to is about XeTeX. – Olivier Cailloux Dec 23 '19 at 10:39
  • Yes I misread the meaning of "my latex". I did not realise that you were restricted to pdflatex. I do everything in xelatex, and I was wondering why it worked. There, the command in tuenc.def is \DeclareTextCommand\textquotesingle \UnicodeEncodingName{\remove@tlig{"0027}}, otherwise you have to switch off tex-ligatures manually to use ^^^^0027 as straight quote direct from unicode. None of it is applicable to pdflatex because of the small size, in terms of glyph slot numbers, of the legacy font files used by pdflatex. – Cicada Dec 23 '19 at 12:00
  • Legacy 8-bit fonts, and Unicode fonts, are two completely incompatible universes. You can do iftex logic to determine the engine being used, for true compatibility. Undoing the ligatures (mapping) that TeX automatically applies seems to be what you want to do. But in pdflatex, which encoding/fonts will you get things from? textcomp already does the TS1 encoding for \textquotesingle - there is much juggling required because the character is not in the cmr default font (128 chars only). – Cicada Dec 23 '19 at 12:29
  • Does \tqs as a shorter version work for what you want in terms of readability? - it then matches with \lq and \rq. This runs under pdflatex: \documentclass{article} \usepackage{textcomp} \newcommand\tqs{\textquotesingle} \begin{document} \tqs{}z\tqs{} \lq{}z\rq{} \end{document} – Cicada Dec 23 '19 at 12:39
  • Another option for a short command is to redefine \' - it prints the acute accents, but you can \renewcommand\'{\textquotesingle} and use it as \'{x}\' (if you don't have any acutes). – Cicada Dec 23 '19 at 13:05
  • Does egreg's answer and font table at https://tex.stackexchange.com/questions/315266/typewriter-apostrophe?noredirect=1&lq=1 help? "\texttt{\fontencoding{OT1}\selectfont\symbol{13}} produces the straight quote in typewriter type". – Cicada Dec 23 '19 at 13:14
  • Thanks for your comments. I am curious if it’s possible (and reasonably easy) to use ' in my source code, and not a command such as \'. When I type a, I get the letter a; when I type a comma, I get a comma. When I type a straight quote, I would like to get a straight quote. And so on. As much as reasonably possible, of course. – Olivier Cailloux Dec 23 '19 at 20:01
  • Typing input direct is Unicode concept: 蘆 gives 蘆. There is no ' in OT1 encoding - not enough slots -, so you have to swap over to TS1 encoding (commands), and stop the ligature mechanism from working - see https://tex.stackexchange.com/questions/373440/libertine-pdftex-using-th-and-qu-ligatures-with-t1-font-encoding?rq=1. \documentclass{article} \usepackage{textcomp}%for TS1 encoding \newcommand\qa{\begingroup\fontencoding{TS1}\selectfont '\endgroup} \newcommand\qm{\begingroup\fontencoding{T1}\selectfont "\endgroup} \begin{document} 'z' ''z'' "z" \qa{}z\qa{}\qm{}z\qm{}\end{document} – Cicada Dec 24 '19 at 02:34
  • Use xelatex/lualatex with no ligatures. For pdflatex, do a \documentclass{article}\usepackage{textcomp}\usepackage{fonttable}\begin{document} \xfonttable{TS1}{\rmdefault}{\seriesdefault}{\shapedefault}\end{document} and look in position 39; then do a \documentclass{article}\usepackage{fonttable}\begin{document}\xfonttable{OT1}{\rmdefault}{\seriesdefault}{\shapedefault}\end{document} and look in position 39 (and 96). – Cicada Dec 24 '19 at 02:40
  • 1
    Or with T1 encoding: \documentclass{article} \usepackage[T1]{fontenc} \usepackage{textcomp} \catcode39=\active \let'\textquotesingle \begin{document} A "'straight' quoting". \texttt{A "'straight' quoting".} \end{document} (not sure about side effects). – Marijn Dec 25 '19 at 19:17
  • 1
    @Marijn thanks! Do you have a similar trick for the backquote, `? I’d like it to display as-is as well. (Something like \catcode\\`=\active \let`\textasciigrave`, I suppose, but this produces an error message, unsurprinsingly.) – Olivier Cailloux Dec 27 '19 at 14:01
  • Also, compiling $'a'$ does not end with this code, endlessly writing: LaTeX Warning: Command \textquotesingle invalid in math mode. (I don’t mind much that it doesn’t work in math mode as this is text anyway, but I mind that it makes compilation cycle.) – Olivier Cailloux Dec 27 '19 at 14:07

1 Answers1

1

A solution is to make the single quote and backtick active and define them to be the required commands. Within the definition different output can be set for math mode and text mode. This solution is partly based on Replace backtick ` with power/caret ^ in math mode. I would like to repeat the warning given there that this may break any code that expects either character to have the usual category code.

T1 font encoding is used in the MWE because " is available in that encoding and therefore no additional catcode changes or definitions are needed.

MWE:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}

\def\pr{^{\prime}}
\def\qsingle{\ifmmode\pr\else\textquotesingle\fi}
\def\gravesingle{\ifmmode\pr\else\textasciigrave\fi}
\catcode`\'=\active
\let'\qsingle
\catcode`\`=\active
\let`\gravesingle

\begin{document}
A "'straight' quoting".

\texttt{A "'straight' quoting".}

$a 'b' c` x$

The character ` is the grave accent.
\end{document}

Result:

enter image description here

Marijn
  • 37,699
  • 1
    I’d be most interested by a concrete example of something breaking because of this code, in order to know what kind of problems to expect (or perhaps, give up using this code altogether if it creates too much problems). – Olivier Cailloux Dec 28 '19 at 09:23
  • The linked-to answer claims to redefine the catcode only for math-mode (though I don’t understand how the code provided there achieves that). If I understand correctly, the code provided here redefines the catcode for both text and math mode. Is it possible to similarly redefine the catcode only for text mode, and leave it alone for math mode (therefore possibly reducing the adverse effects)? – Olivier Cailloux Dec 28 '19 at 09:28
  • I have to abandon this at least for the Grave Accent symbol: in that context, \catcode\'=\activeis incompatible with\usepackage{graphicx}orempheq`. (However, the linked-to answer does not suffer this incompatibility.) – Olivier Cailloux Dec 28 '19 at 12:32
  • In the previous comment I meant that \catcode\\`=\activeis incompatible with\usepackage{graphicx}. But moving the two lines\catcode`\`=\active \let`\gravesingleafter the\begin{document}works when loading thegraphicx,empheq`, and various other packages. Not sure about their use, however. – Olivier Cailloux Dec 28 '19 at 13:20
  • Also, this fails to produce straight quotes in \verb|A "'straight' quoting".|. Any idea? – Olivier Cailloux Dec 28 '19 at 13:36