![修改 \foreach 循环中计算节点的小数点](https://linux22.com/image/387567/%E4%BF%AE%E6%94%B9%20%5Cforeach%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E8%AE%A1%E7%AE%97%E8%8A%82%E7%82%B9%E7%9A%84%E5%B0%8F%E6%95%B0%E7%82%B9.png)
使用以下代码从答案这个问题
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}
答案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}
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}