[Tex/LaTex] Defining a command for changing the line width or the right and left margins

formattinggeometry

I am trying define a multi-parameter command which would break up the text to be only 34 characters per line or I suppose another way to look at it would be to have a left margin of 2.7in (1in more than my default) and a right margin of 2.4in (1.3in more than my default).

Essentially what I want are 34 characters (in a mono-spaced font) per line via a command, is this possible?


This is the command that I have so far, which is non-functional, but it's the only thing I can think would work:

\newcommand{\di}[1]{\setlength{\textwidth}{34em}#1}

Here's a MWE

\documentclass[12pt]{letter}
\usepackage{fontspec}
\setmainfont{Courier New}
\setmonofont{Courier New}
\usepackage[width=8.50in, height=11.00in, left=1.70in, right=1.3in, top=0.50in, bottom=1.00in]{geometry}
\newcommand{\di}[1]{\setlength{\textwidth}{34em}#1}   

\begin{document}
Some regular text here...

\di{I want this text to only be 34 characters long}

\end{document}

Best Answer

The changepage package provides this function. The simplest way is to use the adjustwidth environment. You have to specify an amount by which to increase the size of the margins, which you can calculate based on the textwidth, your default margins, and the desired textwidth of the new narrow block.

One problem is defining that narrower textwidth based on characters: as you can see in the test section of the example below, 34 em is actually wider than your default textwidth. 34 ex is too narrow. So in the example I used \settowidth and just typed thirty-four characters. A non-hard-coded solution would be better.

A second problem is that most of the typewriter fonts do not allow justification (see What to do with both "underfull \hbox" and "overfull \hbox" in the same line?). With a narrow text block, many lines spill over the edge. This is not a problem with a proportional font, which I used for one of the images below (by commenting out the second line of code).

\documentclass[12pt,letterpaper]{article}
\renewcommand*\familydefault{\ttdefault}
\newlength{\Lmargin}
\newlength{\Rmargin}
\newlength{\NarrowBlock}
\newlength{\NarrowMargin}
\setlength{\Lmargin}{1.7in}
\setlength{\Rmargin}{1.3in}
\settowidth{\NarrowBlock}{123456789012345678901234567890123}
\usepackage[left=\Lmargin, right=\Rmargin, top=0.5in, bottom=1in]{geometry}
\setlength{\NarrowMargin}{\textwidth}
\addtolength{\NarrowMargin}{-\NarrowBlock}
\addtolength{\NarrowMargin}{-0.5\NarrowMargin}

\newlength{\Test}
\usepackage{lipsum}
\usepackage[strict]{changepage}

\begin{document}

\section{Lengths}

Textwidth: \the\textwidth; Lmargin: \the\Lmargin; Rmargin: \the\Rmargin

NarrowBlock: \the\NarrowBlock; NarrowMargin \the\NarrowMargin

Test at 34em \setlength{\Test}{34em} \the\Test;
Test at 34ex \setlength{\Test}{34ex} \the\Test

\section{Trial}

\lipsum[1]

\begin{adjustwidth}{\NarrowMargin}{\NarrowMargin}
1234567890123456789012345678901234

\lipsum[2]
\end{adjustwidth}

\lipsum[3]
\end{document}

rmdefault ttdefault

(FYI - I used a LaTeX default font command so these could be tested on any engine.)