如何获取(测量)物体的高度或宽度?

如何获取(测量)物体的高度或宽度?

如果我想指定某个对象的尺寸(例如)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

相关内容