
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.34pt
そしてそれ以下\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}