考虑以下代码:
\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
。使用pdflatex
OT1 编码的默认字体,我们可以阅读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%
}{}
[...]
and\m@ne
代表-1
。大多数字体没有\hyphenchar
声明,因为在这种情况下,TeX 将使用\defaulthyphenchar
,它保存值 45(连字符的字体插槽)。单词间空格的值存储在文件中.tfm
,您可以通过操作适当的\fontdimen
参数来更改它们。
取而代之lualatex
的tulmtt.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}