# Log paragraphs with more than n hyphenations

The (large) body of text I have written has a few paragraphs with an large number of hyphenated words. My goal is to examine these paragraphs, with the aim of rewording them. Is there a way I can make TeX issue a warning or otherwise produce an entry in the log when it has used more than n hyphens in a paragraph?

I am using XeLaTeX with microtype and the Swiss 721 typeface.

Here is an example (I appreciate the tight leading makes it look worse)

MWE:

% XeLaTeX
\documentclass{article}
\usepackage{microtype}

\usepackage{fontspec}
\setmainfont{Swiss}[Path = D:/fonts/, UprightFont = Swiss721Light.ttf, BoldFont = Swiss721MdBT.ttf, ItalicFont= Swiss721Italic.ttf, BoldItalicFont = Swiss721BoldItalicX.ttf, FontFace={k}{n}{Swiss721black.ttf},  SmallCapsFeatures={Letters=SmallCaps}]
\setsansfont{Swiss}[Path = D:/fonts/, UprightFont = Swiss721Light.ttf, BoldFont = Swiss721MdBT.ttf, ItalicFont= Swiss721Italic.ttf, BoldItalicFont = Swiss721-BoldItalic-BT-WGL4.ttf, SmallCapsFeatures={Letters=SmallCaps}]

\usepackage{lipsum}

\begin{document}
\lipsum

\end{document}


The first paragraph has three hyphenations:

I appreciate that a judicious choice of \hyphenpenalty or \exhyphenpenalty \brokenpenalty \linepenalty \doublehyphendemerits

might be suitable, but I'd like to have some measure by which the choice of these penalties could be judged. And it may not solve all hyphenation problems (which ultimately have to be solved by rewording). As a last resort, I'd like to check the remainder.

• This is rarely an issue with pdfLaTeX as microtype essentially solves this problem (or makes it so rare to be acceptable). A supplementary question would be how to use this typeface in pdfLaTeX. – Hugh Jun 15 '16 at 5:00
• The total number of instances of discretionary hyphenations in a (long) paragraph seems like an odd criterion to consider. Assuming the default parameter settings apply, TeX's paragraph-building algorithm does a good job of avoiding instances of three lines that end in a (discretionary) hyphen. Could you explain the rationale of your use case in more depth? Alternatively, could you post an example of a long paragraph that contains too many discretionary hyphenation points? – Mico Jun 15 '16 at 5:18
• Removing unsightly hyphenations is the real goal. I said n hyphenations as I thought this might be the simplest thing to log. I don't mind if some things which get logged are acceptable. I've uploaded an image to illustrate an example I'd like logged. – Hugh Jun 15 '16 at 5:22
• In that case, you should raise (a) the penalty for each discretionary and (b) the penalty for lines with consecutive hyphenation points. It would be helpful if you stated whether or not you've modified these penalty parameters already and/or if you load a package that does so. – Mico Jun 15 '16 at 5:25
• I've added an edit. Essentially, changing the penalties is an option but not the full story. I'm open to a better proxy than 'number of hyphenations'. – Hugh Jun 15 '16 at 5:39

You can get this information from the log, I didn't have the font so I used latin modern but encouraged it to have some hyphens

 % XeLaTeX
\documentclass{article}
\usepackage{microtype}

\usepackage{fontspec}

\doublehyphendemerits=-10000000 \finalhyphendemerits=-10000000
\usepackage{lipsum}

\begin{document}
\tracingparagraphs1
\lipsum

\end{document}


logging of line breaking is enabled with

\tracingparagraphs1


so in the log every line that ends with a discretionary hyphen can be found by searching for line [0-9]+\.[[0-9]+- where the number before the . is the line number, the number after the . is a measure of how much the white space is stretched and the trailing - denote a hyphenation point.

So the first para is

which corresponds to the block of tracing after the first @secondpass and the lines

@@4: line 4.1- t=12931 -> @@3
tus


-

@@10: line 6.1- t=-9980588 -> @@8
lus


-

@@27: line 9.2- t=20249 -> @@22
tor


-

@@35: line 10.2- t=-9977082 -> @@27
san


You have to be careful as tex logs all feasible breakpoints not just ones taken, so you have to follow the chain of -> back from the end, here tex ends the paragraph choosing break point @@45 which is logged as

@@45: line 11.2- t=-19976982 -> @@35


which tells you that breakpint @@35 the accum-sam hyphenation was taken.

So... the log does tell you this but it might be simpler just to seach for - in the output eg

pdftotext -layout qq213.pdf

$grep -o -- '.......-$' qq213.txt
eu tel-
tur auc-
, accum-
m. Prae-
lam cur-
Vestibu-
a a fau-
Vestibu-
s eu la-
sto lec-
sed la-
as. Cur-
st. Cur-
fermen-
sed ul-
nec luc-
s, eges-


plus a bit of perl or whatever to split on blank lines.

• The pdftotext idea is definitely better than trying to parse the trace. But is there a way to preserve paragraphs with -layout? I looked at tutorialspoint.com/unix_commands/pdftotext.htm for options to pdftotext without much luck. Currently I count hyphens per page (which is not too bad), but if there is a function out there already that can show paragraph breaks, that would be ideal. – Hugh Jun 20 '16 at 4:51