Latex 中的字符串替换
我想知道如何替换乳胶中的部分字符串。具体来说,我得到了一个测量值(例如 3pt、10mm 等),并且我想删除该测量值的单位(例如 3pt-->3、10mm-->10 等)。 我想要一个命令来执行此操作的原因在以下代码中:
\newsavebox{\mybox}
\sbox{\mybox}{Hello World!}
\newlength{\myboxw}
\newlength{\myboxh}
\settowidth{\myboxw}{\usebox{\mybox}}
\settoheight{\myboxh}{\usebox{\mybox}}
\begin{picture}(\myboxw,\myboxh)
\end{picture}
基本上我创建了一个名为 mybox 的保存箱。我将“Hello World”插入 mybox 中。我创建一个新的长度/宽度,称为 myboxw/h。然后我获取 mybox 的宽度/高度,并将其存储在 myboxw/h 中。然后我设置了一个图片环境,其尺寸对应于myboxw/h。问题在于 myboxw 返回“132.56pt”形式的内容,而图片环境的输入必须是无量纲的:“\begin{picture}{132.56, 132.56}”。
因此,我需要一个命令来从字符串中去除测量单位。 谢谢。
I'd like to know how to replace parts of a string in latex. Specifically I'm given a measurement (like 3pt, 10mm, etc) and I'd like to remove the units of that measurement (so 3pt-->3, 10mm-->10, etc).
The reason why I'd like a command to do this is in the following piece of code:
\newsavebox{\mybox}
\sbox{\mybox}{Hello World!}
\newlength{\myboxw}
\newlength{\myboxh}
\settowidth{\myboxw}{\usebox{\mybox}}
\settoheight{\myboxh}{\usebox{\mybox}}
\begin{picture}(\myboxw,\myboxh)
\end{picture}
Basically I create a savebox called mybox. I insert the words "Hello World" into mybox. I create a new length/width, called myboxw/h. I then get the width/height of mybox, and store this in myboxw/h. Then I set up a picture environment whose dimensions correspond to myboxw/h. The trouble is that myboxw is returning something of the form "132.56pt", while the input to the picture environment has to be dimensionless: "\begin{picture}{132.56, 132.56}".
So, I need a command which will strip the units of measurement from a string.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用以下技巧:
然后编写:
Use the following trick:
Then write:
考虑 https://www.ctan.org/pkg/xstring 处的
xstring
包。Consider the
xstring
package at https://www.ctan.org/pkg/xstring.LaTeX 内核 -
latex.ltx
-已经提供了\strip@pt
,您可以使用它来删除对长度的任何引用。此外,无需为盒子的宽度和/或高度创建长度;\wd
返回宽度,而\ht
返回高度:The LaTeX kernel -
latex.ltx
- already provides\strip@pt
, which you can use to strip away any reference to a length. Additionally, there's no need to create a length for the width and/or height of a box;\wd<box>
returns the width, while\ht<box>
returns the height: