如何改善小型頁面中對齊打字機文字的外觀

如何改善小型頁面中對齊打字機文字的外觀

考慮代碼:

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

產生

在此輸入影像描述

儘管第二個小型頁產生了我正在尋找的東西——tttext左右對齊,最後一行輸出居中——與第一個小型頁的合理輸出相比,外觀似乎顯得蒼白。

我想透過調用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}

產生輸出:

在此輸入影像描述

由於第二段中沒有任何內容可以連字符,因此我添加了第三段來說明代碼產生連字符。

答案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 Primitive 設定了大空格\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}

相關內容