ミニページで整列したタイプライターテキストの外観を改善する方法

ミニページで整列したタイプライターテキストの外観を改善する方法

次のコードを考えてみましょう:

\documentclass[12pt]{book}
\usepackage{microtype}
%\usepackage{ragged2e} 

\begin{document}
\thispagestyle{empty}

\begin{center}
\begin{LARGE}
\begin{minipage}{5.75in}\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
\textbf{This is a minipage. The above line of commands justifies the text and centers the last line. Notice how "nice" it appears.}
\end{minipage}
\end{LARGE}
\end{center}

\vspace*{25pt}

\begin{center}
\begin{LARGE}
\begin{minipage}{5.75in}\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
\spaceskip1em plus 1em \xspaceskip 1em plus 2em\relax
\texttt{I have, with the two above lines of commands, been able to justify the tttext in this minipage, as well as center the last line of output. This is what I want; however, the appearance does not seem as "nice" as the content of the above minipage.}
\end{minipage}
\end{LARGE}
\end{center}
\end{document}

生成する

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

2 番目のミニページは、私が求めているもの (出力の最後の行が中央揃えで左揃えと右揃えのテキスト) を生成しますが、最初のミニページの両端揃えの出力と比較すると、見た目が見劣りするようです。

パッケージを呼び出すことで解決できると考えましたragged2eが、実際に実行すると、テキストの最後の行が中央揃えになるほかtexttt、状況はさらに悪化しました。

質問: テキストの両端揃え出力の外観を (可能であれば簡単に) 改善するにはどうすればよいでしょうかtexttt(例: 行間の間隔を狭める、単語間の間隔を狭めるなどして、出力の行数を減らす)?

コードをコンパイルするとlualatex

ありがとう。

答え1

単語間のスペースが広く、等幅文字の配置がないため、等幅文字を揃えると見栄えが悪くなるため、可変幅のタイプライター フォントを使用することをお勧めします。

lvmtt では、デフォルトでハイフネーションと可変の単語間スペースが許可されるため、必要な設定が少なくなります。

\documentclass[12pt]{book}
\usepackage{microtype}
%\usepackage{ragged2e} 

\begin{document}
\thispagestyle{empty}

\begin{center}
\begin{LARGE}
\begin{minipage}{5.75in}\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
  \textbf{This is a minipage. The above line of commands justifies the
    text and centers the last line. Notice how "nice" it appears.}
\end{minipage}
\end{LARGE}
\end{center}

\vspace*{25pt}

\begin{center}
\begin{LARGE}
\begin{minipage}{5.75in}\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
    \fontfamily{lmvtt}\selectfont
    I
    have, with the two above lines of commands, been able to justify
    the tttext in this minipage, as well as center the last line of
    output. This is what I want; however, the appearance does not seem
    as "nice" as the content of the above minipage.
\end{minipage}
\end{LARGE}
\end{center}
\end{document}

今日は画像はありませんが出力はここを参照

答え2

上記のコメントに沿って、これは似ていますが異なるアプローチです。

プリアンブルで使用します\DeclareFontFamily{OT1}{cmtt}{\hyphenchar \font=1}。(font=1 ではハイフネーション時にダッシュを使用できます。font=-1 ではハイフネーションは許可されますがダッシュは使用されません。)

また、フォントサイズを小さくすると見た目も良くなるはずです。

さて、コード

\documentclass[12pt]{book}
\usepackage{microtype}
\DeclareFontFamily{OT1}{cmtt}{\hyphenchar \font=1}

\begin{document}
\thispagestyle{empty}

\begin{center}
\begin{LARGE}
\begin{minipage}{5.75in}\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
\textbf{This is a minipage. The above line of commands justifies the text and centers the last line. Notice how "nice" it appears.}
\end{minipage}
\end{LARGE}
\end{center}

\vspace*{25pt}

\begin{center}
\begin{large}
\begin{minipage}{5.75in}\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
\spaceskip1em plus 1em \xspaceskip 1em plus 2em\relax
\texttt{This is a minipage. The above line of commands justifies the text and centers the last line. Notice how "nice" it appears.}
\end{minipage}
\end{large}
\end{center}

\vspace*{25pt}

\begin{center}
\begin{large}
\begin{minipage}{5.75in}\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
\spaceskip1em plus 1em \xspaceskip 1em plus 2em\relax
\texttt{This is a minipage. Supercalifragilisticexpialidocious Supercalifragilisticexpialidocious Supercalifragilisticexpialidocious Supercalifragilisticexpialidocious Supercalifragilisticexpialidocious}
\end{minipage}
\end{large}
\end{center}
\end{document}

