width%20%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%E3%81%AF%3F.png)
しばらくの間解決しようとしてきた問題がありますが、今のところ成功していません。私の知る限り、TeX は固定されたコンテナ幅に関してテキスト行を適切に分割するのに非常に優れています。私が達成しようとしているのはそれとは異なります。特定のコンテンツに対して「最適な」幅を計算できるようにしたいのです。
最適とは、根本的な制約があることを意味します。たとえば、\settooptimalwidth#1#2#3
次のようなコマンドを記述できるようにしたいとします。
- 調整する長さとして#1
- 最大幅として#2
- コンテンツとしての#3
#3 が非常に短い (#2 より短い) 場合は、 と同じ処理が実行されます\settowidth
。#3 が #2 より長い場合は、#3 をミニボックスに入れたかのように行に分割し、(ここが難しいところですが) その中で最も幅の広い行の長さを返します。ここでは、行分割アルゴリズムが通常の「両端揃え」ではなく、不揃いの左/右または中央揃えを使用していると想定しています (そうでない場合、問題は無関係です)。
このようなコマンドが役立つ例をいくつか挙げてみましょう。 * コンテンツを中央揃えにして、改行する可能性のある配列列の幅を自動調整する。 * 装飾的なフレームを備えた、右揃えの詩環境。詩が非常に短い場合は、フレームは小さくなります。詩が非常に長い場合は、テキストが「最適な」幅に分割されます。
を使うのがよいと思います\unhbox
が、どのように使用すればよいかよくわかりません。どなたか助けていただければ幸いです。
答え1
David Carlisle が上で書いたように、パッケージはvarwidth
最適です。これは、varwidth
私が求めていたことを実現する環境を提供します。以下は、beamer を使用してグラデーション シェード バックグラウンドにテキストを描画する最小限の例です。
\documentclass{beamer}
\usepackage{calc,varwidth}
\colorlet{titleshadeA}{white!30!orange}
\colorlet{titleshadeB}{red!30!black}
\newlength\shadeboxwidth
\newlength\shadeboxheight
\newlength\shadeboxsep
\newcommand\shadebox[4]{%
\setlength\shadeboxsep{#3}%
\settowidth\shadeboxwidth{\begin{varwidth}{\textwidth}#4\end{varwidth}}%
\advance\shadeboxwidth by 2\shadeboxsep%
\settototalheight\shadeboxheight{\begin{varwidth}{\textwidth}#4\end{varwidth}}%
\advance\shadeboxheight by 2\shadeboxsep%
\pgfdeclarehorizontalshading{titleshade}{\shadeboxheight}{%
color(0mm)=(#1);%
color(\shadeboxwidth)=(#2)%
}%
\rlap{\pgfuseshading{titleshade}}%
\begin{beamercolorbox}[sep=\shadeboxsep,wd=\shadeboxwidth,ht=\shadeboxheight,dp=0pt]{postit}%
\color{white}\begin{varwidth}{\textwidth}\raggedleft#4\end{varwidth}%
\end{beamercolorbox}%
}
\begin{document}
\begin{frame}
\strut\hfill\shadebox{titleshadeA}{titleshadeB}{1ex}{\raggedleft\LARGE Something short}\\
\strut\hfill\shadebox{titleshadeA}{titleshadeB}{1ex}{\raggedleft\LARGE Something very long which will be broken and aligned to the right}\bigskip
\hrule height 2pt\bigskip
Notice how it doesnt spawn the total text width (see the black rule above), but instead adjusts itself to the longest text line of the content.
\end{frame}
\end{document}