
在 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點
和 lesser\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}