如果 \savebox 內沒有空間,則將右對齊文字移至下一行

如果 \savebox 內沒有空間,則將右對齊文字移至下一行

這是我之前問題的後續如果沒有空間,則將右對齊文字移至下一行。現在的區別是我需要將此格式化文字放在\savebox.在我的實際用例中,出於性能原因,我緩存需要跟隨現有文本(即在同一行上)的文本,並且此緩存的文本包含一些要與頁面邊緣右對齊的文本。

我嘗試使用所有四種解決方案如果沒有空間,則將右對齊文字移至下一行以及一個簡單的\hfill.這些似乎在外部使用時有效\savebox,但在內部時無效\savebox

在此輸入影像描述

筆記:

  • 我可以不是使用\parbox{\linewidth}{},因為我不知道該從哪裡\savebox開始。即同一行有文字\usebox應用。
  • 所需的版本將與文字XXX一樣在右側對齊ZZZ

代碼:

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

答案1

使用以下\parbox給出的寬度的選項linegoallinegoal包裹:

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

在此輸入影像描述

答案2

在此輸入影像描述

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

相關內容