更改 detokenize 中下划线的大小

更改 detokenize 中下划线的大小

我的问题与这个。那里的解决方案对于文本来说很有效,但这是我的情况。

我想特别排版所有包的名称,这样我引用的内容就一目了然了。为此,我定义了命令:

\newcommand*{\pkg}[1]{\textsf{\detokenize{#1}}}

我使用 是\detokenize因为我必须_在包名称中使用 ,所以我不想要额外的\。此外,有时我会从某处复制粘贴名称,因此它已经具有 形式package_name


我使用lmodern字体,主要是出于惯性。当然,如果你能推荐一种更好的字体,它有不错的下划线并且看起来不错,那就太好了。我尝试了palatinotimes,正如相关问题中所建议的那样。我不喜欢palatinotimes可以工作,但\textsf相对于正常文本来说似乎太大了。


\documentclass[12pt]{report}
\usepackage{lmodern}        % use modern latin fonts
\usepackage[T1]{fontenc}    % use 8 bit output font encoding with more glyphs
\usepackage[utf8]{inputenc} % so you can type ă

\usepackage{relsize}        % allows you to resize individual characters e.g. underscore
\renewcommand{\_}{\textscale{.5}{\textunderscore}}

\newcommand*{\pkg}[1]{\textsf{\detokenize{#1}}} % typeset the name of a package: \pkg{tum_ardrone}
\newcommand*{\ds}[1]{\textsl{\textsf{\detokenize{#1}}}} % and of a dataset \ds{RIMES}
% these complicated definitions ensure that you always get correct spacing after the command
% and that you cannot forget the correct form (e.g. \FRCNN{}) because this would give you
% bad spacing; and that the name is not hyphenated
\makeatletter
    \@ifdefinable{\FRCNN}{\def\FRCNN#{\mbox{\pkg{Faster R-CNN}}}}
    \@ifdefinable{\CTPN}{\def\CTPN#{\mbox{\pkg{CTPN}}}}
    \@ifdefinable{\RESNET}{\def\RESNET#{\mbox{\pkg{ResNet-101}}}}
    \@ifdefinable{\CRNN}{\def\CRNN#{\mbox{\pkg{CRNN}}}}
\makeatother


\begin{document}

Template\_bin % works

\pkg{Template_bin} % nope

\end{document}

编辑:忘了说我使用诸如 等东西\section{The \pkg{Template_bin}}\caption[\pkg{Pkg_name}]{...}理想情况下,它也可以在那里工作,但如果太难,我想我可以找到解决方法或不在\pkg{}那里使用。

在此处输入图片描述

答案1

要求 TeX 进行所需的替换,而不触碰其他标记。

我更愿意\scalebox\textscale我还为您的复杂定义添加一些简化。

\documentclass[12pt]{report}
\usepackage{lmodern}        % use modern latin fonts
\usepackage[T1]{fontenc}    % use 8 bit output font encoding with more glyphs
\usepackage[utf8]{inputenc} % so you can type ă
\usepackage{graphicx}
\usepackage{xparse}

\RenewDocumentCommand{\_}{}{\scalebox{0.5}[1]{\textunderscore}}

\ExplSyntaxOn
\NewDocumentCommand{\changeunderscore}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \regex_replace_all:nnN { _ } { \c{_} } \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

% typeset the name of a package: \pkg{tum_ardrone}
\newcommand*{\pkg}[1]{\textsf{\changeunderscore{#1}}}
% and of a dataset \ds{RIMES}
\newcommand*{\ds}[1]{\textsl{\textsf{\changeunderscore{#1}}}}
% syntactic sugar
\makeatletter
\newcommand{\newcommandb}[2]{\@ifdefinable{#1}{\def#1##{#2}}}
\makeatother

\newcommandb{\FRCNN}{\mbox{\pkg{Faster R-CNN}}}
\newcommandb{\CTPN}{\mbox{\pkg{CTPN}}}
\newcommandb{\RESNET}{\mbox{\pkg{ResNet-101}}}
\newcommandb{\CRNN}{\mbox{\pkg{CRNN}}}

\newcommandb{\TB}{\pkg{Template_bin}}

\begin{document}

Template\_bin

\pkg{Template_bin}

\TB{}

\end{document}

在此处输入图片描述

答案2

在这里我使用listofitems读取\detokenized 的输出并用\pkg替换 catcode12 的实例。_\_

\documentclass[12pt]{report}
\usepackage{lmodern}        % use modern latin fonts
\usepackage[T1]{fontenc}    % use 8 bit output font encoding with more glyphs
\usepackage[utf8]{inputenc} % so you can type ă

\usepackage{relsize}        % allows you to resize individual characters e.g. underscore
\usepackage{listofitems}
\renewcommand{\_}{\textscale{.5}{\textunderscore}}
% THIS IS WHAT I AM SEARCHING THE DETOKENIZED STRING FOR
\expandafter\setsepchar\expandafter{\detokenize{_}}%
%
\newcommand*{\pkg}[1]{%
  \edef\tmp{\detokenize{#1}}%
  \readlist\xyz{\tmp}% FIND ALL INSTANCES OF _
  {\sffamily%
  \foreachitem\x\in\xyz{\ifnum\xcnt=1\else\_\fi\x}}% REPLACE _ WITH \_
} % typeset the name of a package: \pkg{tum_ardrone}
\newcommand*{\ds}[1]{\textsl{\textsf{\detokenize{#1}}}} % and of a dataset \ds{RIMES}
% these complicated definitions ensure that you always get correct spacing after the command
% and that you cannot forget the correct form (e.g. \FRCNN{}) because this would give you
% bad spacing; and that the name is not hyphenated
\makeatletter
    \@ifdefinable{\FRCNN}{\def\FRCNN#{\mbox{\pkg{Faster R-CNN}}}}
    \@ifdefinable{\CTPN}{\def\CTPN#{\mbox{\pkg{CTPN}}}}
    \@ifdefinable{\RESNET}{\def\RESNET#{\mbox{\pkg{ResNet-101}}}}
    \@ifdefinable{\CRNN}{\def\CRNN#{\mbox{\pkg{CRNN}}}}
\makeatother


\begin{document}

\textsf{Template\_bin} % works

\pkg{Template_bin} % nope

\pkg{Template_double_bin} % nope

\end{document}

在此处输入图片描述


作为对 OP 的后续跟进,他将问题扩展到包括\pkg在章节标题中,我能做的最好的事情就是有一个安静的模式,\pkg不打印出结果,而是将其保存在 中\pkgname。这样,就可以\pkgname在章节标题之前定义\pkg[q]{My_bad},然后在章节标题中使用结果:\section{The package is \pkgname}

\documentclass[12pt]{report}
\usepackage{lmodern}        % use modern latin fonts
\usepackage[T1]{fontenc}    % use 8 bit output font encoding with more glyphs
\usepackage[utf8]{inputenc} % so you can type ă

\usepackage{relsize}        % allows you to resize individual characters e.g. underscore
\usepackage{listofitems}
\renewcommand{\_}{\textscale{.5}{\textunderscore}}
% THIS IS WHAT I AM SEARCHING THE DETOKENIZED STRING FOR
\expandafter\setsepchar\expandafter{\detokenize{_}}%
%
\makeatletter
\newcommand*{\pkg}[2][\relax]{%
  \edef\tmp{\detokenize{#2}}%
  \readlist\xyz{\tmp}% FIND ALL INSTANCES OF _
  \def\pkgname{}%
  \foreachitem\x\in\xyz{\ifnum\xcnt=1\else\g@addto@macro\pkgname{\_}\fi%
    \expandafter\g@addto@macro\expandafter\pkgname\expandafter{\x}}%
  \expandafter\def\expandafter\pkgname\expandafter{%
    \expandafter\textsf\expandafter{\pkgname}}%
  \ifx\relax#1\relax\pkgname\fi% REPLACE _ WITH \_
} % typeset the name of a package: \pkg{tum_ardrone}
\makeatother
\newcommand*{\ds}[1]{\textsl{\textsf{\detokenize{#1}}}} % and of a dataset \ds{RIMES}
% these complicated definitions ensure that you always get correct spacing after the command
% and that you cannot forget the correct form (e.g. \FRCNN{}) because this would give you
% bad spacing; and that the name is not hyphenated
\makeatletter
    \@ifdefinable{\FRCNN}{\def\FRCNN#{\mbox{\pkg{Faster R-CNN}}}}
    \@ifdefinable{\CTPN}{\def\CTPN#{\mbox{\pkg{CTPN}}}}
    \@ifdefinable{\RESNET}{\def\RESNET#{\mbox{\pkg{ResNet-101}}}}
    \@ifdefinable{\CRNN}{\def\CRNN#{\mbox{\pkg{CRNN}}}}
\makeatother


\begin{document}
\tableofcontents

\pkg[q]{My_bad}
\section{The package is \pkgname}

\textsf{Template\_bin} % works

\pkg{Template_bin} % nope

\pkg{Template_double_bin} % nope

\pkg[q]{My_other_bad}
\let\nextbad\pkgname
\pkg[q]{My_third_bad}
\section{The package names are \nextbad{} and \pkgname}
\end{document}

在此处输入图片描述

相关内容