如何在 alltt 环境中排版 em 破折号

如何在 alltt 环境中排版 em 破折号

---我尝试在环境中排版破折号alltt,但是我得到的是三个连字符而不是一个破折号。

平均能量损失

\documentclass[12pt]{article}

\usepackage{alltt}

\begin{document}
Usually, \verb"---" becomes an em dash, i.e. "---", in normal mode.
But in the alltt environment, it becomes three hyphens.
\begin{alltt}
Please---will you give me an em dash?
\end{alltt}
\end{document}

回复评论 评论问我为什么要在等宽环境中排版破折号。我想排版一个看起来像破折号的破折号,但在等宽环境中占用两个空格的长度,即像“--”一样,但连接在一起形成一个像破折号的字符。

答案1

我的建议是将其以宏的形式“隐藏”,其中破折号取自\normalfont

在此处输入图片描述

\documentclass{article}
\newcommand{\emdash}{{\normalfont ---}}
\usepackage{alltt}

\begin{document}
Usually, \verb"---" becomes an em dash, i.e. ``---'', in normal mode.
But in the alltt environment, it becomes three hyphens.
\begin{alltt}
Please\emdash{}will you give me an em dash?
\end{alltt}
\end{document}

除非等宽字体带有破折号,否则它将被设置为单独的破折号。您可以强制使用等宽破折号,也许可以使用\emdash类似于以下内容的定义:

在此处输入图片描述

\documentclass{article}
\makeatletter
\newlength{\emdashttlen}
\settowidth{\emdashttlen}{\ttfamily---}
\newcommand{\emdash}{\makebox[\emdashttlen]{-\cleaders\hbox{\hspace{-.45ex}-}\hfill\kern0pt}}
\makeatother
\usepackage{alltt}

\begin{document}
Usually, \verb"---" becomes an em dash, i.e. ``---'', in normal mode.
But in the alltt environment, it becomes three hyphens.
\begin{alltt}
Please---will you give me an em dash?
Please\emdash{}will you give me an em dash?
\end{alltt}

\end{document}

有关领导者的一些信息可以在想要用重复的字符串填充行

答案2

简单的解决方案:使用 UTF-8

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{alltt,etoolbox}
\apptocmd{\alltt}{\def\textemdash{{\fontfamily{cmvtt}\selectfont---}}}{}{}

\begin{document}

\begin{alltt}
Please—will you give me an em dash?
Please--will you give me an em dash?
\end{alltt}

\end{document}

请注意,第一行包含 - (Unicode U+2014 EM DASH)

在此处输入图片描述

更复杂的解决方案:转变---{\fontfamily{cmvtt}\selectfont---}

\documentclass{article}

\usepackage{alltt,etoolbox}

\makeatletter
\begingroup\lccode`~=`-
\lowercase{\endgroup\apptocmd{\alltt}{\let~\alltthyphen}{}{}
\newcommand\alltthyphen{\@ifnextchar~{\@alltthyphen@i}{\char`\- }}
\def\@alltthyphen@i#1{% #1 is -
  \@ifnextchar~{{\fontfamily{cmvtt}\selectfont---}\@gobble}{\char`\-\char`\- }%
}}
\makeatother

\begin{document}

\begin{alltt}
Please---will you give me an em dash?
PleaseXXwill you give me an em dash?
\end{alltt}

\end{document}

输出与之前相同

过于复杂的解决方案:伪造破折号,因为字体系列没有可变宽度的打字机字体

\documentclass{article}

\usepackage{alltt,etoolbox}
\usepackage{tgcursor}

\makeatletter
\begingroup\lccode`~=`-
\lowercase{\endgroup\apptocmd{\alltt}{\let~\alltthyphen}{}{}
\newcommand\alltthyphen{\@ifnextchar~{\@alltthyphen@i}{\char`\- }}
\def\@alltthyphen@i#1{% #1 is -
  \@ifnextchar~{\fake@em@dash\@gobble}{\char`\-\char`\- }%
}}
\def\fake@em@dash{%
  \sbox0{--}%
  \makebox[\wd0][s]{-\hss-\hss-}%
}
\makeatother

\begin{document}

\begin{alltt}
Please---will you give me an em dash?
PleaseXXwill you give me an em dash?
\end{alltt}

\end{document}

在此处输入图片描述

--您可能希望使用 just而不是来简化设置---;这需要更改上面的宏。

相关内容