3

The objective of the following code is to occupy the rest of a line with a parbox. I know this is a wrong way. So, how to get length from glue?

\documentclass{article}
\usepackage{geometry}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
\geometry{showframe}
\begin{document}
A B C D\parbox{\hfill}{Fill the rest length of this line with a parbox}
\end{document}
4
  • you need to use \pdfsavepos to write the position to the aux file then use the length on the next run May 1, 2022 at 8:30
  • Thank you @David Carlisle. Would you give an answer to show more details?
    – lyl
    May 1, 2022 at 8:58
  • I'm just on a phone someone will answer or search for pdfsvepos or tikzmark you will probably find a duplicate question May 1, 2022 at 9:00
  • @David Carlisle Would you please give an example for my post using pdfsavepos?
    – lyl
    Sep 17, 2022 at 13:48

2 Answers 2

2

You can record a position using \pdfsavepos and use it on a later run:

enter image description here

\documentclass{article}
\usepackage{geometry}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
\geometry{showframe}
\def\tx{Fill the rest length of this line with a parbox Fill the rest length of this line with a parbox
Fill the rest length of this line with a parbox Fill the rest length of this line with a parbox
Fill the rest length of this line with a parbox}
\makeatletter
\newcommand\currentpos[1]{%
  \ifcsname length#1\endcsname\else
  \expandafter\gdef\csname length#1\endcsname{0sp}%
  \fi
\pdfsavepos
\write\@auxout{%
  \gdef\expandafter\string\csname length#1\endcsname{\the\pdflastxpos sp}}%
  }
\makeatother
\begin{document}

\noindent X\dotfill X

A B C D \currentpos{a}\parbox[t]{\dimexpr\textwidth-\lengtha+1cm}{\tx}

A B C D E F \currentpos{b}\parbox[t]{\dimexpr\textwidth-\lengthb+1cm}{\tx}


A B C D E F G H \currentpos{c}\parbox[t]{\dimexpr\textwidth-\lengthc+1cm}{\tx}
\end{document}
2
  • Many thanks!! It seems that pdfsavepos does not come from a package. Where is it from? How can I find more explanation about this macro?
    – lyl
    Sep 17, 2022 at 14:39
  • 1
    @lyl texdoc pdftex it is not macro, it is a pdftex primitive (copied to other engines, but usually as \savepos) Sep 17, 2022 at 15:36
6

As long as one only needs to know the horizontal position on a page, the tabto package can suffice.

\documentclass{article}
\usepackage{geometry,tabto}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
\geometry{showframe}
\newlength\tolinened
\begin{document}
A B C D%
\tabto{\linewidth}%
\setlength\tolinened{\dimexpr\linewidth-\TabPrevPos}%
\tabto*{\TabPrevPos}%
\parbox[t]{\tolinened}{Fill the rest length of this line with a parbox
  Fill the rest length of this line with a parbox
  Fill the rest length of this line with a parbox
  Fill the rest length of this line with a parbox
  Fill the rest length of this line with a parbox
  Fill the rest length of this line with a parbox
}
\end{document}

enter image description here

You must log in to answer this question.

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