Changing all single quotes to be straight when within texttt
Here I redefine \texttt
to make '
active and replace itself with \textquotesingle
. The original definition is saved in \svtexttt
.
EDITED to return '
to non-active status at the conclusion of a \texttt
.
REEDITED to use letltxmacro
, per egreg's suggestion.
REREEDITED to restore the originally defined '
after \texttt
usage, so as not to interfere with math conversion to \prime
(hat tip/mumbled curse to David).
\documentclass{article}
\usepackage{textcomp,lmodern,letltxmacro}
\LetLtxMacro{\svtexttt}{\texttt}
\catcode`'=\active %
\let\svprime'
\renewcommand\texttt{\catcode`'=\active \def'{\textquotesingle}\textttaux}
\newcommand\textttaux[1]{\svtexttt{#1}\gdef'{\svprime}\catcode`'=12 }
\catcode`'=12 %
\begin{document}
\svtexttt{That's life!}
versus
\texttt{That's life!}
Is ' active? No.
Testing prime: $x' =dx/dt$
\end{document}
You could make '
active, but this has the big disadvantage that \texttt
can no longer be used in the argument to another command.
\documentclass{article}
\usepackage{textcomp}
\usepackage{xparse}
\usepackage{etoolbox} % for robustify
\robustify\texttt
\let\originaltexttt\texttt
\newcommand\activatestraightquote{%
\begingroup\lccode`~=`'
\lowercase{\endgroup\let~}\textquotesingle
\catcode`'=\active
}
\renewrobustcmd{\texttt}{%
\begingroup\activatestraightquote
\innertexttt
}
\newcommand{\innertexttt}[1]{\originaltexttt{#1}\endgroup}
\begin{document}
abc'def \texttt{abc'def} abc'def
\end{document}
A solution that's free from this problem.
\documentclass{article}
\usepackage{xparse}
\usepackage{textcomp}
\ExplSyntaxOn
\RenewDocumentCommand{\texttt}{m}
{
\group_begin:
\tl_set:Nn \l_jmlarson_texttt_tl { #1 }
\regex_replace_all:nnN { ' } { \c{textquotesingle} } \l_jmlarson_texttt_tl
\ttfamily
\tl_use:N \l_jmlarson_texttt_tl
\group_end:
}
\ExplSyntaxOff
\begin{document}
\section{abc'def \texttt{abc'def} abc'def}
abc'def \texttt{abc'def} abc'def
\end{document}
This can also be extended so as to treat also "
and ''
.
You can use package newtxtt
with option straightquotes
.
\documentclass{article}
\usepackage[straightquotes]{newtxtt}
\begin{document}
\section{abc'def \texttt{abc'def} abc'def}
abc'def \texttt{abc'def} abc'def
\texttt{`xyz', ``xyz'', "xyz"}
\end{document}
This method, by essence, replaces completely the typewriter font, naturally. In the example above it seems to fit not so bad with default serif font.