出力は次のようになります:

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

2番目の段落にはハイフンでつなぐべきものがなかったので、コードがするハイフネーションを生成します。

答え3

そのようなものを改善するのは難しいです。等幅フォントは通常、単語間の伸縮性がありません。これは、行をまたいで文字を揃えたいテキストを再現するために主に使用され、これを実現するために単語間のスペースが文字と同じ幅に固定されているからです。

このようなフォントは通常、 を無効にすることでハイフネーションを許可しません\hyphenchar。 とpdflatexOT1 エンコーディングのデフォルトフォントを使用すると、ot1cmtt.fd次のように表示されます。

\ProvidesFile{ot1cmtt.fd}
        [2019/12/16 v2.5j Standard LaTeX font definitions]
\DeclareFontFamily{OT1}{cmtt}{\hyphenchar \font\m@ne}
\DeclareFontShape{OT1}{cmtt}{m}{n}
     {%
      <5><6><7><8>cmtt8<9>cmtt9%
      <10><10.95>cmtt10%
      <12><14.4><17.28><20.74><24.88>cmtt12%
      }{}
[...]

および\m@neは を表します-1。ほとんどのフォントには宣言がありません。\hyphencharその場合、TeX は\defaulthyphenchar値 45 (ハイフンのフォント スロット) を保持する を使用するためです。代わりに単語間スペースの値はファイルに保存され.tfm、適切なパラメータを操作することで変更できます\fontdimen

代わりにlualatextulmtt.fdが使用されます:

\ProvidesFile{tulmtt.fd}
        [2021/07/23 v1.8a Standard LaTeX font definitions for Latin Modern]
\DeclareFontFamily{TU}{lmtt}{%
\hyphenchar \font\m@ne
\fontdimen3\font\z@
\fontdimen4\font\z@
}
[...]

そのため、単語間のスペースの伸縮性は強制的に削除されます。

ただし、フォント パラメータへの割り当ては本質的にグローバルであることを覚えておく必要があります。等幅フォントを逐語的資料やリストにも使用する予定の場合は、 の最後でそれらの割り当てを元に戻す必要がありますminipage

そうではないと思います。

\documentclass[12pt]{book}
\usepackage{microtype}

\newenvironment{monospacedquote}{%
  \begin{center}
  \begin{minipage}{5.75in}
  \LARGE
  \ttfamily
  \ifnum\hyphenchar\font=-1
    \hyphenchar\font=`-
    \fontdimen3\font=0.5\fontdimen2\font
    \fontdimen4\font=0.3\fontdimen2\font
    \fontdimen7\font=0.2\fontdimen2\font
  \fi
  \selectfont
  \leftskip=\fill
  \rightskip=-\leftskip
  \parfillskip=\stretch{2}%
}{\end{minipage}\end{center}}

\begin{document}

\begin{monospacedquote}
This is a minipage. The above line of commands justifies the text 
and centers the last line. Notice how awful it appears.
\end{monospacedquote}

\end{document}

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

答え4

問題は、 を設定して単語間に大きなスペースを設定したことです\spaceskip。質問を言い換えると、「TeX プリミティブ を使用して大きなスペースを設定しましたが\spaceskip、なぜ大きなスペースがあるのですか?」となります。

それまでにスペースの数を少なく設定すると、\spaceskip望ましい結果が得られます。さらに、フォントにハイフン文字を設定しましたが\tt、これは他の回答でも言及されています。

\documentclass[12pt]{book}
\usepackage{microtype}

\begin{document}
\thispagestyle{empty}

\begin{center}
\begin{LARGE}
\begin{minipage}{5.75in}\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
\textbf{This is a minipage. The above line of commands justifies the text and centers the last line. Notice how "nice" it appears.}
\end{minipage}
\end{LARGE}
\end{center}

\vspace*{25pt}

\begin{center}
\begin{LARGE}
\begin{minipage}{5.75in}\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
%\spaceskip1em plus 1em \xspaceskip 1em plus 2em\relax %% ??? why this?
\spaceskip.3em plus .2em minus .1em \xspaceskip=\spaceskip {\tt\hyphenchar\font=`-}%
\texttt{I have, with the two above lines of commands, been able to justify the tttext in this minipage, as well as center the last line of output. This is what I want; however, the appearance does not seem as "nice" as the content of the above minipage.}
\end{minipage}
\end{LARGE}
\end{center}
\end{document}

関連情報