打字机撇号,打字机字体中的撇号符号

打字机撇号,打字机字体中的撇号符号

当在文档中输入撇号符号时,是否有办法以打字机字体生成打字机撇号(U+0027).tex

举个例子,我想用打字机撇号 (U+0027) 而不是PDFLaTeX 目前生成的引号 (U+2019) 来\texttt{'hello'}生成。有基于命令的解决方案提供语法,但可能有一种方法可以在文档中使用并获得所需的结果,也许是通过更改打字机字体编码或其他技巧。我愿意在文档序言中放入任何复杂的代码。'hello'’hello’\texttt{\something hello\something}\texttt{'hello'}

upquote包为 verbatim 环境提供结果。我正在寻找完全相同的功能,但适用于\texttt

答案1

karlkoeller 解决方案的精简版本:

\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}

在此处输入图片描述

答案2

这是一个解决方案:

\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} 

输出:

在此处输入图片描述

相关内容