Hi all,
I have a raggedright section within a parbox. I want to calculate the length of the last line in this section. Unfortunately \settowidth doesn't work here. Any suggestions?
Cheers
Text Formatting ⇒ Calculate actual text width
-
- Posts: 3
- Joined: Wed Feb 16, 2011 7:36 pm
Calculate actual text width
Last edited by theprofessor on Fri Feb 18, 2011 2:05 am, edited 1 time in total.
NEW: TikZ book now 40% off at Amazon.com for a short time.

Calculate actual text width
Please provide a minimal working example we can test with.
-
- Posts: 3
- Joined: Wed Feb 16, 2011 7:36 pm
Calculate actual text width
Here's my document:
I want to measure the width of the last line in the parbox. The only command I know which comes close is \settowidth but in the above example, I can only get the width of the whole parbox, not the individual lines.
Code: Select all
\documentclass{article}
\newlength\mylength
\begin{document}
\parbox{50pt}{\raggedright The quick brown fox jumps over the lazy dog}
\settowidth{\mylength}{\raggedright The quick brown fox jumps over the lazy dog}
\the\mylength
\settowidth{\mylength}{\parbox{50pt}{\raggedright The quick brown fox jumps over the lazy dog}}
\the\mylength
\end{document}
Calculate actual text width
My only thought at the moment would be to try something using the \linegoal command from the linegoal package, which tells you how much room there is left on the line. That, measured at the end of the last line, subtracted from the total width, should give you the width of the last line.
I confess this doesn't feel very robust to me, however, so use with caution.
Code: Select all
\documentclass{article}
\usepackage{linegoal}
\newlength{\mylength}
\newlength{\myposition}
\begin{document}
\sloppy
\noindent\parbox{50pt}{\raggedright The quick brown fox jumps over the lazy dog.\global\setlength{\myposition}{\linegoal}}
\noindent\setlength{\mylength}{50pt}%
\addtolength{\mylength}{-1\myposition}%
\noindent 50pt minus \the\myposition\ is \the\mylength
\bigskip
\noindent\parbox{50pt}{\raggedright The quick brown fox jumps over the lazy puma.\global\setlength{\myposition}{\linegoal}}
\noindent\setlength{\mylength}{50pt}%
\addtolength{\mylength}{-1\myposition}%
\noindent 50pt minus \the\myposition\ is \the\mylength
\end{document}
-
- Posts: 3
- Joined: Wed Feb 16, 2011 7:36 pm
Re: Calculate actual text width
Thanks, I didn't know about the linegoal package. It does require two compilations before it gets the correct values. Seems to be working fine. Great!
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Calculate actual text width
If so, then please mark the topic (not the last post) accordingly as written in Section 3 of the Board Rules (to be read before posting).theprofessor wrote:[…] Seems to be working fine. […]
Best regards and welcome to the board
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10