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.

This is a follow up to my earlier question Bump right-aligned text to next line iff no room. The difference now is that I need to place this formatted text within a \savebox. In my actual use case, for performance reasons, I am caching the text that needs to follow existing text (i.e., on the same line), and this cached text contains some text that is to be right aligned to the edge of the page.

I attempted to use all four solutions at Bump right-aligned text to next line iff no room along with a simple \hfill. These all seems to work when used outside of the \savebox, but not when inside the \savebox:

enter image description here

Notes:

  • I can not use \parbox{\linewidth}{} as I don't know where on the line the \savebox will start. That is, there is text on the same line before the \usebox is applied.
  • The desired version would have the XXX aligned on the right as is the ZZZ text.

Code:

\documentclass{article}
\usepackage{showframe}

\newsavebox{\MyCachedResults}

% The following are from
%    http://tex.stackexchange.com/questions/91548/bump-right-aligned-text-to-next-line-iff-no-room

\newcommand*{\BumpCarlisle}[1]{% http://tex.stackexchange.com/a/91556/4301
    {\rightskip\fill\parfillskip-\rightskip
    \linepenalty100
    \exhyphenpenalty0
    \linebreak[0] % <-- Need space here
    %Space before \hspace* allows for a break before it
    \hspace*{\fill}#1}%
}

\newcommand*{\FillLineEgreg}[1]{% http://tex.stackexchange.com/a/91564/4301
    {%
        \nobreak\hfill\penalty50\hskip1em\null\nobreak
        \hfill#1%
        \parfillskip=0pt \finalhyphendemerits=0 \par%
     }%
}


\newcommand{\signed}[1]{%http://tex.stackexchange.com/a/91563/4301
    {%
        \leavevmode\unskip\nobreak\hfil\penalty50\hskip2em
        \hbox{}\nobreak\hfil#1%
        \parfillskip=0pt \finalhyphendemerits=0 \endgraf%
    }%
}

\newcommand*{\FillLineTohecz}[1]{%
    \raggedright
    \parfillskip-\rightskip
    \looseness=-1 % <-- Need space here
    %Space before \hspace* allows for a break before it
    \hspace*{0pt plus 1fil}%
    #1% 
}

\begin{document}

\bigskip\noindent
\savebox{\MyCachedResults}{Inside Savebox Carlisle: \BumpCarlisle{XXX}}%
Outside Savebox Carlisle: \BumpCarlisle{ZZZ}\par
\noindent
Text \usebox{\MyCachedResults}%

\bigskip\noindent
\savebox{\MyCachedResults}{Inside Savebox egreg: \FillLineEgreg{XXX}}%
Outside Savebox egreg: \FillLineEgreg{ZZZ}\par
\noindent
Some Text \usebox{\MyCachedResults}%

\bigskip\noindent
\savebox{\MyCachedResults}{Inside Savebox Seamus: \signed{XXX}}%
Outside Savebox Seamus: \signed{ZZZ}\par
\noindent
A bit of text \usebox{\MyCachedResults}%

\bigskip\noindent
\savebox{\MyCachedResults}{Inside Savebox tohecz: \FillLineTohecz{XXX}}%
Outside Savebox tohecz: \FillLineTohecz{ZZZ}\par
\noindent
Text before the bump \usebox{\MyCachedResults}%


\bigskip\noindent
\savebox{\MyCachedResults}{Inside Savebox hfill: \hfill XXX}%
Outside Savebox hfill: \hfill ZZZ\par
\noindent
Some more text \usebox{\MyCachedResults}%

\end{document}
share|improve this question
    
A \savebox is typeset at its natural width. You probably want \unhbox instead of \usebox –  egreg Mar 13 at 1:15
    
And \parbox{\linegoal}{} with the linegoal package? –  Gonzalo Medina Mar 13 at 1:17
    
Are you sure you want a box and not a macro? –  egreg Mar 13 at 8:21
    
@egreg: Hmmm, perhaps you are right in that I should use a macro - sure would simplify things. I was using a \savebox for performance reasons as the text may be used several times. –  Peter Grill Mar 13 at 21:27

2 Answers 2

An option using a \parbox of width given by linegoal from the linegoal package:

\documentclass{article}
\usepackage{showframe}
\usepackage{linegoal}

\newsavebox{\MyCachedResults}

% The following are from
%    http://tex.stackexchange.com/questions/91548/bump-right-aligned-text-to-next-line-iff-no-room

\newcommand*{\BumpCarlisle}[1]{% http://tex.stackexchange.com/a/91556/4301
    {\rightskip\fill\parfillskip-\rightskip
    \linepenalty100
    \exhyphenpenalty0
    \linebreak[0] % <-- Need space here
    %Space before \hspace* allows for a break before it
    \hspace*{\fill}#1}%
}

\newcommand*{\FillLineEgreg}[1]{% http://tex.stackexchange.com/a/91564/4301
    {%
        \nobreak\hfill\penalty50\hskip1em\null\nobreak
        \hfill#1%
        \parfillskip=0pt \finalhyphendemerits=0 \par%
     }%
}


\newcommand{\signed}[1]{%http://tex.stackexchange.com/a/91563/4301
    {%
        \leavevmode\unskip\nobreak\hfil\penalty50\hskip2em
        \hbox{}\nobreak\hfil#1%
        \parfillskip=0pt \finalhyphendemerits=0 \endgraf%
    }%
}

\newcommand*{\FillLineTohecz}[1]{%
    \raggedright
    \parfillskip-\rightskip
    \looseness=-1 % <-- Need space here
    %Space before \hspace* allows for a break before it
    \hspace*{0pt plus 1fil}%
    #1% 
}

\begin{document}

\bigskip\noindent
\savebox{\MyCachedResults}{\parbox[t]{\linegoal}{Inside Savebox Carlisle: \BumpCarlisle{XXX}}}%
Outside Savebox Carlisle: \BumpCarlisle{ZZZ}\par
\noindent
Text \usebox{\MyCachedResults}%

\bigskip\noindent
\savebox{\MyCachedResults}{\parbox[t]{\linegoal}{Inside Savebox egreg: \FillLineEgreg{XXX}}}%
Outside Savebox egreg: \FillLineEgreg{ZZZ}\par
\noindent
Some Text \usebox{\MyCachedResults}%

\bigskip\noindent
\savebox{\MyCachedResults}{\parbox[t]{\linegoal}{Inside Savebox Seamus: \signed{XXX}}}%
Outside Savebox Seamus: \signed{ZZZ}\par
\noindent
A bit of text \usebox{\MyCachedResults}%

\bigskip\noindent
\savebox{\MyCachedResults}{\parbox[t]{\linegoal}{Inside Savebox tohecz: \FillLineTohecz{XXX}}}%
Outside Savebox tohecz: \FillLineTohecz{ZZZ}\par
\noindent
Text before the bump \usebox{\MyCachedResults}%

\bigskip\noindent
\savebox{\MyCachedResults}{\parbox[t]{\linegoal}{Inside Savebox hfill: \hfill XXX}}%
Outside Savebox hfill: \hfill ZZZ\par
\noindent
Some more text \usebox{\MyCachedResults}%

\end{document}

enter image description here

share|improve this answer
    
@cfr No, this will also work with latex and xelatex. –  Gonzalo Medina Mar 13 at 2:36
    
So it does. So what does it mean that linegoal depends on the '\pdf­save­pos prim­i­tive of pdf­tex'? –  cfr Mar 13 at 3:53

enter image description here

\documentclass{article}
\usepackage{showframe}

\newsavebox{\MyCachedResults}

% The following are from
%    http://tex.stackexchange.com/questions/91548/bump-right-aligned-text-to-next-line-iff-no-room

\newcommand*{\BumpCarlisle}[1]{% http://tex.stackexchange.com/a/91556/4301
    {\rightskip\fill\parfillskip-\rightskip
    \linepenalty100
    \exhyphenpenalty0
    \linebreak[0] % <-- Need space here
    %Space before \hspace* allows for a break before it
    \hspace*{\fill}#1}%
}


\begin{document}

\bigskip\noindent
\savebox{\MyCachedResults}{Inside Savebox Carlisle: \BumpCarlisle{XXX}}%
Outside Savebox Carlisle: \BumpCarlisle{ZZZ}\par
\noindent
Text \unhbox\MyCachedResults

\end{document}
share|improve this answer
    
Does \unhbox preserve penalties? –  egreg Mar 13 at 10:27
    
@egreg You shouldn't ask trick questions:-) \unhbox preserves all penalties that are in the box but the savebox (setbox) in an unfortunate premature optimisation when building the inner horizontal list doesn't add penalties and language nodes that would be added to an outer horizontal list in what latex calls par mode. –  David Carlisle Mar 13 at 10:31

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.