tikz/pgf: \small text의 너비 계산

tikz/pgf: \small text의 너비 계산

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

모든 경우에 의 값은 \mywidth0이 됩니다.

내가 도대체 ​​뭘 잘못하고있는 겁니까?

편집하다:

여기 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}

관련 정보