# Smart way to center verse

The documentation of the verse package provides the following recommendation for beginning a block of poetry to be typeset:

\settowidth{\versewidth}{This is the average line,}
\begin{verse}[\versewidth]


The relevance of the width option is that it enables the verse environment to produce a block that is left aligned internally, placed in the center of text area, and mostly filled left-to-right. Verse must not be full justified, yet preferably has balanced space between the text and either margin.

Poems that have already been completed by a writer can be handed to a typesetter who can choose an "average line" for each poem and use it to calculate the width. But if the LaTeX is automatically generated from a textual source with more limiting formatting features, this option is not available.

Ideally, this work can be automated through LaTeX measuring the width of every line, and then averaging, or using some other simple statistical assessment. I am wondering how such an operation can be handled by a LaTeX command sequence.

Having been asked for an example, I am providing the below minimal document based on wrapping an example from the verse documentation into a trivial article document. As seen, there little distinctive to consider, except the question of whether it is possible to achieve a similar effect absent the manual reproduction of a line from the poem, in this case the first, into the \settowidth call.

\documentclass{article}

\usepackage{verse}

\begin{document}

\poemtitle{Fleas}
\settowidth{\versewidth}{What a funny thing is a flea}
\begin{verse}[\versewidth]
What a funny thing is a flea. \\
You can’t tell a he from a she. \\
But he can. And she can. \\
Whoopee! \\
\end{verse}

\end{document}

• Please post a compilable example to play with. – cfr Oct 11 at 20:13
• @cfr Sure, but not much to see. – epl Oct 11 at 20:53
• It means people don't have to type as much to play and maybe answer your question, so you're more likely to get people playing and maybe answering. (Typing is boring; puzzles are fun.) – cfr Oct 11 at 21:03
• Well, now it's waiting for you to solve it. – epl Oct 12 at 11:35
• I guess that it could be most easily done with luatex and lua programming. However there are aesthetics involved --- what might look centered to an algorithm will not necessarily look centered to the reader. My description of the \versewidth macro was to indicate a potentially useful way of setting it; it does not necessarily produce an accurate result but hopefully one that is near enough. One can always fiddle with the source as a final step to get a pleasing result. – Peter Wilson Oct 12 at 18:20

I'm going to propose my comment as an answer together with some additional material.

I guess that it could be most easily done with luatex and lua programming. However there are aesthetics involved --- what might look centered to an algorithm will not necessarily look centered to the reader. My description of the \versewidth macro was to indicate a potentially useful way of setting it; it does not necessarily produce an accurate result but hopefully one that is near enough. One can always fiddle with the source as a final step to get a pleasing result.

Below I have provided some code that explores various possibilities in automatically centering the verse.

% verseprob4.tex SE 511886 Smart way to center verse

\documentclass{article}
\usepackage{verse}
\begin{document}

\newcommand{\myverse}{
What a funny thing is a flea. \\
You can’t tell a he from a she. \\
But he can. And she can. \\
Whoopee! \\
}

Per the MWE and the manual.
\settowidth{\versewidth}{What a funny thing is a flea}
versewidth = \the\versewidth

\poemtitle{Fleas}
\begin{verse}[\versewidth]
What a funny thing is a flea. \\
You can’t tell a he from a she. \\
But he can. And she can. \\
Whoopee! \\
\end{verse}

% Average of the longest and shortest lines.
\newlength{\lline} % long line
\newlength{\sline} % short line

\settowidth{\lline}{You can't tell a he from a she.}
\settowidth{\sline}{Whoopee!}
\setlength{\lline}{0.5\lline} % average of longest and shortest line

lline = \the\lline (average of longest and shortest line)
\begin{verse}[\lline]
\myverse
\end{verse}

% total width of verse lines
\settowidth{\lline}{\myverse}
lline = \the\lline (total width of verse lines)

\begin{verse}[\lline]
\myverse
\end{verse}

Set at 1/4 of total width of the 4 verse lines
\begin{verse}[0.25\lline]
\myverse
\end{verse}

\settowidth{\lline}{\mbox{\myverse}}
lline = \the\lline (width of boxed verse lines)

\begin{verse}[\lline]
\myverse
\end{verse}

Set with a versewidth of 200pt
\begin{verse}[200pt]
\myverse
\end{verse}
\end{document}


The results are shown below.

I think that the bottom line is that you have to decide what looks right; I don't think that an algorithm will do it for you. What if some lines are indented? What if some lines are turned over? What if ...?

EDIT

In my estimation the sequence:

\newcommand{\myverse}{
< the verse text, all N lines >
}

\settowidth{\versewidth}{\myverse}
% divide \versewidth by number of lines for average line width
\setlength{\versewidth}{<1/N \versewidth>}
\begin{verse}[\versewidth]
\myverse
\end{verse}


Will produce a verse centered about the average line width. This could be later tweaked to give a visually centered verse.

If there are other multiple verses then modify the above accordingly. If there are multiple poems then for each one \renewcommand{\myverse}{<text of next poem's verse>} and continue as before.

• Thank you. I agree that computers have yet effectively to compete with humans for generation of material with optimal visual appeal to the latter. I also agree that broad success relies on hinting in the input data about the relation of the lines (e.g. indenting, breaks). I have more optimism that solutions generally satisfactory in a large number of cases is possible through a higher but incomplete level of automation. Averaging, the trivial case, was meant only for study The base case, choosing a line just shorter than the longest, is not far beyond the bounds of algorithmic considerations. – epl Oct 14 at 16:06
• As you mention, the other consideration is limitations in TeX for expressing sophisticated computations, Lua greatly simplifying any possible implementation. The latter, however, being Turing-complete and supporting iteration through recursion, might be adequate for an implementation by an intrepid designer having experience far mightier than my own both in TeX and typesetting, and wishing to explore the matter further. Such a solution, I argue, could support, and should support, hints from the input text, about how to use the various lines, without being directly provided an answer. – epl Oct 14 at 16:14
• @epl Thank you for posting the results of my MWE. I have extended my answer indicating what I think is a reasonable solution. – Peter Wilson Oct 14 at 18:08
• In the example from the addendum, then, the value for N would be substituted by manual hard coding? – epl Oct 14 at 18:31
• By the way, if your system has ImageMagick tools installed in the command search path, as do most Unix-like environments, then you can generate a high-quality bitmap from your text PDF using the following command: mogrify -density 300 -alpha off -format png poem.pdf – epl Oct 14 at 21:47