\pgfmathprintnumber 和预定义样式

\pgfmathprintnumber 和预定义样式

在文本模式下,我想使用\pgfmathprintnumber统一的样式打印数字。所用的样式应在文档的序言中定义。请参阅以下 MWE:

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
   textnumber/.style={
     fixed,
     use comma,
     fixed zerofill,
     precision=4,
     1000 sep={.},
     },
  }

\begin{document}
  The deviation from the origin is \pgfmathprintnumber[textnumber]{10.008}\,\%.
\end{document}

不幸的是,这给了我以下错误:

! Package pgfkeys Error: I do not know the key '/pgf/number format/textnumber'
and I am goinf to ignore it. Perhaps you misspelled it.

我现在有点困惑,不知道该把样式定义放在哪里textnumber。你能给我提示一下吗?

答案1

\pgfplotsset/pgfplots/设置键的前缀并\pgfmathprintnumbers使用前缀/pgf/number format/

因此,您的定义textnumber需要 explizit 前缀/pgf/number format/

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
   /pgf/number format/textnumber/.style={
     fixed,
     use comma,
     fixed zerofill,
     precision=4,
     1000 sep={.},
     },
  }

\begin{document}
  The deviation from the origin is \pgfmathprintnumber[textnumber]{10.008}\,\%.
\end{document}

或者您必须使用并在\pgfmathprintnumber[/pgfplots/textnumber]{...}所有选项前加上前缀:textnumber/pgf/number format/

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
   textnumber/.style={
     /pgf/number format/.cd,% <- changes the prefix for the following options
     fixed,
     use comma,
     fixed zerofill,
     precision=4,
     1000 sep={.},
     },
  }

\begin{document}
  The deviation from the origin is \pgfmathprintnumber[/pgfplots/textnumber]{10.008}\,\%.
\end{document}

结果:

在此处输入图片描述


更新

正如@percusse 在评论中提到的那样,您也可以使用\pgfkeys\pgfplotsset那么您至少需要软件包pgfkeyspgfmaths以及pgfcore您的示例。

\documentclass{article}
\usepackage{pgfkeys,pgfmath,pgfcore}% or pgf or tikz or pgfplots

\pgfkeys{
   /pgf/number format/textnumber/.style={
     fixed,
     use comma,
     fixed zerofill,
     precision=4,
     1000 sep={.},
     },
  }

\begin{document}
  The deviation from the origin is \pgfmathprintnumber[textnumber]{10.008}\,\%.
\end{document}

或者

\documentclass{article}
\usepackage{pgfkeys,pgfmath,pgfcore}

\pgfkeys{
   /textnumber/.style={
     /pgf/number format/.cd,% <- changes the prefix for the following options
     fixed,
     use comma,
     fixed zerofill,
     precision=4,
     1000 sep={.},
     },
  }

\begin{document}
  The deviation from the origin is \pgfmathprintnumber[/textnumber]{10.008}\,\%.
\end{document}

相关内容