3

I've a problem with parbox spacing in combination with hyphenation. What I like to achieve is setting parboxes in a tabular-like way as key value pairs. So I have two parboxes with a given width per line. The first problem was the hyphenation of the first word in parbox, that was solved by using \hspace{0pt}. Now, when I have multiple lines, each with a key and a value parbox, there's no vertical spacing between.

without strut

So I tried to overcome this by using a \strut. Now, I have the vertical spacing, but the hyphenation of the first word does no longer work.

with strut

Please see the following minimal working example:

\documentclass[10pt,a4paper]{article}

\newcommand{\myKey}{MyLongKeyLabel}
\newcommand{\myValue}{ThisIsMyLongValue}

\newcommand{\myKeyValuePair}{%
    \parbox[t]{20mm}{\raggedright%
        \hspace{0pt}\myKey%\strut%
    }%
    \hspace{3mm}%
    \parbox[t]{20mm}{\raggedright%
        \hspace{0pt}\myValue%\strut%
    }\par
}

\begin{document}

    \myKeyValuePair
    \myKeyValuePair
    \myKeyValuePair

\end{document}

When uncommenting the \strut commands hyphenation will no longer work.

My question is: How can I achieve hyphenation of the first word in a parbox AND have vertical spacing between parboxes in consecutive lines?

Thanks in advance for any ideas.

  • the default hyphenation is not optimal. suggestion: add to the preamble the line \hyphenation{My-Long-Key-Label} so that the breaks come only at "logical" places. – barbara beeton Mar 9 '15 at 13:16
  • This is not possible in my case, because the data is generated from a PHP script, but thanks. – nis Mar 10 '15 at 11:44
4

The word potential to hyphenation in TeX is a sequence of letters (nonzero \lccode) started by glue (i.e. \hskip or inter-word glue) and ended by something different than box or line. The algorithm is more complicated but the sentence mentioned above is substantial. And \strut is box or line. This negates the hyphenation. You need to use this code:

    \strut\hskip0pt\relax\myKey\nobreak\hskip0pt\strut

and

    \strut\hskip0pt\relax\myValue\nobreak\hskip0pt\strut

Now, the word is started by \hskip and it is eded by \hskip too. Both \struts (before and after) are significant because the height of the first strut is used and the depth of the second strut is used too.

  • Do you really need \nobreak after \myKey and \myValue? – SJU Nov 26 '16 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.