# Changing all single quotes to be straight when within texttt

I have a document with many ' characters inside of texttt. Is there a way to make them all appear straight when in texttt?

I know that I can search/replace all ' with \textsinglequote:

Perhaps there is some preamble declaration to tell texttt to render all ' as textsinglequote?

• Did you know that LuaLaTeX and XeLaTeX both typeset straight single quotes (apostrophes) automatically whenever a monospaced font is in use (as is the case with the argument of \texttt)? I've posted an answer to elaborate on this observation. – Mico Jun 13 at 20:06
• For which versions of (Xe/Lua)LaTeX is this known to work? – jmlarson Jun 14 at 12:51
• It certainly works for the current versions, i.e., the ones distributed with TeXLive2018. I believe it's been working like this for quite some time, but I don't have access to older TeX distributions. – Mico Jun 14 at 13:07

Here is an approach using active characters, but still allowing \texttt usage inside arguments of other macros. (because it uses \scantokens).

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{upquote}
\usepackage{etoolbox} % for robustify

\robustify{\texttt}
\let\originaltexttt\texttt

\begingroup
\catcode'=\active
\catcode=\active
\globaldefs1
\makeatletter
\renewrobustcmd{\texttt}[1]{%
{%
\everyeof{\noexpand}\endlinechar-1
\expandafter\catcode\string=\active
\expandafter\catcode\string'=\active
\let'\textquotesingle
\let\textasciigrave
\ifx\encodingdefault\upquote@OTone
\ifx\ttdefault\upquote@cmtt
\def'{\char13 }\def{\char18 }%
\fi
\fi
\scantokens{\originaltexttt{#1}}%
}%
}%
\endgroup

\begin{document}

\null\vfill
abc'def---\texttt{abc'def}---abc'def
\footnote{\texttt{This is a footnote ''}}

\textit{\texttt{abcdef'ghi}}---\texttt{\textit{abcdef'ghi}}
\footnote{\textit{\texttt{This is another footnote ''}}}

\end{document}


The advantage compared to my other answer is that it makes do with the document typewriter font, it does not modify it. The disadvantage is that \scantokens may reserve some surprises. Concretely the main issue coming to my mind is that one should not use it in a context where the space character has been made active, and the main case will be the alltt environment (of course verbatim is no issue, as \texttt macro does not get executed). But that's already a bit specialized context, so generally speaking it will be fine.

Notice that my approach loads package upquote so verbatim environment will be with straight single quotes too.

• Very nice answer! – Steven B. Segletes Jun 14 at 9:56
• @StevenB.Segletes yours already was :). – jfbu Jun 14 at 11:43
• Though a pity our relative scores don't reflect that (yet!) – Steven B. Segletes Jun 14 at 11:52
• in my text I wanted to say one could not use \texttt inside alltt environment... I did not want to say one could not use alltt environment! And who would use \texttt inside alltt which, as its name indicates is already using typewriter? :) . (now that I think about it...) – jfbu Jun 14 at 14:22

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}


• With this code ' is active also after the \texttt. Try Ups' \texttt{That's life!} Ups'. – Ulrike Fischer Jun 13 at 15:39
• @UlrikeFischer Thank you for pointing that out. Fixed. – Steven B. Segletes Jun 13 at 16:36
• Remember about letltxmacro. – egreg Jun 13 at 16:40
• @egreg The fact that you mention it means I should adapt to it. Does that help protect me against moving arguments or something? – Steven B. Segletes Jun 13 at 16:59
• – Steven B. Segletes Jun 13 at 17:03

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 ''.

• Ah yes...the argument within an argument problem of catcodes. – Steven B. Segletes Jun 13 at 17:16
• I receive an undefined control sequence error when I run this example in pdflatex and latex ! Undefined control sequence. \texttt ...n_texttt_tl {#1}\regex_replace_all:nnN {'}{\c {textquotesingle}}\... l.20 \section{abc'def \texttt{abc'def} abc'def} – jmlarson Jun 13 at 18:30
• @jmlarson Are you loading textcomp? – egreg Jun 13 at 19:46
• Yes, I copied and pasted (and compiled with pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian)) – jmlarson Jun 13 at 20:26
• @jmlarson You have an outdated TeX distribution. – egreg Jun 13 at 21:18

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.

Here's a solution that works with both LuaLaTeX and XeLaTeX. Do examine the code below the screenshot: you'll notice that absolutely nothing needs to be done to achieve the desired outcome. This is because LuaLaTeX and XeLaTeX use "straight" apostrophes (single quotes) automatically if a monospaced font is in use (as is the case with \texttt). This is true regardless of whether or not the fontspec package is loaded.

% !TEX TS-program = lualatex   %% or xelatex
\documentclass{article}
\begin{document}
\section{abc'def \texttt{abc'def} abc'def}

abc'def \texttt {abc'def} abc'def
\end{document}

• This does not result in straightquotes for XeTeX, Version 3.14159265-2.6-0.99992 (TeX Live 2015/Debian) (preloaded format=xelatex) – jmlarson Jun 14 at 12:50