\savebox 内にスペースがない場合は、右揃えのテキストを次の行に移動する

\savebox 内にスペースがない場合は、右揃えのテキストを次の行に移動する

これは私の以前の質問の続きですスペースがない場合は右揃えのテキストを次の行に移動する違いは、このフォーマットされたテキストを 内に配置する必要が生じたことです\savebox。実際の使用例では、パフォーマンス上の理由から、既存のテキストの後に続く必要があるテキスト (つまり、同じ行にあるテキスト) をキャッシュしており、このキャッシュされたテキストには、ページの端に右揃えされるテキストが含まれています。

私は4つの解決策をすべて試してみましたスペースがない場合は右揃えのテキストを次の行に移動するシンプルな と一緒に\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}

関連情報