15

I want to get the natural width of the last line in a paragraph. For example, the last line of the following paragraph

Georg Friedrich Bernhard Riemann (September 17, 1826 – July 20, 1866) was an influential German mathematician who made lasting contributions to analysis, number theory, and differential geometry, some of them enabling the later development of general relativity.

may be from some to relativity(that is, some of them enabling the later development of general relativity.) When the line-breaking happened, the last line will be put into an hbox, so, how can I get this hbox?

1 Answer 1

18

enter image description here

\documentclass{article}

\begin{document}

Georg Friedrich Bernhard Riemann (September 17, 1826 -- July 20, 1866) was an
influential German mathematician who made lasting contributions to analysis, number
theory, and differential geometry, some of them enabling the later development of
general relativity.
$$\mbox{the last line had length \the\predisplaysize}$$

\end{document}

Actually \predisplaysize is set to 2em more than the natural size so you may need to subtract 2em, depending..


To get the value without leaving a visible display math you can do something like this (copied from afterpage)

\makeatletter
Georg Friedrich Bernhard Riemann (September 17, 1826 -- July 20, 1866) was an influential German mathematician who made lasting contributions to analysis, number theory, and differential geometry, some of them enabling the later development of general relativity.
{\abovedisplayshortskip\z@\abovedisplayskip\z@
    \belowdisplayshortskip\z@\belowdisplayskip\z@
$$\global\dimen\@ne\predisplaysize
 \xdef\tmp{%
      \predisplaysize\the\predisplaysize
      \prevgraf\the\prevgraf\relax}%
$$\vskip\dimexpr-\parskip-\baselineskip\relax}\tmp

new para in which we can use \the\dimen\@ne


and another new para
7
  • I think breqn uses this approach: it only uses display math mode to grab info, and all of the actual setting is done in inline math mode.
    – Joseph Wright
    Apr 9, 2013 at 13:45
  • yeah, wonderful! I like this simple and elegant method :-) But if I don't want the last line $$\mbox ... ... can you show me another way?
    – yanpengl
    Apr 9, 2013 at 13:46
  • @JosephWright doesn't everyone? (afterpage also:-) Apr 9, 2013 at 13:48
  • @TimLi look at afterpage and breqn etc you need a $$ ..$$ to set up \predisplaysize but then you can globally save that to another length register finish up the display math leaving no visible trace if you locally set abovedisplayskip and friends to zero Apr 9, 2013 at 13:51
  • 3
    And yet another non-math application that requires math mode.
    – Ryan Reich
    Apr 9, 2013 at 15:23

You must log in to answer this question.

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