
Esta es una continuación de mi pregunta anterior.Pasar el texto alineado a la derecha a la siguiente línea si no hay espacio. La diferencia ahora es que necesito colocar este texto formateado dentro de un archivo \savebox
. En mi caso de uso real, por razones de rendimiento, estoy almacenando en caché el texto que debe seguir al texto existente (es decir, en la misma línea), y este texto almacenado en caché contiene algo de texto que debe estar alineado a la derecha con el borde de la página.
Intenté utilizar las cuatro soluciones enPasar el texto alineado a la derecha a la siguiente línea si no hay espaciojunto con un sencillo \hfill
. Todo esto parece funcionar cuando se usa fuera de \savebox
, pero no dentro de \savebox
:
Notas:
- Puedonoutilícelo
\parbox{\linewidth}{}
ya que no sé en qué parte de la línea\savebox
comenzará. Es decir, hay texto en la misma línea.antesse\usebox
aplica. - La versión deseada tendría el
XXX
alineado a la derecha como está elZZZ
texto.
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}
Respuesta1
Una opción que utiliza un \parbox
ancho dado por linegoal
desde ellinegoal
paquete:
\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}
Respuesta2
\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}