alltt 環境で em ダッシュをタイプセットする方法

alltt 環境で em ダッシュをタイプセットする方法

---環境内でem ダッシュを使用してタイプセットしようとしていますallttが、単一の em ダッシュではなく 3 つのハイフンが表示されます。

ムウェ

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

コメントへの返信 コメントでは、なぜ等幅環境で em ダッシュをタイプセットしたいのかと尋ねられました。私は、等幅環境で em ダッシュのように見えるが 2 つのスペースの長さを占めるダッシュ、つまり「--」のようでありながら結合されて単一の em ダッシュのような文字を形成するダッシュをタイプセットしたいのです。

答え1

私の提案は、em-dash を次の場所から取得したマクロの形式で「隠す」ことです\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}

等幅フォントに em ダッシュが付属していない限り、em ダッシュは個別のダッシュとして設定されます。次のように定義することで、等幅フォントのように見える em ダッシュを強制的に設定できます\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}

出力は以前と同じです

複雑すぎる解決策: フォントファミリーに可変幅のタイプライターフォントがないため、emダッシュを偽装する

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

ここに画像の説明を入力してください

--の代わりにだけを使用して設定を簡略化することもできますが---、その場合は上記のマクロを変更する必要があります。

関連情報