![如何获取(测量)物体的高度或宽度?](https://linux22.com/image/439781/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%EF%BC%88%E6%B5%8B%E9%87%8F%EF%BC%89%E7%89%A9%E4%BD%93%E7%9A%84%E9%AB%98%E5%BA%A6%E6%88%96%E5%AE%BD%E5%BA%A6%EF%BC%9F.png)
如果我想指定某个对象的尺寸(例如)rule{<width>}{<height>}
,使得这些尺寸是其他对象的尺寸,那么我如何获取(测量)这些其他对象的高度和宽度并将它们传递给的参数rule{<width>}{<height>}
。我正在寻找某种东西,它可以接受合适的对象Object
并给出其尺寸之一,例如h = \height{Object_1}
,或者像w = \height{Object_2}
。
主要问题是获得给定物体的尺寸,不使用这rule
答案1
LaTeX 提供了\settowidth{<len>}{<stuff>}
和\settoheight{<len>}{<stuff>}
,用于设置 的<len>
宽度或高度<stuff>
。以下是示例:
\documentclass{article}
\begin{document}
\newlength{\mylen}
abc has width
\settowidth{\mylen}{abc}\the\mylen,
height
\settoheight{\mylen}{abc}\the\mylen{}
and depth
\settodepth{\mylen}{abc}\the\mylen.
def has width
\settowidth{\mylen}{def}\the\mylen,
height
\settoheight{\mylen}{def}\the\mylen{}
and depth
\settodepth{\mylen}{def}\the\mylen.
ghi has width
\settowidth{\mylen}{ghi}\the\mylen,
height
\settoheight{\mylen}{ghi}\the\mylen{}
and depth
\settodepth{\mylen}{ghi}\the\mylen.
\end{document}
您还可以将内容捕获到一个框中,然后从那里提取尺寸(宽度通过\wd<box>
,高度通过\ht<box>
,深度通过\dp<box>
):
\documentclass{article}
\begin{document}
\newsavebox{\mybox}
\savebox{\mybox}{abc}%
abc has width \the\wd\mybox,
height \the\ht\mybox{}
and depth \the\dp\mybox.
\savebox{\mybox}{def}%
def has width \the\wd\mybox,
height \the\ht\mybox{}
and depth \the\dp\mybox.
\savebox{\mybox}{ghi}%
ghi has width \the\wd\mybox,
height \the\ht\mybox{}
and depth \the\dp\mybox.
\end{document}
\the
上面只是用来打印长度。参见命令\the
。