如何在 alltt 環境中排版全破折號

如何在 alltt 環境中排版全破折號

---我正在嘗試在環境內部使用全角破折號進行排版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}

在此輸入影像描述

您可能想使用;--來簡化設置---這需要更改上面的巨集。

相關內容