# [Tex/LaTex] Typewriter apostrophe in typewriter font for apostrophe symbol

punctuationtypewriter

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.

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}