
No tikz/pgf, existe uma função chamada width("x")
. Do manual pgf, ele retorna:
a largura de uma caixa TeX (horizontal) contendo
x
.
Depois dessa frase começa a falar de coisas que não entendo:
Os caracteres de aspas são necessários para evitar
x
que sejam analisados. É importante lembrar que qualquer expressão é expandida\edef
antes de ser analisada, portanto quaisquer macros (por exemplo, comandos de fonte como\tt
ou\Huge
) precisarão ser “protegidas” (por exemplo,\noexpand\Huge
geralmente é suficiente).
Preciso medir a largura de algum texto com o \small
modificador. No entanto, não entendo o que \edef
fazer \noexpand
. Eu tentei todas as seguintes combinações:
\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")}
Em todos os casos, o valor de \mywidth
acaba sendo zero.
O que estou fazendo de errado?
EDITAR:
Aqui está um 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}
Isto dá:
Responder1
Não entendi qual é o sentido mais profundo aqui. Mas widt("abc")
dá umapontarvalor, porquelarguraé ummedida de comprimentoe por definição temuma unidade de comprimento, neste caso pontos.
Portanto, normalmente é necessário , se esta largura for usada em algum lugar, o que dá\pgfmathsetlenghtmacro\name{widt("abc")}
O comprimentopor exemplo, 12,34ponto
e menor \pgfmathsetmacro\name{widt("abc")}
que apaga os pontos unitários e dáo númeropor exemplo, 12,34
Qualquer que seja:
\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}
Responder2
Eu entendo isso usando \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}