tikz/pgf: Breite von \small-Text berechnen

tikz/pgf: Breite von \small-Text berechnen

In tikz/pgf gibt es eine Funktion namens width("x"). Laut dem pgf-Handbuch gibt sie Folgendes zurück:

die Breite einer (horizontalen) TeX-Box, die enthält x.

Nach diesem Satz beginnt es, über Dinge zu sprechen, die ich nicht verstehe:

Die Anführungszeichen sind notwendig, um xeine Analyse zu verhindern. Es ist wichtig, daran zu denken, dass jeder Ausdruck vor der Analyse mit erweitert wird , daher müssen \edef alle Makros (z. B. Schriftbefehle wie \ttoder ) „geschützt“ werden (z. B. ist normalerweise ausreichend).\Huge\noexpand\Huge

Ich muss die Breite eines Textes mit dem \smallModifikator messen. Ich verstehe jedoch überhaupt nicht, was ich \edeftun \noexpandsoll. Ich habe alle der folgenden Kombinationen ausprobiert:

\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")}

In allen Fällen ist der Wert \mywidthletztendlich Null.

Was mache ich falsch?

BEARBEITEN:

Hier ist ein 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}

Das gibt:

Bildbeschreibung hier eingeben

Antwort1

Ich verstehe nicht, was hier der tiefere Sinn ist. Aber widt("abc")es gibt einePunktWert, dennBreiteist einLängenmaßund per Definition hat eseine Längeneinheit, in diesem Fall Punkte.

Es wird also normalerweise benötigt , wenn diese Breite irgendwo verwendet werden soll, was gibt\pgfmathsetlenghtmacro\name{widt("abc")}die LängezB 12.34pt
und kleiner \pgfmathsetmacro\name{widt("abc")}, was die Einheitspunkte löscht und gibtdie NummerzB 12.34

Was auch immer:

Bildbeschreibung hier eingeben

\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}

Antwort2

Ich bekomme dies mithilfe von \pgfmathsetmacro{\mywidthsmall}{width("{\small My Text }")}.

Bildbeschreibung hier eingeben

\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}

verwandte Informationen