2

I followed the solution given in this question in order to change the font for a few sections of the text I am writing. However, the text does not get justified and it exceeds the set width of the text. How can I fix this? See, e.g., the following code:

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

Using some ideas from What to do with both "underfull \hbox" and "overfull \hbox" in the same line?, you can change your interword stretch/shrink for the monospaced font selection:

enter image description here

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

Note that such adjustments are not perfect, and may require fine-tuning. As such, adding \sloppy sometimes helps as it increases \emergencystretch.

  • Thanks! It works well. The text inside \textmyfont{} sometimes exceeds the text width as well. Would it be possible to fix this too? – Valerio May 18 '17 at 0:33
  • 1
    @Valerio: You could add the \fontdimen3\font=0.15em \fontdimen4\font=0.15em commands after \selectfont within the definition of \usemyfont. – Werner May 18 '17 at 6:01
2

It is usually best to set monospace fonts flushleft (raggedright)

enter image description here

Also you need to account for the smaller baseline by ensuring a paragraph end at the end of the enviornment.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.