1

This has the second cell span over three lines:

\documentclass[12pt,landscape,a4paper]{article}
\usepackage{microtype}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\begin{longtable}{p{2.1cm}p{3.4cm}}
\centering do not hyphen this & \centering please do hyphenate this longer one
\end{longtable}
\end{document}

Three lines

This does not:

\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}

\begin{longtable}{p{2.1cm}p{3.4cm}}
\centering do not hyphen this & please do hyphenate this longer one
\end{longtable}
\end{document}

Two lines

Can I somehow make LaTeX figure on its own that I prefer the text to be fitted to two lines and only if this is possible, then centre it?I actually need it for a table (that I generate programatically, otherwise I could just add and remove \centering as needed).

  • The both example -- except of presence multirow package in preamble -- are the same. Both gives equal result: texts are (only) in two lines. – Zarko Jun 25 '15 at 18:53
  • No, they don't, see the picture. At least on my system. I use pdflatex. – sup Jun 25 '15 at 22:37
  • I test your examples (I have recent 64-bit MikTeX, if this matter) with pdfLaTeX, but with commented babel (since texts are in English). Result was as I say: only two lines of text without hyphenation. Anyway, meanwhile @HeikoOberdiek provide an answer, which frankly say, I do not understand well (I'm not familiar with microtype). – Zarko Jun 25 '15 at 23:10
  • Hm, Babel loads hyphenation patterns, maybe that is why it did not work for you? But as you say, it probably does not matter (even though the answer by @HeikoOberdiek does not really adress my issue, even though it improves this MWE. – sup Jun 25 '15 at 23:30
  • No, I now find cause of difference: I also delete \centering˙in second column. When I return it, the text in second column is in three lines, but not hyphenated (probably consequence of commented babel). And by the way, move \centering' to column options: {>{\centering}p{2.6cm} >{\centering}p{2.6cm}. For this you need to add package array. Sorry for noise about different result at my place... – Zarko Jun 25 '15 at 23:37
2

A. Package microtype

Especially if pdfTeX is used in PDF mode (pdflatex), the package adds some flexibility (character protrusion, font expansion). Especially the font expansion is quite useful, because it allows that the letters can be stretched or shrunk a little bit to get better line breaks.

The following example shows, that the entry in the second column consists of two lines, even without the need for hyphenation:

\documentclass[12pt,landscape,a4paper]{article}
\usepackage{microtype} 
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\begin{longtable}{p{2.6cm}p{2.6cm}}
 \centering do not hyphen this & \centering do hyphen this longer one
\end{longtable}
\end{document}

Result

B. Increasing \linepenalty

The default value of \linepenalty is ten. If the value is increased, then line breaks are more penalized and TeX tries to find line breaks with less lines:

\documentclass[12pt,landscape,a4paper]{article}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\linepenalty=100
\begin{longtable}{p{2.6cm}p{2.6cm}}
 \centering do not hyphen this & \centering do hyphen this longer one
\end{longtable}
\end{document}

Result

C. Using \looseness for fewer lines

If \looseness is set to a negative value, then TeX tries to typeset the current paragraph with fewer lines as specified by the parameter. Here it succeeds and makes the same two lines as in the previous example:

\begin{document}
\begin{longtable}{p{2.6cm}p{2.6cm}}
 \centering do not hyphen this
  & \centering do hyphen this longer one\looseness=-1
\end{longtable}   
\end{document}

D. Difference

The difference between the two versions can be seen with \showlists. The second line is differently set.

First version with microtype, shows that the character widths are shrunk by two percent: \T1/lmr/m/n/12 (-20). The number in brackets has the unit &#x2030.

....\hbox(8.26648+4.35004)x73.97733, glue set - 0.17274
.....\glue(\leftskip) 0.0 plus 1.0fil
.....\T1/lmr/m/n/12 (-20) t
.....\T1/lmr/m/n/12 (-20) h
.....\T1/lmr/m/n/12 (-20) i
.....\T1/lmr/m/n/12 (-20) s
.....\glue 3.91661 plus 1.9583 minus 1.30554
.....\T1/lmr/m/n/12 (-20) l
.....\T1/lmr/m/n/12 (-20) o
.....\T1/lmr/m/n/12 (-20) n
.....\T1/lmr/m/n/12 (-20) g
.....\T1/lmr/m/n/12 (-20) e
.....\T1/lmr/m/n/12 (-20) r
.....\glue 3.91661 plus 1.9583 minus 1.30554
.....\T1/lmr/m/n/12 (-20) o
.....\T1/lmr/m/n/12 (-20) n
.....\T1/lmr/m/n/12 (-20) e

In the other cases (\linepenalty or \looseness), the inter-word spaces are stronger shrunk (glue set - 0.69324):

....\hbox(8.26648+4.35004)x73.97733, glue set - 0.69324
.....\glue(\leftskip) 0.0 plus 1.0fil
.....\T1/lmr/m/n/12 t
.....\T1/lmr/m/n/12 h
.....\T1/lmr/m/n/12 i
.....\T1/lmr/m/n/12 s
.....\glue 3.91661 plus 1.9583 minus 1.30554
.....\T1/lmr/m/n/12 l
.....\T1/lmr/m/n/12 o
.....\T1/lmr/m/n/12 n
.....\T1/lmr/m/n/12 g
.....\T1/lmr/m/n/12 e
.....\T1/lmr/m/n/12 r
.....\glue 3.91661 plus 1.9583 minus 1.30554
.....\T1/lmr/m/n/12 o
.....\T1/lmr/m/n/12 n
.....\T1/lmr/m/n/12 e

E. Summary

The three methods can be combined:

\documentclass[12pt,landscape,a4paper]{article}
\usepackage{microtype}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\begin{longtable}{p{2.6cm}p{2.6cm}}
 \centering do not hyphen this &
 \centering
  \linepenalty=100
  \looseness=-1\relax
  do hyphen this longer one
\end{longtable}
\end{document}

F. \Centering instead of \centering

When \centering is used, the hyphenation is in practice disabled because of the infinite stretchability at the left and right side of the lines. Package ragged2e provides variants of \centering and friends with Camel-case names, witch limit the infinite stretchability to smooth the raggedness of the paragraphs. The package sets \CenteringLeftskip and \CenteringRightskip to 0pt plus 2em instead of 0pt plus 1fil. Smaller values than 2em could be tried in narrow columns, but it increases the risk of underfull \hbox warnings and more hyphenation than necessary.

In this case, however, this method makes less ragged lines, but three of them:

\documentclass[12pt,landscape,a4paper]{article}
\usepackage{microtype}
\usepackage{ragged2e}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern} 
\usepackage{longtable}  
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\begin{longtable}{p{2.6cm}p{2.6cm}}
 \centering do not hyphen this &
 \setlength{\CenteringLeftskip}{0pt plus 1em}%
 \setlength{\CenteringRightskip}{0pt plus 1em}%
 \Centering
  do hyphen this longer one
\end{longtable}
\end{document} 

Result

  • That is very interesting and microtype does seem to help, but it does not entirely answer my question (I edited it to make it cleare, I hope). I want latex to: 1. Try to fit the text in two lines, using hyphenation if neccessary 2. Center the text on the two lines. – sup Jun 25 '15 at 23:33
  • @sup 1.is more or less the normal behavior of TeX. There are parameters, which influence this behavior, two of them, \linepenalty and \looseness are presented in the middle sections of my answer. 2. is already answered in the question, \centering does this job. – Heiko Oberdiek Jun 25 '15 at 23:54
  • Yes, I must have misunderstood your very detailed answer. Actually, I found the F. option to work the best, but thanks for all the options anyway! – sup Jun 26 '15 at 12:09
1

If correct understand your question, then the package `ragged2e gives results close to what you looking for:

enter image description here

\documentclass[11pt,a4paper]{article}
    \usepackage{microtype}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    \usepackage[utf8]{inputenc}
    \usepackage[english]{babel}% option "czech" is not relevant ...
    \usepackage{ragged2e,array,longtable}%<-- added ragged2e and array

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{longtable}% <-------------
    \setlength\PreviewBorder{1em}

    \begin{document}
\begin{longtable}{>{\centering\arraybackslash}p{24mm}%<-- not allow hyphenation
                  >{\Centering\arraybackslash}p{24mm}}%<--- allow hyphenation
do not hyphen this  & please do hyphenate this longer one    
\end{longtable}
    \end{document}

The code as well comments in it gives basic explanation.

  • Yes, exactly, I have just commented to the asnwer by @Heiko Oberdiek in that sense who included this option at the end of his answer. I also just accepted his answer. I am at a loss as to which answer accept, since both are correct, yours is more to the point, his has lots of other options. I at least upvoted yours. – sup Jun 26 '15 at 12:12

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.