2

I have a custom monospace font. I want to write the whole document using it. The output from doing Lorem Ipsum shows that my monospaced font is not being rendered as monospaced (i.e. some character widths are squished, probably due to the line-rendering algorithms). What do I need to configure on LaTeX's end to make this work right? I am using XeLaTeX.

\documentclass[a4paper,12pt]{article}
\usepackage{fontspec}
\usepackage{fullpage}
\setmainfont{MyFont}
\begin{document}
Hello world hello world Hello world hello world Hello world hello world Hello world hello worldHello world hello world Hello world hello worldHello world hello worldHello world hello world Hello world hello world Hello world hello world Hello world hello world Hello world hello world.
\end{document}

I would like to customize how many lines exactly per page, and how many characters per line (since it is monospaced), or at least to set a font size and have the whole document be monospaced at that size.

Lance
  • 1,799

2 Answers2

6

Using fullpage will not give you the possibility to set the number of characters per line nor the number of lines per page. This can be done with geometry instead.

However, justification will make characters not align vertically, because it's usually impossible that lines contain the exact number of characters (including spaces). If you want vertical alignment, you need \raggedright and \frenchspacing.

In the example, I set 72 characters (maximum) per line and 48 lines per page.

\documentclass[a4paper,12pt]{article}
\usepackage{fontspec}
\usepackage{geometry}

\usepackage{kantlipsum}

\setmainfont{Latin Modern Mono}

\newlength{\characterwidth}
\settowidth{\characterwidth}{\normalfont x}
\geometry{textwidth=72\characterwidth,lines=48}
\AtBeginDocument{\raggedright\setlength{\parindent}{3\characterwidth}}
\frenchspacing

\begin{document}

\kant[1-4]

\end{document}

enter image description here

The first lines with a frame around the text block to show more clearly where the problem is:

enter image description here

As you see, the first two lines end at the margin, but the third one is two character short of 72; even with hyphenation on, the next word cannot fit, because the space would count for a character and the hyphen for another.

egreg
  • 1,121,712
  • How do I change the number of characters that fit on a single line? Changing the font size and then changing the textwidth doesn't always align to the available page space. – Lance Aug 21 '19 at 09:45
  • If I do this, monospace is no longer working :/ \fontsize{10pt}{12pt}\settowidth{\characterwidth}{\normalfont x}\geometry{margin=0.7in,textwidth=96\characterwidth,lines=48}. – Lance Aug 21 '19 at 09:48
  • @LancePollard Remove the 12pt option, if you want 10pt. – egreg Aug 21 '19 at 09:49
  • It errors without it, not sure how... – Lance Aug 21 '19 at 09:49
  • Your example isn't monospaced either it looks like... https://imgur.com/ugsynvZ Phenomena and paralogisms are not aligned at the na and ms. – Lance Aug 21 '19 at 09:52
  • @LancePollard Oh, sorry! I forgot \frenchspacing. – egreg Aug 21 '19 at 09:53
  • What is \frenchspacing?! So random :) Adding that doesn't work either, it changes it though! The ending letters here are not aligned https://imgur.com/ClWtIlj – Lance Aug 21 '19 at 09:54
  • @LancePollard By default, TeX uses increased spaces after punctuation. With \frenchspacing it doesn't. I edited the example and changed the pictures. Remember that in your font the interword space should be the same as the character width. – egreg Aug 21 '19 at 09:57
  • Ok great! My font is ever-so-slightly off however, any ideas? https://imgur.com/cSu6hWP It works fine in the browser as monospace with no settings not even pre/code. – Lance Aug 21 '19 at 10:02
  • @LancePollard Your font is not really monospaced, my crystal ball tells me. – egreg Aug 21 '19 at 10:06
  • Arg okay will have to explore that more then :) – Lance Aug 21 '19 at 10:18
0

Please, be more specific. At least with respect to spacing, flushleft avoids uneven spacing.

%!TEX program=xelatex
\documentclass[a4paper,12pt]{article}
\usepackage{fontspec}
\usepackage{fullpage}
\setmainfont{DejaVu Sans Mono}
\begin{document}
\begin{flushleft}
Hello world hello world Hello world hello world Hello world hello world Hello world hello world Hello world hello world Hello world hello worldHello world hello worldHello world hello world Hello world hello world Hello world hello world Hello world hello world Hello world hello world.
\end{flushleft}
\end{document}

enter image description here

Wrt lines per page, this may be worth your attention: How can I limit the maximum number of lines in a page?

  • flushleft doesn't work for me. – Lance Aug 21 '19 at 08:37
  • It's pretty clear what I mean, I want monospace spacing, but it's giving me non-monospace spacing. – Lance Aug 21 '19 at 08:37
  • It would be great if you shared a MWE: https://tex.meta.stackexchange.com/questions/228/ive-just-been-asked-to-write-a-minimal-example-what-is-that –  Aug 21 '19 at 08:40