\setbox 与 \sbox 和 \savebox - 我需要了解哪些区别?

\setbox 与 \sbox 和 \savebox - 我需要了解哪些区别?

使用 TeX和\setboxLaTeX有什么区别?\sbox\savebox

答案1

\sbox是 的缩写形式,\savebox同样,\mbox是 的缩写形式,\makebox适用于常见情况,即您不需要指定任何长度并希望使用框的自然大小。是底层 TeX 基元,因此它不会将其参数读取为普通宏(例如和\setbox之间的比较),并且除非您采取额外的预防措施来插入额外的组(LaTeX 版本会自动执行此操作),否则它不能安全地与颜色命令一起使用。\hbox\mbox

补充说明:我之前忘记了另一个重要的区别,它隐含在下面的例子中,但没有突出显示。\sbox\boxa{aa}就像\setbox\boxa\hbox{....}它提供了一样\hbox。因此,如果您需要保存垂直材料,则需要将 vbox 保存在 hbox 中,而 \setbox您可以这样做\setbox\boxa\vbox{aaa \par bbb},但是 vbox 和包含 vbox 的 hbox 之间没有太大区别,除非您开始使用拆箱原语,所以这个区别在实践中并不重要。

更新根据要求添加的示例(源信息比输出更具信息量)

\documentclass{article}
\tracingonline2
\showboxdepth10
\showboxbreadth10
\usepackage{color}

%these are the same except the latex version  \newsavebox
%checks that the name is not already used.
\newbox\boxa
\newsavebox\boxb

\begin{document}


\setbox\boxa\hbox{abc}
\sbox\boxb{abc}

The above look the same but see
%\showbox\boxa
%\showbox\boxb

\begin{verbatim}
> \box26=
\hbox(6.94444+0.0)x15.27782
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 b
.\kern0.27779
.\OT1/cmr/m/n/10 c

! OK.
\end<verbatim}

\begin{verbatim}
> \box27=
\hbox(6.94444+0.0)x15.27782
.\pdfcolorstack 0 push {0 g 0 G}
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 b
.\kern0.27779
.\OT1/cmr/m/n/10 c
.\pdfcolorstack 0 pop
\end{verbatim}

The LaTeX version has extra specials inserted so that colour acts like
fonts and stays with the box.

If you \emph{use} colour in the box it gets more serious

\setbox2\vbox{{% stop this broken test leaking to the page


\setbox\boxa\hbox{ab \color{red}c}
%\showbox\boxa
\begin{verbatim}
> \box26=
\hbox(6.94444+0.0)x18.33336
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 b
.\glue 3.33333 plus 1.66666 minus 1.11111
.\pdfcolorstack 0 push {1 0 0 rg 1 0 0 RG}
.\OT1/cmr/m/n/10 c
\end{verbatim}

See the box just contains a color push and the color pop is not saved
in the box but happens at the point of the save, so the colour stack
is corrupted which depending what you are doing can crash your printer
or just get the wrong colours or\ldots.

\setbox\boxa\hbox{{ab \color{red}c}}
%\showbox\boxa
\begin{verbatim}
> \box26=
\hbox(6.94444+0.0)x18.33336
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 b
.\glue 3.33333 plus 1.66666 minus 1.11111
.\pdfcolorstack 0 push {1 0 0 rg 1 0 0 RG}
.\OT1/cmr/m/n/10 c
.\pdfcolorstack 0 pop
\end{verbatim}
see that fixed it and the pop and push are now matched.

\sbox\boxb{ab \color{red}c}
%\showbox\boxb
\begin{verbatim}
> \box27=
\hbox(6.94444+0.0)x18.33336
.\pdfcolorstack 0 push {0 g 0 G}
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 b
.\glue 3.33333 plus 1.66666 minus 1.11111
.\pdfcolorstack 0 push {1 0 0 rg 1 0 0 RG}
.\OT1/cmr/m/n/10 c
.\pdfcolorstack 0 pop
.\pdfcolorstack 0 pop
\end{verbatim}
Latex gets it right.
}}% this box not put into the page so the colour stack is not
  % corrupted.

Even without colour there are differences

\setbox\boxa\hbox{aa\verb|\relax|}
%\showbox\boxa
\begin{verbatim}
> \box26=
\hbox(6.94444+0.8333)x41.49976
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 a
.\hbox(0.0+0.0)x0.0
.\OT1/cmtt/m/n/10 \
.\OT1/cmtt/m/n/10 r
.\OT1/cmtt/m/n/10 e
.\OT1/cmtt/m/n/10 l
.\OT1/cmtt/m/n/10 a
.\OT1/cmtt/m/n/10 x
\end{verbatim}

%\sbox\boxb{aa\verb|\relax|}% commented out as it makes an error.

\begin{verbatim}
! Missing } inserted.
<inserted text> 
\end{verbatim}


\end{document}

相关内容