Text FormattingCalculate actual text width

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
theprofessor
Posts: 3
Joined: Wed Feb 16, 2011 7:36 pm

Calculate actual text width

Post by theprofessor »

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
Last edited by theprofessor on Fri Feb 18, 2011 2:05 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Calculate actual text width

Post by frabjous »

Please provide a minimal working example we can test with.
theprofessor
Posts: 3
Joined: Wed Feb 16, 2011 7:36 pm

Calculate actual text width

Post by theprofessor »

Here's my document:

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}
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.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Calculate actual text width

Post by frabjous »

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.

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}
I confess this doesn't feel very robust to me, however, so use with caution.
theprofessor
Posts: 3
Joined: Wed Feb 16, 2011 7:36 pm

Re: Calculate actual text width

Post by theprofessor »

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!
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Calculate actual text width

Post by localghost »

theprofessor wrote:[…] Seems to be working fine. […]
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).


Best regards and welcome to the board
Thorsten
Post Reply