[Tex/LaTex] Text justifying problem when using monospaced font

fontshorizontal alignment

I got text justifying problem when I change the default font to a monospaced font, as you can see in my MWE.

Note that this does not only happen in article class, but also in memoir, book, as well.

\documentclass[12pt]{article}
\usepackage{lipsum}
\usepackage{tgcursor}
\renewcommand*\familydefault{\ttdefault} 
\begin{document}
\lipsum
\end{document}

How can I fix it? Please help me.

Thanks

Best Answer

The problem here is, that monospaced fonts also have a fixed width space between words, so TeX cannot stretch these spaces. You have to adjust the interword space settings. The following solution is adapted from http://texblog.net/latex-archive/plaintex/full-justification-with-typewriter-font/

\documentclass[12pt]{article}
\usepackage{lipsum}
\usepackage{everysel}
\renewcommand*\familydefault{\ttdefault}
\EverySelectfont{%
\fontdimen2\font=0.4em% interword space
\fontdimen3\font=0.2em% interword stretch
\fontdimen4\font=0.1em% interword shrink
\fontdimen7\font=0.1em% extra space
\hyphenchar\font=`\-% to allow hyphenation
}
\usepackage{tgcursor}

\begin{document}
\selectfont % Force initial font feature selection
\lipsum
\end{document}

OUTPUT:

justify