TikZ 노드에 대한 키를 사용하여 길이 설정

TikZ 노드에 대한 키를 사용하여 길이 설정

지정된 선의 길이에 따라 너비가 정의되는 분할 직사각형을 만들고 싶습니다. 다음과 같은 키를 통해 이 작업을 수행하고 싶습니다.

\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\newlength\aetmplength

\begin{document}

\begin{tikzpicture}[
  set text to width of/.code={%%
    \settowidth\aetmplength{#1}%%
    \typeout{--->`#1`::\the\aetmplength}%%
    \pgfkeysalso{text width=\the\aetmplength}%%
  },
  ]
  \node[draw,
        shape=rectangle split,
        rectangle split parts=2,
        set text to width of=This is the top of the node,
        %text width=0.95in
        ] () 
        {
         This is the top of the node
         \nodepart{two}
           first item\\
           second item\\
           \ldots};
\end{tikzpicture}

\end{document}

그러나 이것은 작동하지 않습니다. 이 문제를 해결하는 데 쉽게 시간을 보낼 수 있습니다. 따라서 이것은 내가 원하는 길이를 설정하는 방법의 문제가 아닙니다. 내가 이해하고 싶은 것은 키에 대한 이 정의가 내가 원하는 것을 수행하지 않는 이유입니다.

답변1

TikZ가 사용하는 설명\nullfont 은 이미 percusse의답변. 따라서 \settowidth예상대로 작동하지 않습니다.

\pgfmathsetlength{width("#1")}

대신에 \settowidth함수 pgfmathwidth사용할 수 있습니다:

\pgfmathsetlength\aetmplength{width("#1")}

전체 예:

\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\newlength\aetmplength

\begin{document}

\begin{tikzpicture}[
  set text to width of/.code={%%
    % \settowidth\aetmplength{#1}%%
    \pgfmathsetlength\aetmplength{width("#1")}%
    \typeout{--->`#1`::\the\aetmplength}%%
    \pgfkeysalso{text width=\the\aetmplength}%%
  },
  ] 
  \node[draw,
        shape=rectangle split,
        rectangle split parts=2,
        set text to width of=This is the top of the node,
        %text width=0.95in
        ] ()
        {   
         This is the top of the node
         \nodepart{two}
           first item\\
           second item\\
           \ldots};
\end{tikzpicture}

\end{document}

결과

\pgftext{...}

또 다른 방법은 \pgftextTeX로 다시 탈출하여 \settowidth다시 작동하는 것입니다. 인수는 \pgftext그룹 내에서 처리되므로 \aetmplength전역적으로 할당되고 \setwidth로컬 임시 치수 레지스터에서 작동합니다. 메모리상의 이유로 로컬 할당과 전역 할당을 동일한 제어 시퀀스에서 혼합하면 안 됩니다.

\pgftext{%
  \settowidth{\dimen0}{#1}%%
  \global\aetmplength=\dimen0\relax
}%

node font

최신 버전을 사용하여 option 값을 존중할 수도 있습니다 node font. 값은 매크로에 저장됩니다 \tikz@node@textfont. \makeatletter및 없이 \makeatother제어 순서는 \csname다음 으로 지정할 수도 있습니다 \endcsname.

\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\newlength\aetmplength

\begin{document}

\begin{tikzpicture}[node font=\tiny,
  set text to width of/.code={%%
    \pgftext{%
      \csname tikz@node@textfont\endcsname
      \settowidth{\dimen0}{#1}%%
      \global\aetmplength=\dimen0\relax
    }%
    \typeout{--->`#1`::\the\aetmplength}%%
    \pgfkeysalso{text width=\the\aetmplength}%%
  },
  ] 
  \node[draw,
        shape=rectangle split,
        rectangle split parts=2,
        set text to width of=This is the top of the node,
        %text width=0.95in
        ] ()
        {   
         This is the top of the node
         \nodepart{two}
           first item\\
           second item\\
           \ldots};
\end{tikzpicture}

\end{document}

노드 글꼴 설정 결과

답변2

TikZ/PGF 디자인은 일반 구문에 허위 공백 누출이 없거나 의도하지 않은 문자가 코드 내부에 인쇄되는 등의 방식으로 수행됩니다. 이를 수행하는 한 가지 방법은 현재 글꼴을 특수 프리미티브에 매핑하는 것입니다 \nullfont.

이로 인해 원래의 비 \nullfont상태를 다시 만들지 않고는 단순히 텍스트 너비를 처리할 수 없습니다. 약간의 맥락을 제공하기 위해 아래는 pgf@picture환경 begin정의에서 가져온 것입니다.

    \begingroup%
      \let\pgf@setlengthorig=\setlength%
      \let\pgf@addtolengthorig=\addtolength%
      \let\pgf@selectfontorig=\selectfont%  <== Records the original
      \let\setlength=\pgf@setlength%
      \let\addtolength=\pgf@addtolength%
      \let\selectfont=\pgf@selectfont% <== Overwrites
      \nullfont\spaceskip0pt\xspaceskip0pt% <== Resets font
      \setbox\pgf@layerbox@main\hbox to0pt\bgroup%
        \begingroup%

이를 알게 되면 그룹 내에서 일시적으로 원래 정의로 돌아가 폭을 확보하고 그룹에서 살아남을 수 있습니다.

\documentclass[border=4pt,tikz]{standalone}
\usetikzlibrary{shapes.multipart}
\makeatletter
\tikzset{set text to width of/.code={%%
\begingroup%
\pgfutil@selectfont%
\settowidth\pgf@xc{#1}% <== PGF internal scratch registers xa,ya,xb,yb,xc,yc
\edef\temp{\noexpand\pgfkeysalso{text width=\the\pgf@xc}}%<- Nonzero
\expandafter\endgroup\temp%%
\typeout{--->`#1`::\the\pgf@xc}%<-Still zero!
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
  \node[draw,
        align=center,%<- for line breaks
        shape=rectangle split,
        rectangle split parts=2,
        set text to width of=This is looooooooonnnnnnnnger than the top,
        ] () 
        {
         This is the top of the node
         \nodepart{two}
           first item\\
           second item\\
           \ldots};
\end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

관련 정보