11

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 \textquotesingle:

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

jmlarson
  • 625
  • 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 '18 at 20:06
  • For which versions of (Xe/Lua)LaTeX is this known to work? – jmlarson Jun 14 '18 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 '18 at 13:07

5 Answers5

11

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}

enter image description here

8

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}

enter image description here

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

enter image description here

egreg
  • 1,121,712
5

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}

enter image description here

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.

5

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{abc`def'ghi}}---\texttt{\textit{abc`def'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.

enter image description here

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

3

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.

enter image description here

% !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}
Mico
  • 506,678
  • 1
    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 '18 at 12:50
  • Same as @jmlarson. Not sure what other font settings could be messing with this? – Domi Aug 10 '22 at 12:35
  • @Domi - Thanks. I currently use MacTeX2022. With both XeTeX 3.141592653-2.6-0.999994 and LuaHBTeX 1.15.0, I can re-create the above screenshot, which I originally posted more than 4 years ago, exactly, viz. "straight" quotes when using ' (single apostrophe) while in \texttt mode. No idea why it's not working on other systems/TeX distributions. – Mico Aug 10 '22 at 16:34
  • @Mico Thanks for following up. I ended up using the rather restrictive hackfix posted above. Frustrating, to say the least, that simple things cannot just easily work or be understood across different settings and setups. Maybe there is some fontspec interference? Or maybe it has to do with the fact that I also have setCJKmainfont, or...? I don't feel like debugging it, but maybe if I have a day or two in the future where I feel like it, I'll apply binary search to find out what the single line is that causes this problem ‍♀️ – Domi Aug 10 '22 at 17:57