2010-09-06 41 views
1

我想知道如何替换乳胶中的部分字符串。具体来说,我得到一个测量(如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上savebox。我将单词“Hello World”插入mybox。我创建了一个新的长度/宽度,称为myboxw/h。然后获取mybox的宽度/高度,并将其存储在myboxw/h中。然后我建立了一个图片环境,其大小对应于myboxw/h。麻烦的是,myboxw返回的形式为“132.56pt”,而图片环境的输入必须是无量纲的:“\ begin {picture} {132.56,132.56}”。

所以,我需要一个命令,将从字符串中去除测量单位。 谢谢。

+0

考虑使用http://tex.stackexchange.com/为LaTeX的相关问题。 – You 2010-09-06 15:26:55

回答

1

使用下面的技巧:

{ 
\catcode`p=12 \catcode`t=12 
\gdef\removedim#1pt{#1} 
} 

然后写:

\edef\myboxwnopt{\expandafter\removedim\the\myboxw} 
\edef\myboxhnopt{\expandafter\removedim\the\myboxh} 
\begin{picture}(\myboxwnopt,\myboxhnopt) 
\end{picture} 
0

LaTeX内核 - latex.ltx - 已经提供\[email protected],您可以使用它删除对长度的任何引用。另外,不需要为盒子的宽度和/或高度创建长度; \wd<box>返回宽度,而\ht<box>返回高度:

\documentclass{article} 

\makeatletter 
\let\stripdim\[email protected] % User interface for \[email protected] 
\makeatother 

\begin{document} 

\newsavebox{\mybox} 
\savebox{\mybox}{Hello World!} 

\begin{picture}(\stripdim\wd\mybox,\stripdim\ht\mybox) 
    \put(0,0){Hello world} 
\end{picture} 

\end{document}