Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 12 down vote accepted

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
share|improve this answer
2  
+1 every day a new command ;-) –  Marco Daniel Apr 9 '13 at 13:40
    
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 '13 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? –  Timothy Li Apr 9 '13 at 13:46
    
@JosephWright doesn't everyone? (afterpage also:-) –  David Carlisle Apr 9 '13 at 13:48
2  
And yet another non-math application that requires math mode. –  Ryan Reich Apr 9 '13 at 15:23
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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