width%EB%A5%BC%20%EC%96%B4%EB%96%BB%EA%B2%8C%20%EC%82%AC%EC%9A%A9%ED%95%98%EB%82%98%EC%9A%94%3F.png)
한동안 해결하려고 노력했지만 지금까지 성공하지 못한 문제가 있습니다. 내가 아는 한, TeX은 고정된 컨테이너 너비에 따라 텍스트 줄을 적절하게 나누는 데 매우 능숙합니다. 내가 달성하려는 것은 다릅니다. 주어진 콘텐츠에 대한 "최적의" 너비를 계산할 수 있기를 원합니다.
최적이라는 말은 근본적인 제약이 있다는 뜻입니다. 예를 들어, 다음과 같은 명령을 작성하고 싶습니다 \settooptimalwidth#1#2#3
.
- #1 조정할 길이
- #2 최대 너비
- #3 콘텐츠로
#3이 매우 짧은 경우(#2보다 짧음) 와 동일한 작업을 수행합니다 \settowidth
. #3이 #2보다 길면 #3을 미니박스에 넣은 것처럼 줄로 나누고 (이것이 까다로운 부분입니다) 그 중에서 가장 넓은 줄의 길이를 반환합니다. 여기서는 줄 바꿈 알고리즘이 일반적인 "정렬" 정렬을 사용하지 않고 대신 왼쪽/오른쪽 또는 가운데 정렬을 사용한다고 가정합니다(그렇지 않으면 문제는 관련이 없습니다).
이러한 명령이 유용할 수 있는 몇 가지 예를 들어 보겠습니다. * 내용이 가운데에 맞춰질 배열 열의 너비를 자동 조정하고 잠재적인 줄 바꿈 * 멋진 프레임이 있는 일종의 오른쪽 정렬 절 환경. 구절이 매우 짧다면 프레임도 작을 것입니다. 구절이 매우 길면 텍스트가 "최적의" 너비로 끊어집니다.
가는 방법은 을 사용하는 것이라고 생각하는데 \unhbox
어떻게 사용하는지 잘 모르겠습니다. 어떤 도움이라도 주시면 감사하겠습니다.
답변1
David Carlisle이 위에서 쓴 것처럼 패키지가 varwidth
좋은 방법입니다. varwidth
내가 요청한 작업을 수행하는 환경을 제공합니다 . 다음은 비머를 사용하여 그라데이션 음영 배경에 텍스트를 그리는 최소한의 예입니다.
\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}