
我試圖將我的文字放入特定的空間,因為它是圖形解釋。我想做如下圖的東西:
是否可以使文字適合該空間? LaTex 自己做嗎?應該使用套件還是命令?
謝謝。
答案1
修改後的答案(全自動):
在這個修改後的答案中,有人調用\constraintext
,向其傳遞五個參數:
#1
受約束的文字高度
#2
受約束的文字寬度
#3
要約束的文字內容
#4
大於或等於內容的自然(未縮放)寬度的猜測(選擇太大隻會導致一些額外的迭代)
#5
每次迭代時都會從自然寬度中減去長度的正增量,以滿足所需的縱橫比。
如所提到的,\parbox
使用自然寬度(輸入)作為框寬度來建立未縮放的(以當前字體大小)#4
。測試了縱橫比。如果您選擇的#4
足夠大,縱橫比將低於目標值。然後,將未縮放的方塊縮小指定的量#5
,並重新測試縱橫比。相對於先前的測試,它將增加。遞歸地重複此過程,直到測試的縱橫比等於或超過目標值。
此時,框可以設定為約束高度 ( #1
),\parbox
寬度保持等於或略低於約束寬度。填充是透過匹配的\hfill
s 來完成的,以使排版寬度等於約束寬度。
我已在 MWE 中新增註釋,說明刪除診斷列印的註釋內容。
\documentclass{article}
\usepackage{scalerel}
\usepackage{fp}
\usepackage{lipsum}
\usepackage{stackengine}
\newcount\boxheight
\newcount\boxwidth
\newlength\constrainedwidth
\newsavebox\testbox
\newlength\currentwidth
\newcommand\aspect[1]{%
\boxheight=\ht#1\relax%
\boxwidth=\wd#1\relax%
\FPdiv\testaspect{\the\boxheight}{\the\boxwidth}%
\testaspect\\% COMMENT THIS LINE TO REMOVE ASPECT-RATIO PRINTS
}
% TARGET-HEIGHT, TARGET-WIDTH, CONTENT, GUESS>NATURAL-WIDTH, dWIDTH
\newcommand\constraintext[5]{%
\def\svfboxsep{\the\fboxsep}%
\setlength\fboxsep{0pt}%
\setlength\constrainedwidth{#2}%
\sbox\testbox{\rule{#2}{#1}}%
TARGET ASPECT RATIO\\% COMMENT THIS LINE TO REMOVE COMMENT
V\\% COMMENT THIS LINE TO REMOVE COMMENT
\aspect{\testbox}%
\edef\aspectratio{\testaspect}%
\constrainsize{#1}{\aspectratio}{#3}{#4}{#5}%
\setlength\fboxsep{\svfboxsep}%
}
\newcommand\constrainsize[5]{%
\sbox{\testbox}{\fbox{\parbox[b]{#4}{#3}}}%
\aspect{\testbox}%
\FPiflt{\testaspect}{#2}%
\setlength{\currentwidth}{#4}%
\addtolength{\currentwidth}{-#5}%
\constrainsize{#1}{#2}{#3}{\currentwidth}{#5}%
\else
\setlength\constrainedwidth{#1}%
\FPdiv\result{1.0}{#2}%
% CHANGE \framebox TO \makebox TO REMOVE FRAME
\framebox[\result\constrainedwidth]{\hfill%
\scaleto{\parbox[b]{\currentwidth}{#3}}{#1}%
\hfill%
}%
\fi%
}
\parindent 0in
\begin{document}
\constraintext{5in}{2.8in}{\lipsum[1-2]}{3in}{.02in}
\rule[-\fboxsep]{.2ex}{5in}\\
\rule{2.8in}{.2ex}
\clearpage
\constraintext{5in}{2.8in}{\lipsum[3]}{2.25in}{.02in}
\rule[-\fboxsep]{.2ex}{5in}\\
\rule{2.8in}{.2ex}
\end{document}
下面顯示了兩個結果,具有不同大小的輸入流,但都設定為相同大小(5 英吋 x 2.8 英吋)的最終框。框旁邊和下方設定了規則,以確認框尺寸的準確性。
顯示診斷,首先給出目標縱橫比,然後給出每次迭代的縱橫比。當寬高比突然出現變化時,表示由於換行,排版框需要額外的文字行。最終的縱橫比越接近目標,約束框所需的水平填充就越少。
原答案(非全自動):
這不是完全自動化的,但是透過一些迭代,就可以完成工作。假設您想將前兩個 Lipsum 段落放入一個 5 英寸高 x 2 英寸寬的盒子中。計算出來的高度/寬度長寬比為 2.5。我首先將這些段落放入框架中\parbox
,然後(這裡是迭代)使用我方便的\aspect
宏來調整框寬度以實現正確的縱橫比(我花了大約 4 次迭代,在這個迭代階段可能需要一分鐘)。
一旦我獲得了 2.5+/-epsilon 縱橫比的結果,我就使用包裝\scaleto
中的一個scalerel
將盒子縮放到 5 英寸的高度。長寬比保證其寬度為 2。我展示了原始的 10pt 排版框 (\x) 及其縮放後的同伴。
\documentclass{article}
\usepackage{scalerel}
\usepackage{fp}
\usepackage{lipsum}
\newcount\boxheight
\newcount\boxwidth
\newcommand\aspect[1]{%
\boxheight=\ht#1\relax%
\boxwidth=\wd#1\relax%
\FPdiv\result{\the\boxheight}{\the\boxwidth}\result%
}
\begin{document}
\newsavebox\x
\sbox{\x}{\fbox{\parbox[b]{2.42in}{\lipsum[1-2]}}}
\aspect{\x}
\usebox{\x}
\scaleto{\usebox{\x}}{5in}
\end{document}
至於您的特定範例圖像的佈局,我重新評估了縱橫比為 1.75 作為我的目標(導致段落大小為 5" x 20/7"),並提出了這個,我的\rule
s 可以替換和\includegraphics
。當然,我的 5 英寸高度只是一個猜測,但這是可以修改的。
\documentclass{article}
\usepackage{scalerel}
\usepackage{fp}
\usepackage{lipsum}
\usepackage[oldsyntax]{stackengine}
\newcount\boxheight
\newcount\boxwidth
\newcommand\aspect[1]{%
\boxheight=\ht#1\relax%
\boxwidth=\wd#1\relax%
\FPdiv\result{\the\boxheight}{\the\boxwidth}\result%
}
\begin{document}
\newsavebox\x
\sbox{\x}{\fbox{\parbox[b]{2.84in}{\lipsum[1-2]}}}
%\aspect{\x}
\def\hgp{\hspace{.1in}}
\def\plotA{\protect\rule{1.36in}{0.66in}}
\def\plotB{\protect\rule{1.36in}{1.36in}}
\def\plotC{\protect\rule{1.36in}{1.36in}}
\def\plotD{\protect\rule{1.36in}{1.36in}}
\def\plotE{\protect\rule{1.36in}{1.36in}}
\def\plotF{\protect\rule{1.36in}{1.36in}\hgp}
\def\plotG{\protect\rule{1.36in}{1.36in}}
\def\stackalignment{l}
\Sstackgap=.1in
\Shortstack{{\plotA} {\plotB} {\plotC} {\plotD} {\plotE}}\hgp%
\stackon{\plotF \plotG}{\scaleto{\usebox{\x}}{5in}}
\end{document}
ps 我正在使用 V2.0 stackengine,你可能還沒有。在您能夠升級之前,為了讓第二個 MWE 正常工作,只需刪除[oldsyntax]
軟體包選項即可。
答案2
\tcolorbox
包含庫fitting
及其命令\tcboxfit
。它將其內容放入給定寬度和高度的盒子內。
接下來一些例子:
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage{lmodern}
\tcbset{colframe=blue!50!black,colback=green!10!white,
boxsep=0pt,top=1mm,bottom=1mm,left=1mm,right=1mm,
nobeforeafter, arc=0pt, outer arc=0pt}
\begin{document}
\lipsum[1]
\noindent\tcboxfit[width=.5\linewidth,height=6cm]{\lipsum[1]}
\hfill
\tcboxfit[width=.45\linewidth, height=4cm]{\lipsum[1]}
\noindent\tcboxfit[width=\linewidth, height=2cm]{\lipsum[1]}
\end{document}