34

When TeX produces Overfull box, is puts something like this in the log:

Overfull \hbox (15.47195pt too wide) in paragraph at lines 7--9
[]\OT1/cmr/m/n/10 I re-ally re-ally want to get dis-played the op-tions for 
hy-phen-ation xxxxxxxxxxxxxx |

I would like a macro \showhyphens that would for an entry like \showhyphens{hyphenation} display its hyphenation options, i.e. hy-phen-ation. This should be displayed either in the document (preferred) or in the log.

(Background: I need this for sumbitting a paper that contains many multi-part words and similar stuff like $(2,3)$-representation etc. I know that there are ways to modify - with \discretionary to make the word breakable, but it makes the code extremly unreadable and I'm not allowed to make this a macro definition.)

1
  • Some old TUGboat articles did it in a paragraph, I guess with Plain. Are those macros available somewhere?
    – jarnowicz
    Commented Jul 14, 2017 at 19:27

4 Answers 4

40

Bing! Your wish has been granted.

Seriously: \showhyphens already exists under that exact same name.

8
  • Unbelievable! Still, I'm curious if someone has a solution that avoids the need to check the log file.
    – yo'
    Commented May 10, 2012 at 15:02
  • 16
    @tohecz -- I tend to use the \showhyphens tool interactively (using the steps listed in the TeXBook): First, type tex (not latex) in an open command window, and you'll get a ** prompt; at the ** prompt type \relax, and you'll get a * (single-asterisk) prompt. At the * prompt, type \showhyphens{hyphenation} (or whatever words you're interested in) and watch TeX's output. Whey you're done, type \bye to exit TeX. :-) On your second observation: I'm not aware of a variant of the \showhyphens command that does not write its output to the log file.
    – Mico
    Commented May 10, 2012 at 15:28
  • @Mico sounds great! That is the solution I wanted. (I did not think it's TeX command not LaTeX and even though I'm aware of the interactive mode, this usage of it is hilarious!)
    – yo'
    Commented May 10, 2012 at 15:45
  • Note that \showhyphens won't work with XeLaTeX if the current font is an OpenType one. The package xltxtra redefines the command, but the words must be separated by commas, rather than spaces.
    – egreg
    Commented May 10, 2012 at 16:08
  • 2
    @Mico How do you tell tex the language?
    – cfr
    Commented Dec 16, 2014 at 3:13
30

Ok, here is the answer for printing the hyphens:

\documentclass[12pt,a4paper]{article}
\def\rehbox{{\unskip\unpenalty\setbox0\lastbox\ifhbox0 \rehbox
    \hbox{\unhbox0} \else \leavevmode \fi}}
\newcommand\printhyphens[1]{%
  \setbox0\vbox{{%
      \pretolerance-1\hsize=0pt\hfuzz=\maxdimen
      \noindent\hspace*{0pt}#1\par\rehbox}\par}%
  \unvbox 0
}

\begin{document}
\printhyphens{If you cannot reconsider this proposal, you are
  incompatible, my dearest friend.}
\end{document}

As the first version is limited by its usage of the save stack, here is a version that isn't. However, as stated in the comments, it is O(n^3), so it will become inefficient for large arguments rather fast, and the whole point of not using the save stack is to be able to use large arguments. So it would really make more sense to first split the input into words or lines and process them one by one, using a call of the first version of \printhyphens.

\documentclass[12pt,a4paper]{article}
\def\rehbox{\unskip\unpenalty\setbox2\lastbox\ifhbox2
  \setbox0\hbox{\hbox{\unhbox2} \unhbox0}\expandafter\rehbox\fi}
\newcommand\printhyphens[1]{%
  \setbox0\vbox{{\setbox0\hbox{}%
      \pretolerance-1\hsize=0pt\hfuzz=\maxdimen
      \noindent\hspace*{0pt}#1\par\rehbox\unhbox0}\par}%
  \unvbox 0
}

\begin{document}
\printhyphens{If you cannot reconsider this proposal, you are
  incompatible, my dearest friend.}
\end{document}
4
  • This is a great little program! However, if there are more than about 140 words in the argument of the \printhyphen statement, the program crashes with a "TeX capacity exceeded, sorry [grouping levels = 255]" message. Is there a way to make the \rehbox statement non-recursive?
    – Mico
    Commented May 10, 2012 at 20:14
  • 1
    Not without a complete rewrite with a huge loss of performance (instead of storing the boxes on the save stack, about O(n), you would have to append them to a global box, O(n^3)). It would likely make more sense to feed it one word at a time. Which reminds me: you might need \hspace*{0pt} after \noindent to have the first word hyphenated as well.
    – user9588
    Commented May 11, 2012 at 12:27
  • Thanks, David. I suspected there was a very good reason you'd implemented the program the way you did!
    – Mico
    Commented May 11, 2012 at 13:19
  • The documentation (version 2.19) of the soul package contains yet another method in section 7.3: mirror.unl.edu/ctan/macros/latex/contrib/soul/soul.pdf Commented Feb 19, 2013 at 11:07
26

With LuaTeX you have two options: \usepackage{lua-visual-debug} gives a lot of information whereas \usepackage{showhyphens} marks only all possible hyphenation points.

See http://texdoc.net/texmf-dist/doc/lualatex/showhyphens/showhyphens-doc.pdf

enter image description here

0
7

Because I know that OP (yo) understand Czech then I can refer the book "TeXbook naruby" (full text available on the internet here), page 225 below and page 226. The macro \showhyphenpar is presented here. The paragraph after \showhyphenpar is printed with all words shown as hy-phen-ated.

\def\showhyphenpar{\bgroup  
   \def\par{\setparams\endgraf\composelines}
   \setbox0=\vbox\bgroup \noindent\hskip0pt\relax}
\def\setparams{\rightskip=0pt plus1fil
   \linepenalty=1000 \pretolerance=-1 \hyphenpenalty=-10000}
\def\composelines{\setbox2=\hbox{}%
   \loop \setbox0=\lastbox \unskip \unpenalty
   \ifhbox0 % je už \vbox vyprázdněn?
     \global\setbox2=\hbox{\unhbox0\unskip\unhbox2}
   \repeat
   \egroup % vyprázdněný \vbox ukončím
   \exhyphenpenalty=10000 % \box2 vyšoupnu do vnějšího seznamu
   \emergencystretch=4em \leavevmode\unhbox2 \endgraf\egroup}

The principles of this macro is described in the TeXbook naruby.

1
  • Díky.
    – yo'
    Commented Mar 9, 2015 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .