15

Is there a way of producing a typewriter apostrophe (U+0027) in a typewriter font when an apostrophe symbol is entered in .tex document?

To give an example, I’d like \texttt{'hello'} to produce 'hello' with typewriter apostrophes (U+0027) rather than ’hello’ with quotation marks (U+2019) currently produced by PDFLaTeX. There are command-based solutions that offer \texttt{\something hello\something} syntax, but there might be a way of using \texttt{'hello'} in a document and getting the desired result, perhaps through changing typewriter font encoding or by another trick. I’m willing to put into document preamble a code of any complexity.

upquote package delivers the result for verbatim environment. I'm looking for exactly the same functionality but for \texttt.

2

2 Answers 2

7

A streamlined version of karlkoeller's solution:

\documentclass{article}
\usepackage{textcomp,upquote}
\usepackage{regexpatch}

\makeatletter
\def\active@text@prime{\ifin@texttt\textquotesingle\else'\fi}
\def\active@math@prime{^\bgroup\prim@s}
\newif\ifin@texttt

\regexpatchcmd{\pr@m@s}{\'}{\cA\'}{}{}
\xapptocmd{\ttfamily}{\in@texttttrue}{}{}

\begingroup\lccode`\~=`\'
\lowercase{\endgroup\protected\def~}{%
  \ifmmode
    \expandafter\active@math@prime
  \else
    \expandafter\active@text@prime
  \fi}
\AtBeginDocument{\catcode`\'=\active}

% fix \@resetactivechars not to redefine the active apostrophe
\begingroup
\obeylines\obeyspaces%
\gdef\@resetactivechars{%
\def^^M{\@activechar@info{EOL}\space}%
\def {\@activechar@info{space}\space}%
}%
\endgroup

\makeatother

\begin{document}
\texttt{'hello'} `here the quotation marks are normal'

Also \verb|'hello'| works

And here is some math $f''(x)$
\end{document}

enter image description here

1
  • @val Fixed: I always forget about \@resetactivechars
    – egreg
    Jan 10, 2015 at 11:06
13

This is a solution:

\documentclass{article}
\usepackage{letltxmacro}

\LetLtxMacro{\oldtexttt}{\texttt}
\def\oldapostrophe{'}
\catcode`\'=\active

\makeatletter
\def\active@text@prime{\oldapostrophe}

% this trick was done by egreg in another answer of mine

\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}

\protected\def'{%
  \ifmmode
    \expandafter\active@math@prime
  \else
    \expandafter\active@text@prime
  \fi}

\renewcommand\texttt[1]{%
\def'{\symbol{13}}%
\oldtexttt{#1}%
\protected\def'{%
  \ifmmode
    \expandafter\active@math@prime
  \else
    \expandafter\active@text@prime
  \fi}
}
\makeatother

\begin{document}
\texttt{'hello'} `here the quotation marks are normal'

and here is some math $f''(x)$
\end{document} 

Output:

enter image description here

0

You must log in to answer this question.

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