Como melhorar a aparência do texto alinhado da máquina de escrever em uma minipágina

Como melhorar a aparência do texto alinhado da máquina de escrever em uma minipágina

Considere o código:

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

que produz

insira a descrição da imagem aqui

Embora a segunda minipágina produza o que estou procurando - tttext justificado à esquerda e à direita com a última linha de saída centralizada - a aparência parece pálida em comparação com a saída justificada da primeira minipágina.

Pensei em invocar o ragged2epacote; mas ao fazer isso, além de centralizar a última linha do texttttexto, piorou muito a situação.

PERGUNTA: Como posso (simplesmente, se possível) melhorar a aparência da textttsaída justificada do texto (por exemplo, reduzir o espaçamento entre as linhas, reduzir o espaçamento entre as palavras --- para reduzir o número de linhas de saída)?

Eu compilo o código comlualatex

Obrigado.

Responder1

Como você tem espaços elásticos entre palavras, não há alinhamento de caracteres monoespaçados; seria melhor usar uma fonte de máquina de escrever de largura variável, pois justificar o monoespaço sempre parecerá horrível.

lvmtt permite hifenização e espaço variável entre palavras por padrão, portanto, menos configurações são necessárias.

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

Nenhuma imagem hoje, masveja a saída aqui

Responder2

Seguindo as linhas dos comentários acima, esta é uma abordagem semelhante, mas diferente:

Use \DeclareFontFamily{OT1}{cmtt}{\hyphenchar \font=1}no preâmbulo. (O font=1 permite que um travessão seja usado ao hifenizar; font=-1 permite a hifenização, mas sem travessão.)

Além disso, diminuir o tamanho da fonte também deve melhorar a estética.

Agora, o código

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

produz a saída:

insira a descrição da imagem aqui

Como não havia nada para hifenizar no segundo parágrafo, coloquei um terceiro para ilustrar que o códigofazproduzir hifenização.

Responder3

É difícil melhorar algo assim. Fontes monoespaçadas geralmente não têm elasticidade e encolhimento entre palavras, pois são usadas principalmente para reproduzir texto onde o alinhamento de caracteres entre linhas é desejado e, para conseguir isso, o espaço entre palavras é definido para um comprimento rígido tão largo quanto os caracteres.

Essas fontes geralmente não permitem a hifenização, desativando o \hyphencharpara elas. Com pdflatexas fontes padrão na codificação OT1, podemos ler ot1cmtt.fdpara ver

\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%
      }{}
[...]

e \m@nesignifica -1. A maioria das fontes não terá \hyphenchardeclaração porque nesse caso o TeX usará o \defaulthyphenchar, que contém o valor 45 (o slot da fonte para o hífen). Os valores dos espaços entre palavras são armazenados no .tfmarquivo e você pode alterá-los agindo nos \fontdimenparâmetros apropriados.

Em lualatexvez disso, tulmtt.fdseria usado:

\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@
}
[...]

portanto, a elasticidade e a contração dos espaços entre palavras são removidas à força.

No entanto, você deve lembrar que as atribuições aos parâmetros de fonte são inerentemente globais: se você planeja usar a fonte monoespaçada também para material literal ou listagens, você precisará reverter essas atribuições no final do seu arquivo minipage.

Presumo que este não seja o caso.

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

insira a descrição da imagem aqui

Responder4

Seu problema é que você definiu grandes espaços entre as palavras definindo \spaceskip. Tento parafrasear sua pergunta: "Eu configurei grandes espaços usando TeX primitivo \spaceskip, por que tenho grandes espaços?"

Se você definir menos quantidade de espaços, \spaceskipobterá o resultado desejado. Além disso, defini hífen para \ttfonte, isso também é mencionado aqui em outras respostas.

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

informação relacionada