字体编码影响 \box_wd:N 计算

字体编码影响 \box_wd:N 计算

我编写了一个简单的函数来计算一行文本的宽度并使用以下方法显示它tikz

\documentclass{article}
%\usepackage{mathpazo}
\usepackage{domitian}
\usepackage[T1]{fontenc}
%\let\oldstylenums\oldstyle
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{math}
\usetikzlibrary{calc}
\usetikzlibrary{fpu}
\usetikzlibrary{fit}
\ExplSyntaxOn
\box_new:N \l_my_otherbox
\dim_new:N \l_my_dim
\dim_new:N \l_my_htdim
\dim_new:N \l_my_dpdim
\NewDocumentCommand{\boxlen}{m}
{
    \hbox_set:Nn \l_my_otherbox {#1}
    \dim_set:Nn \l_my_dim  {\box_wd:N \l_my_otherbox}
    \dim_set:Nn \l_my_htdim  {\box_ht:N \l_my_otherbox}
    \dim_set:Nn \l_my_dpdim  {\box_dp:N \l_my_otherbox}
    \begin{tikzpicture}        
        \node[draw,text~width=9cm,align=left,fill=white,draw=black,inner~sep=0pt] (box) at (0,0)
        {#1};  
         \draw[line~width=2pt]~($(box.south~west)+(0,-.5cm)$)~--~+(9cm,0) node[below,pos=0.5] {9 cm};
         \draw[red,line~width=2pt]~let~\p1=(box.west),\p2=(box.base)~in~($(box.base)+(\x1-\x2,-\dim_use:N \l_my_dpdim)$)~--~+(\dim_use:N \l_my_dim,0) node[below,pos=0.5] 
         {\fp_eval:n {round(\dim_to_decimal_in_cm:n {\dim_use:N \l_my_dim},2)}cm};
    \end{tikzpicture}
}
\ExplSyntaxOff
\begin{document}
This part of the document uses the \verb+domitian+ font family:\par
\boxlen{Long word.}
\vskip 2ex
\boxlen{\textbf{Even longer} word.}
\vskip 2ex
\boxlen{\textbf{Emphasize a word} or do not emphasize a word.}
\vskip 2ex
\boxlen{Emphasize a word or do not emphasize a word.}

{  % use computer modern roman
\fontfamily{cmr}\selectfont 
This part of the document uses the \verb+Computer Modern Roman+ font family:\par
\boxlen{Long word.}
\vskip 2ex
\boxlen{\textbf{Even longer} word.}
\vskip 2ex
\boxlen{\textbf{Emphasize a word} or do not emphasize a word.}
\vskip 2ex
\boxlen{Emphasize a word or do not emphasize a word.}
}
\end{document}

当我使用 Computer Modern Roman 时,该函数工作正常(顺便说一句,获取命令的代码确实很困难\fontfamily)。但是如果我使用domitian,随着句子变长,计算出的宽度\box_wd似乎小于文本的实际长度。 在此处输入图片描述

它似乎\box_wd是为特定字体系列编写的,所以我想知道是否有办法计算非标准字体系列的调整。

答案1

您可以简化为

\documentclass{article}
%\usepackage{mathpazo}
\usepackage{domitian}
\usepackage[T1]{fontenc}
%\let\oldstylenums\oldstyle
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{math}
\usetikzlibrary{calc}
\usetikzlibrary{fpu}
\usetikzlibrary{fit}
\ExplSyntaxOn
\box_new:N \l_my_otherbox
\dim_new:N \l_my_dim
\dim_new:N \l_my_htdim
\dim_new:N \l_my_dpdim
\NewDocumentCommand{\boxlen}{m}
{
    \hbox_set:Nn \l_my_otherbox {#1}
    \dim_set_eq:NN \l_my_dim  {\box_wd:N \l_my_otherbox}
    \dim_set_eq:NN \l_my_htdim  {\box_ht:N \l_my_otherbox}
    \dim_set_eq:NN \l_my_dpdim  {\box_dp:N \l_my_otherbox}
    \begin{tikzpicture}        
        \node[draw,text~width=9cm,align=left,fill=white,draw=black,inner~sep=0pt] (box) at (0,0)
        {#1};  
        \node[draw,green,text~width=9cm,align=left,fill=white,draw=black,inner~sep=0pt] (boxx) at (0,.5)
        {\box_use:N\l_my_otherbox};  
         \draw[line~width=2pt]~($(box.south~west)+(0,-.5cm)$)~--~+(9cm,0) node[below,pos=0.5] {9 cm};
         \draw[red,line~width=2pt]~let~\p1=(box.west),\p2=(box.base)~in~($(box.base)+(\x1-\x2,-\dim_use:N \l_my_dpdim)$)~--~+(\dim_use:N \l_my_dim,0) node[below,pos=0.5] 
         {\fp_eval:n {round(\dim_to_decimal_in_cm:n {\dim_use:N \l_my_dim},2)}cm};
    \end{tikzpicture}
}
\ExplSyntaxOff

\showoutput
\begin{document}

\boxlen{\textbf{Emphasize a word} or do not emphasize a word.}

\end{document}


在此处输入图片描述

您可以看到红线显示的是绿色原始框的宽度。

在黑色文本中,您正在重新设置 9 厘米小页面中的文本,raggedright,它会进行调整,\spaceskip因此单词之间的空间会有所不同

................\glue(\spaceskip) 3.33298
................\T1/Domitian-TLF/bold/n/10 a
................\glue(\spaceskip) 3.33298

而不是

.................\glue 2.5 plus 1.25 minus 0.833
.................\T1/Domitian-TLF/bold/n/10 a
.................\glue 2.5 plus 1.25 minus 0.833

这里的问题是,默认情况下 tikz 使用纯 tex 定义,\raggedright其为

\def\raggedright{\rightskip\z@ plus2em \spaceskip.3333em \xspaceskip.5em\relax}

因此这里的字间间距无条件地设置为 1/3 em,忽略字体度量设置的(在本例中更紧密的)间距。

如果您使用该text badly ragged选项,则您将使用根本不设置空格跳过的标准乳胶定义。

另一种方法是更新普通定义版本以使用\fontdimen当前字体的参数,而不是假设 .3333em 作为空格的目标宽度。

\raggedright如果将其更改为强制使用字体指定的宽度而不是 1/3 em,则纯文本版本会更接近

在此处输入图片描述

如果你在加载 tikz 后添加

\makeatletter
\tikzoption{text ragged}[]%
{\def\tikz@text@action{\pgfutil@raggedright\rightskip0pt plus2em \spaceskip\fontdimen2\font \xspaceskip\fontdimen7\font}}%
\makeatother

但即便如此,由于此版本的“右边不齐”强制单词间空间均匀,因此您不会像平常一样在粗体部分获得不同的空间。

根据实际使用情况,最好不要使用固定宽度节点,而只需使用自然宽度节点(即 mbox),或者像我在绿色文本中所做的那样,重新使用该框,而不是使用不同的设置重新设置文本。

相关内容