使用不同字体格式化小部分文本时出现问题

使用不同字体格式化小部分文本时出现问题

我按照给出的解决方案进行操作这个问题以便更改我正在编写的文本的几个部分的字体。但是,文本没有对齐,并且超出了文本的设置宽度。我该如何解决这个问题?例如,请参阅以下代码:

\documentclass[10pt]{article}
\pdfoutput=1
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[english]{babel}

\newcommand*{\myfont}{\fontfamily{pcr}\slshape\footnotesize\selectfont}
\DeclareTextFontCommand{\textmyfont}{\myfont}

\begin{document}

One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. This is \textmyfont{just dummy text}. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked.

\begin{myfont}
Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.
\end{myfont}

\end{document}

答案1

使用一些想法在同一行中同时出现“underfull \hbox”和“overfull ”该怎么办?\hbox,您可以更改等宽字体选择的单词间拉伸/收缩:

在此处输入图片描述

\documentclass{article}

\usepackage{lmodern}

\newcommand*{\usemyfont}{\fontfamily{pcr}\slshape\footnotesize\selectfont}
\newenvironment{myfont}
  {\par\usemyfont% Sets new font
   \fontdimen3\font=0.15em% inter-word stretch
   \fontdimen4\font=0.15em% inter-word shrink
   \sloppy% May be necessary
  }
  {\par}% Forces appropriate baseline skip due to font change
\DeclareTextFontCommand{\textmyfont}{\usemyfont}

\begin{document}

One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible 
vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly 
domed and divided by arches into stiff sections. This is \textmyfont{just dummy text}. The bedding was hardly able 
to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the 
rest of him, waved about helplessly as he looked.

\begin{myfont}
Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. 
Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river 
named Duden flows by their place and supplies it with the necessary regelialia.
\end{myfont}

One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible 
vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly 
domed and divided by arches into stiff sections. This is \textmyfont{just dummy text}. The bedding was hardly able 
to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the 
rest of him, waved about helplessly as he looked.

\end{document}

请注意,此类调整并不完美,可能需要进行微调。因此,添加\sloppy有时会有所帮助,因为它会增加\emergencystretch

答案2

通常最好将等宽字体设置为左对齐(右对齐)

在此处输入图片描述

您还需要考虑较小的基线,确保段落在环境的末尾结束。

\documentclass[10pt]{article}
\pdfoutput=1
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[english]{babel}

\newcommand*{\myfont}{\fontfamily{pcr}\slshape\footnotesize\selectfont}
\DeclareTextFontCommand{\textmyfont}{\myfont}
\newenvironment{myfontp}{\begin{flushleft}\myfont}{\end{flushleft}}

\begin{document}

One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. This is \textmyfont{just dummy text}. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked.

\begin{myfontp}
Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.
\end{myfontp}

\end{document}

相关内容