Coloque o texto alinhado à direita na próxima linha se não houver espaço dentro de um \savebox

Coloque o texto alinhado à direita na próxima linha se não houver espaço dentro de um \savebox

Esta é uma continuação da minha pergunta anteriorColoque o texto alinhado à direita na próxima linha se não houver espaço. A diferença agora é que preciso colocar esse texto formatado dentro de um arquivo \savebox. No meu caso de uso real, por motivos de desempenho, estou armazenando em cache o texto que precisa seguir o texto existente (ou seja, na mesma linha), e esse texto armazenado em cache contém algum texto que deve ser alinhado à direita na borda da página.

Tentei usar todas as quatro soluções emColoque o texto alinhado à direita na próxima linha se não houver espaçojunto com um simples \hfill. Tudo isso parece funcionar quando usado fora do \savebox, mas não quando dentro do \savebox:

insira a descrição da imagem aqui

Notas:

  • Eu possonãouso \parbox{\linewidth}{}porque não sei onde na linha começará \savebox. Ou seja, há texto na mesma linhaanteso \useboxé aplicado.
  • A versão desejada teria o XXXalinhamento à direita assim como o ZZZtexto.

Código:

\documentclass{article}
\usepackage{showframe}

\newsavebox{\MyCachedResults}

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

\newcommand*{\BumpCarlisle}[1]{% https://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]{% https://tex.stackexchange.com/a/91564/4301
    {%
        \nobreak\hfill\penalty50\hskip1em\null\nobreak
        \hfill#1%
        \parfillskip=0pt \finalhyphendemerits=0 \par%
     }%
}


\newcommand{\signed}[1]{%https://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}

Responder1

Uma opção usando um \parboxde largura dado por linegoala partir dolinegoalpacote:

\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}

insira a descrição da imagem aqui

Responder2

insira a descrição da imagem aqui

\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}

informação relacionada