pgfplots:如果字体大小发生变化,如何获取标签的正确高度

pgfplots:如果字体大小发生变化,如何获取标签的正确高度

我设置font=\Hugeylabel
使用 ,\pgfkeysvalueof{/pgfplots/ylabel}我得到了标签文本。
但是,在使用 测量高度时,如何获得正确的值
\pgfmathsetlengthmacro\yLabelHeight{height("\pgfkeysvalueof{/pgfplots/ylabel}")}
有没有办法用 pgfkeys 读出它?

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
\section{Graph}
\begin{tikzpicture}[]
\begin{axis}[
ylabel={yLabel Huge},
y label style={draw, font=\Huge,   },
xmin=0,
%clip=false
]
\addplot[]{x};
\pgfmathsetlengthmacro\yLabelHeight{height("\pgfkeysvalueof{/pgfplots/ylabel}")}
\node[anchor=south west, fill=yellow, font=\small] at (0,0) {
\pgfkeysvalueof{/pgfplots/ylabel} has a height \yLabelHeight (wrong!)};
\end{axis}
\end{tikzpicture}
\subsection{Normalsize}
\pgfmathsetlengthmacro\yLabelHeight{height("yLabel Huge")} 
"yLabel Huge" has a height \yLabelHeight
\subsection{Huge}
\pgfmathsetlengthmacro\yLabelHeight{height("\Huge yLabel Huge")} 
{\Huge "yLabel Huge"} has a height \yLabelHeight
\end{document}

答案1

您可以保存节点的字体样式(\tikz@node@textfont\tikz@textfont)。

在此处输入图片描述

\documentclass[tikz, border=1cm]{standalone}
\makeatletter
\tikzset{
  savefont/.style={
    execute at begin node={%
      \begingroup%
      \toks\z@=\expandafter{\tikz@node@textfont}%
      \toks\@ne=\expandafter{\tikz@textfont}%
      \xdef#1{\the\toks\z@\the\toks\@ne}%
      \endgroup%
    }
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
\node[font=\Huge, savefont=\myfont] {abd};
\node[font=\myfont] at (0, 2) {abd};
\end{tikzpicture}
\end{document}

相关内容