修改 \foreach 循环中计算节点的小数点

修改 \foreach 循环中计算节点的小数点

使用以下代码从答案这个问题

0.10 个刻度怎么会显示为 1.0

如何让 x 轴黑色刻度看起来像红色刻度,但中间刻度却显示为空

如下图所示

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{beta}
\begin{tikzpicture}[scale=.86, transform shape]
\pgfmathsetmacro{\incrmnty}{.5}
\pgfmathsetmacro{\incrmntx}{.68}
\pgfmathsetmacro{\inc}{1}
\draw [thick,-](0,0) -- (7,0) node [pos=.5, yshift=-1.8cm] {p};
\draw [thick,-](0,0) -- (0,6) node [pos=.5, xshift=-1.4cm, rotate=90] {Beta};
\foreach \y [evaluate=\y as \aff using int(\y)]in {0,...,10}{
\draw [black, yshift=.6cm] (-.2,\incrmnty*\y) node [left] {0.\aff} -- +(0:.2);}

\foreach \x [evaluate= \x as \aff using int(\x)]in {0,...,10}{
\draw [black, xshift=.2cm] (\incrmntx*\x,0) -- +(-90:.2) node [below] {0.\aff};}

\foreach \x [evaluate= \x as \aff using int(\x)]in {0,2,...,10}{
\draw [red, xshift=.2cm, yshift=-.8cm] (\incrmntx*\x,0) -- +(-90:.2) node [below] {0.\aff};}

\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

enter image description here

答案1

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{xintfrac}
\begin{document}
\begin{frame}[t]
\frametitle{beta}
\begin{tikzpicture}[scale=.86, transform shape]
\pgfmathsetmacro{\incrmnty}{.5}
\pgfmathsetmacro{\incrmntx}{.68}
\pgfmathsetmacro{\inc}{1}
\draw [thick,-](0,0) -- (7,0) node [pos=.5, yshift=-1.8cm] {p};
\draw [thick,-](0,0) -- (0,6) node [pos=.5, xshift=-1.4cm, rotate=90] {Beta};
\foreach \y [evaluate=\y as \aff using int(\y)]in {0,...,10}{
  \draw [black, yshift=.6cm] (-.2,\incrmnty*\y) node [left] 
  {\xintDecToString{\aff[-1]}} -- +(0:.2);
}

\foreach \x [evaluate= \x as \aff using int(\x)]in {0,...,10}{
  \draw [black, xshift=.2cm] (\incrmntx*\x,0) -- +(-90:.2) node [below]
   {\ifodd\aff\space\else\xintDecToString{\aff[-1]}\fi};
}


\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

0.0获取和不获取稍微复杂一些0(使用时xintfrac肯定不是这里最自然的方式,但我知道这种方式......)

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{xintfrac}
\begin{document}
\begin{frame}[t]
\frametitle{beta}
\begin{tikzpicture}[scale=.86, transform shape]
\pgfmathsetmacro{\incrmnty}{.5}
\pgfmathsetmacro{\incrmntx}{.68}
\pgfmathsetmacro{\inc}{1}
\draw [thick,-](0,0) -- (7,0) node [pos=.5, yshift=-1.8cm] {p};
\draw [thick,-](0,0) -- (0,6) node [pos=.5, xshift=-1.4cm, rotate=90] {Beta};
\foreach \y [evaluate=\y as \aff using int(\y)]in {0,...,10}{
  \draw [black, yshift=.6cm] (-.2,\incrmnty*\y) node [left] 
  {\ifnum\aff=0 0.0\else\xintDecToString{\aff[-1]}\fi} -- +(0:.2);
}

\foreach \x [evaluate= \x as \aff using int(\x)]in {0,...,10}{
  \draw [black, xshift=.2cm] (\incrmntx*\x,0) -- +(-90:.2) node [below]
   {\ifodd\aff\space\else\ifnum\aff=0 0.0\else\xintDecToString{\aff[-1]}\fi\fi};
}


\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

相关内容