3

The left part of the attached figure showes the nomal typeset of \parbox. The first/second lines are fully filled with words, and the rest words are bent to the third line. What interests me is: how to justify words of the last line autometically as shown on the right part of the attatched figure? Briefly speaking, add a option arg [s] to \parbox just like the \makebox does.

\documentclass{article}
%\usepackage{...}
\begin{document}

\parbox[s]{15em}{How to spread align the rest words which are not enough to fill the width of parbox?}% I just want the option arg of \parbox in this example works like \makebox[s] does.

\end{document}

`

lyl
  • 2,727

1 Answers1

6

There is nothing special about \parbox here, you see the same paragraph justification in the main page, normally the last line is allowed to be short but if you want that space to be 0pt you can set

\setlength\parfillskip{0pt}

As this makes it much harder to get a reasonable paragraph setting you probably should also use \sloppy to allow other spaces to stretch more to compensate.

enter image description here

\documentclass{article}
%\usepackage{...}
\begin{document}

\parbox{15em}{%
\sloppy\setlength\parfillskip{0pt}
How to spread align the rest words which are not
  enough to fill the width of
  parbox?}

\end{document}
David Carlisle
  • 757,742
  • Sorry David, I don't quite understand your meaning. Would you please send a MWE illustrated how to get to the typeset I want? Thank you. – lyl Feb 17 '19 at 09:41
  • @lyl example added – David Carlisle Feb 17 '19 at 09:46
  • Great trick!! That's just what I nedd!! Many thanks! And where can I find descriptions about \sloppy and \parfillskip, because I don't konw how to use them? – lyl Feb 17 '19 at 09:52
  • @lyl \parfillskip is a tex primitive so the texbook or the free texbytopic, \sloppy should be in any basic latex tutorial, but offical documentation is the latex book, there are thousands of examples of both on this site – David Carlisle Feb 17 '19 at 09:54
  • After the use of \setlength\parfillskip{0pt} in \parbox, will it affect folloing paragraph ? – lyl Feb 17 '19 at 10:32
  • @lyl it's a local setting so will affect all the paragraphs in the parbox (unless you surround it with {..} but won't affect the paragraphs outside the box – David Carlisle Feb 17 '19 at 10:35
  • I construct my '\parboxa' by this: \newcommand{\parboxa}{\sloppy\setlength\parfillskip{0pt}\parbox}, but it does not work(has no effect of justify,e.g. \parboxa[t]{4em){this is an example}). Could you please have a look at this and kindly help me with a solution? – lyl Feb 18 '19 at 12:48
  • @lyl that definition would make all the following text after the parbox be set in this style. you want \newcommand{\parboxa}[2][c]\parbox[#1]{\sloppy\setlength\parfillskip{0pt}#2} – David Carlisle Feb 19 '19 at 08:03
  • Thank you. you said "that definition would make all the following text after the parbox be set in this style", but why the text in parbox is not in this style? – lyl Feb 19 '19 at 12:33
  • \parbox normalises some settings (@parboxrestore) so for example if you have a parbox or a minipage in a list you get locally normal justified setting not the paragraph shape from the itemized list. so you need the settings inside the parbox (or change @parboxrestore) – David Carlisle Feb 19 '19 at 12:45
  • As I learnt, \parbox has also two other optional arguments(height and inner-align, not just two arguments, so how to redefine my \parboxa? – lyl Feb 20 '19 at 01:05
  • \sloppy\setlength\parfillskip{0pt} works all right except that when there is \\ in text, the line before \\ loses justify align,e.g. \parbox{5em}{\sloppy\setlength\parfillskip{0pt}M M\\M M M M}, the first line(M M) fails to justify align. Is there a better way to solve it? – lyl Feb 28 '19 at 02:22
  • @lyl, yes of course, \\ (which you should almost never use in text anyway even with standard settings and especially not in this context) explicitly means to force a line break leaving the line short. Simply do not use that. If you really need a forced line break use \linebreak or if you are ending the paragraph use a blank line. (What document type are you producing, using zero parfillskip and forced line breaks is very rare thing, and almost certainly to produce very ugly typesetting with weirdly spaced words trying to meet the constraints placed on the text???) – David Carlisle Feb 28 '19 at 07:37
  • It seems this not work in chapter title. How to make it work in chapter title? – Y. zeng Oct 13 '23 at 03:08