
如果要排版的輸入文字與結構化範本分開,輸入是否可以確定其包含框的高度和寬度?
輸入範例。問號 (?) 被替換(Lua 或 TeX)。
The width of the parent box is ?
The height of the parent box is ?
幾種可能的模板:
\ruledhbox to 10cm{\vbox to 4cm{ \input input }} % 10cm by 4cm
\ruledvbox to 4cm{\hsize=10cm \input input } % 10cm by 4cm
\framed
[width=10cm,
height=4cm,
align={middle,lohi},
offset=0.5cm]
{ \input input } % 9cm by 3cm
在盒子被存放之前,尺寸是沒有意義的,所以這可能是開始的地方。
\aftergroup
我的+擴展\lastbox
回答:
\typeset{\hbox to50pt{\vbox to40pt{\hbox to30pt{width: \parwd }}}}
{ \let\parht\askparent\let\parwd\askparent\let\pardp\askparent
\setbox0=\hbox to50pt{\vbox to40pt{\hbox to30pt{width: \parwd }}}
\hbox to50pt{\vbox to40pt{\hbox to30pt{width: \parwd }}}
}
{ \let\parht\askparent\let\parwd\askparent\let\pardp\askparent
\setbox0=\hbox to50pt{\vbox to40pt{\hbox to30pt{width: \parwd }}}
\hbox to50pt{\vbox to40pt{\hbox to30pt{width: \askparent }}}
}
{ \let\parht\askparent\let\parwd\askparent\let\pardp\askparent
\setbox0=\hbox to50pt{\vbox to40pt{\hbox to30pt{width: \parwd }}}
\hbox to50pt{\vbox to40pt{\hbox to30pt{width: } \afterafter }}
}
{ \let\parht\askparent\let\parwd\askparent\let\pardp\askparent
\setbox0=\hbox to50pt{\vbox to40pt{\hbox to30pt{width: \parwd }}}
\hbox to50pt{\vbox to40pt{\hbox to30pt{width: }} \after }
}
{ \let\parht\askparent\let\parwd\askparent\let\pardp\askparent
\setbox0=\hbox to50pt{\vbox to40pt{\hbox to30pt{width: \parwd }}}
\hbox to50pt{
\setbox\storelast=\vbox to40pt{\hbox to30pt{width: }}
\xdef\parht{\the\ht\storelast}
\xdef\parwd{\the\wd\storelast}
\xdef\pardp{\the\dp\storelast}
\ifvmode\nointerlineskip\fi\box\storelast}
}
{ \let\parht\askparent\let\parwd\askparent\let\pardp\askparent
\setbox0=\hbox to50pt{\vbox to40pt{\hbox to30pt{width: \parwd }}}
\hbox to50pt{
\setbox\storelast=\vbox to40pt{\hbox to30pt{width: }}
\xdef\parht{\the\ht\storelast}
\xdef\parwd{\the\wd\storelast}
\xdef\pardp{\the\dp\storelast}
\vbox to40pt{\hbox to30pt{width: }}}
}
答案1
當您用於文字時,您可以使用和命令\framed
取得框中內容的寬度。這裡的一個限制是,當或設定包含的長度大於 時,您只能獲得可用值。\framedwidth
\framedheight
width
height
0pt
\starttext
\startframed[width=200pt,height=80pt,offset=10pt,rulethickness=0.5pt]
Width: \framedwidth \par
Height: \framedheight
\stopframed
\stoptext
width
要取得和鍵的值,height
您可以使用\localwidth
和\localheight
命令。在這裡您應該注意 ConTeXt 提供使用鍵設定的任何值,即當您設定width=fit
命令時\localwidth
給您fit
作為參數。
\starttext
\startframed[width=200pt,height=80pt,offset=10pt,rulethickness=0.5pt]
Width: \localwidth \par
Height: \localheight
\stopframed
\stoptext
最後一個可能的值可用於框架的背景,但與以前的方法不同,即使使用諸如 之類的設置,您也可以獲得正確的width
或height
框架width=fit
。
\defineoverlay
[boxdimensions]
[\vbox\bgroup
\hbox {Width: \overlaywidth}
\hbox{Height: \overlayheight}
\egroup]
\setupframed % don't make global framed settings in a document
[background=boxdimensions]
\starttext
\startframed[width=200pt,height=80pt,offset=10pt,rulethickness=0.5pt]
%
\stopframed
\stoptext
答案2
這個「解決方案」肯定不是通用的,也不是經過深思熟慮的,但它在簡單的情況下對我有用,使用純 TeX:
\newbox\storelast
\def\after{\setbox\storelast=\lastbox
{\globaldefs=1
\edef\parht{\the\ht\storelast}
\edef\parwd{\the\wd\storelast}
\edef\pardp{\the\dp\storelast}}
\ifvmode\nointerlineskip\fi\box\storelast}
\def\afterafter{\aftergroup\after}
\def\askparent{\aftergroup\afterafter}
\def\typeset#1{\let\parht\askparent\let\parwd\askparent\let\pardp\askparent
{\globaldefs=-1 \setbox0#1}#1}
之後,您可以使用 排版範本\typeset{...}
。在輸入中,您可以使用\parwd
、\parht
和\pardp
分別取得父框的寬度、高度和深度。的論證\typeset
必須是一個盒子(它必須以\hbox
, \vbox
... 開頭)。
例如,我有一個名為的文件,input.tex
其中包含
width \parwd\ height \parht
。
然後,以下幾行
\typeset{\hbox to50pt{\vbox to40pt{\hbox to30pt{\input input }}}}
\typeset{\vbox to50pt{\hbox to40pt{\vbox to30pt{\input input }}}}
列印(除了許多過滿/未滿的盒子)
width 30.0pt height 40.0pt
width 40.0pt height 30.0pt
正確。然而,
- 我不知道這對於更複雜的結構來說表現如何;
- 編輯:正如 Henri Menke 在評論中指出的那樣,對框進行兩次排版(後跟
\setbox0#1
)#1
可能會導致意想不到的副作用,例如,如果框中的計數器增加。我試圖透過\setbox0#1
在一個群組中評估 來減少這個問題\globaldefs=-1
,但是如果使用依賴於全域定義的包,這可能會改變盒子的尺寸,例如tikz
,產生不正確的結果; - 它假設命令
\parht
等是不在組內使用除了由盒子創建的以外; - 僅當父框為不在主要垂直清單中因為它用於
\lastbox
確定框尺寸,在外部垂直模式下為空。
對於最後一點,請考慮上面的範例,但現在我們感興趣的是最外層盒子的尺寸:
\typeset{\hbox to50pt{\vbox to40pt{\hbox to30pt{\input input }}}}
\typeset{\vbox to50pt{\hbox to40pt{\vbox to30pt{\input input }}}}
會列印
width 0.0pt height 0.0pt
width 0.0pt height 0.0pt
因為最外面的盒子設定為外部垂直模式。在這種情況下,您必須用另一個盒子包裹模板,例如
\typeset{\hbox{\hbox to50pt{\vbox to40pt{\hbox to30pt{}\input input }}}}
\typeset{\hbox{\vbox to50pt{\hbox to40pt{\vbox to30pt{}\input input }}}}
印刷
width 50.0pt height 40.0pt
width 40.0pt height 50.0pt
正如預期的那樣。