tikz/pgf:计算 \small 文本的宽度

tikz/pgf:计算 \small 文本的宽度

在 tikz/pgf 中,有一个名为的函数width("x")。根据 pgf 手册,它返回:

包含的(水平)TeX 框的宽度x

这句话之后它开始谈论一些我不明白的事情:

引号字符是防止x被解析所必需的。重要的是要记住,任何表达式在\edef 被解析之前都会被扩展,因此任何宏(例如,字体命令,如\tt\Huge)都需要“保护”(例如,\noexpand\Huge通常就足够了)。

我需要用修饰符测量一些文本的宽度\small。但是,我完全不明白该怎么\edef\noexpand。我尝试了以下所有组合:

\pgfmathsetmacro{\mywidth}{width("{\small My Text}")}
\pgfmathsetmacro{\mywidth}{width("{\noexpand\small My Text}")}
\pgfmathsetmacro{\mywidth}{width("\small{My Text}")}
\pgfmathsetmacro{\mywidth}{width("\noexpand\small{My Text}")}
\pgfmathsetmacro{\mywidth}{width("\small My Text")}
\pgfmathsetmacro{\mywidth}{width("\noexpand\small My Text")}

在所有情况下,的值\mywidth最终都为零。

我究竟做错了什么?

编辑:

这是 MWE

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

    \begin{document}
    
    \begin{tikzpicture}
    
        \pgfmathsetmacro{\mywidthsmall}{width("{\small My Text }")}
        \pgfmathsetmacro{\mywidthregular}{width("{My Text }")}
        \node[draw] (node1) {\mywidthsmall};
        \node[draw, below = 0pt of node1] (node2) {\mywidthregular};
    
    \end{tikzpicture}
    
\end{document}

得出:

在此处输入图片描述

答案1

我不知道这里面更深层的含义是什么。但widt("abc")给出了一个观点值,因为宽度长度测量根据定义它有长度单位,在本例中为点。

因此,如果要在某个地方使用此宽度,通常需要\pgfmathsetlenghtmacro\name{widt("abc")}长度例如 12.34
和较小的\pgfmathsetmacro\name{widt("abc")},消除单位点并给出号码例如 12.34

任何:

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage{tikz}
\def\sampletext{{\tiny My Text}}
\def\Sampletext{{\Huge My Text}}
\begin{document}
\section{tiny}
\pgfmathsetlengthmacro{\mywidth}{width("\sampletext")} 
\sampletext~ has the width \mywidth

\section{Huge}
\pgfmathsetlengthmacro{\Mywidth}{width("\Sampletext")} 
\Sampletext~ has the width \Mywidth

\section{pgfmathset\emph{length}macro}
\begin{tikzpicture}[
mystyle/.style={align=left,inner sep=0pt, anchor=west, draw}
]
\node[mystyle, draw, text width=\mywidth+1pt] (textbox) at (0,0) {\sampletext};
\draw[red] (textbox.north west) -- +(\mywidth,0) node[right=1mm]{\mywidth+1pt};

\node[mystyle, text width=\Mywidth+0pt] (textbox) at (0,-1) {\Sampletext};
\draw[red] (textbox.north west) -- +(\Mywidth,0) node[right=1mm]{\Mywidth};
\end{tikzpicture}

\section{Let's ruin it with pgfmathsetmacro, without \emph{length}}
\pgfmathsetmacro{\Mywidth}{width("\Sampletext")} 
\begin{tikzpicture}[
mystyle/.style={align=left,inner sep=0pt, anchor=west, draw}
]
\node[mystyle, text width=\Mywidth+0pt] (textbox) at (0,-1) {\Sampletext};
\draw[red] (textbox.north west) -- +(\Mywidth,0) node[right=1mm]{\Mywidth};
\end{tikzpicture}

Box correct, because \texttt{text width=123.4} (without unit)  sets points, as one would have written \texttt{text width=123.4pt}. \par 
Draw worse, because the default unit of TikZ is \texttt{cm}. 
\end{document}

答案2

我使用 得到这个\pgfmathsetmacro{\mywidthsmall}{width("{\small My Text }")}

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage{tikz}
\pgfmathsetmacro{\mywidthsmall}{width("{\small My Text }")} %<- added space
\pgfmathsetmacro{\mywidthhuge}{width("{\huge My Text }")}   %<- added space
\setlength{\parindent}{0pt}

\begin{document}
Width of {\small My Text} = \mywidthsmall

Width of {\huge My Text} = \mywidthhuge

\bigskip

Try setting node width $\ldots$

\bigskip

\begin{tikzpicture}

\node[text width=\mywidthsmall,font=\small,align=left,inner sep=0pt] at (0,0) {My Text};
\node[text width=\mywidthsmall,font=\small,inner sep=0pt] at (0,-1) {My Text My Text};

\node[text width=\mywidthhuge,font=\huge,inner sep=0pt] at (0,-3) {My Text};
\node[text width=\mywidthhuge,font=\huge,inner sep=0pt] at (0,-5) {My Text My Text};

\end{tikzpicture}
\end{document}

相关内容