在 tikz 节点中强制十位小数

在 tikz 节点中强制十位小数

我正在尝试在 tikz 节点中显示值,0.5但下面的 MWE 一直显示0。我在 Google 上搜索了所有内容;我该如何实现它!?

\documentclass{article}

\usepackage{tikz}
\usepackage{pgf}

\begin{document}


\begin{tikzpicture}[scale=0.6]

  \begin{scope}[xscale=15/4,yscale=5/60]

    \foreach \ee [remember=\b as \b (initially 0), ] in{0,...,4}{
      \ifnum\ee=0 \pgfmathtruncatemacro{\b}{0.5} \fi %<----- Fix this, make it show 0.5
      \ifnum\ee=1 \pgfmathtruncatemacro{\b}{5} \fi
      \ifnum\ee=2 \pgfmathtruncatemacro{\b}{50} \fi
      \ifnum\ee=3 \pgfmathtruncatemacro{\b}{500} \fi
      \ifnum\ee=4 \pgfmathtruncatemacro{\b}{5000} \fi
      \draw (\ee,0)node[anchor=south]{\pgfmathprintnumber{\b}};
    }

  \end{scope}

\end{tikzpicture}

\end{document}

答案1

\pgfmathtruncatemacro是一个存储整数值的 pgf 函数。

对于实数(而不仅仅是整数),命令的替代方法是:\pgfmathsetmacro

这样你的例子就变成了:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgf}

\begin{document}


\begin{tikzpicture}[scale=0.6]

  \begin{scope}[xscale=15/4,yscale=5/60]

    \foreach \ee [remember=\b as \b (initially 0), ] in{0,...,4}{
      \ifnum\ee=0 \pgfmathsetmacro{\b}{0.5} \fi %<----- Fix this, make it show 0.5 %Changed only here!!!
      \ifnum\ee=1 \pgfmathtruncatemacro{\b}{5} \fi
      \ifnum\ee=2 \pgfmathtruncatemacro{\b}{50} \fi
      \ifnum\ee=3 \pgfmathtruncatemacro{\b}{500} \fi
      \ifnum\ee=4 \pgfmathtruncatemacro{\b}{5000} \fi
      \draw (\ee,0)node[anchor=south]{\pgfmathprintnumber{\b}};
    }

  \end{scope}

\end{tikzpicture}

\end{document}

答案2

请注意,您可以更简单地计算\ee循环次数并指定\b。例如,

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.6]
  \begin{scope}[xscale=15/4,yscale=5/60]
    \foreach \b [count=\ee from 0] in {0.5,5,50,500,5000}{
      \draw (\ee,0) node[anchor=mid] {\pgfmathprintnumber{\b}};
    }
  \end{scope}
\end{tikzpicture}
\end{document}

数字

我不知道这是否有用,因为您的实际示例无疑更复杂。但是,\ifnum ... \fi无论如何,用 来处理每个值可能是一种繁琐的方法。

相关内容