当需要整数时,对 TikZ 中参数的计算值进行舍入

当需要整数时,对 TikZ 中参数的计算值进行舍入

我无法在下图中获得 x 轴的完整标记(旨在作为参数不同值的模板)。

\documentclass[tikz,margin=.5cm]{standalone}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc} 
\usepackage{siunitx}
    \usetikzlibrary{arrows,calc}
\newcommand{\ProgTemp}[4]% #1=T_amb,#2=T_plateau, #3=rate, #4=t_plateau
{
% grid
\draw[style=help lines, ystep=100, xstep=1, ultra thin, color=pink] (0,0) grid ({#4+(6*((#2-#1)/(#3*60))},{1.2*#2});
% axes
\draw[thick,->] (0,0) -- ({(6*(#2-#1)/(#3*60))+#4},0) node[right] {\huge time (\si{\hour})};
\draw[thick,->] (0,0) -- (0,{1.2*#2}) node[above] {\huge T (\si{\celsius})};
% xticks and yticks
  \foreach \x in {1,2,...,{(6*(#2-#1)/(#3*60))+#4)}}  \node at (\x, 0) [below] {\huge\x};
  \foreach \y in {100,200,...,#2}  \node at (0,\y) [left] {\huge\y};
% temperature
\begin{scope}[ultra thick, red]
\draw (0,#1) -- ({(#2-#1)/(#3*60)},#2);
\draw ({(#2-#1)/(#3*60)},#2) -- ({(#2-#1)/(#3*60)+#4},#2);
\draw[thin, dashed, blue] ({(#2-#1)/(#3*60)+#4},#2) -- ({(1.5*(#2-#1)/(#3*60))+#4},#1);
\draw ({(#2-#1)/(#3*60)+#4},#2) .. controls ({(2.0*(#2-#1)/(#3*60))+#4},{#1+0.2*(#2-#1)}) and ({(5.0*(#2-#1)/(#3*60))+#4},#1) .. ({(6*(#2-#1)/(#3*60))+#4},#1);
\end{scope}
}
\begin{document}
\begin{tikzpicture} [x=2cm,y=0.01cm]
\ProgTemp{20}{500}{10}{3}
\end{tikzpicture}
\end{document}

我怀疑这与值的四舍五入有关,该值应该是整数,但我找不到解决这个问题的方法。我该如何将其转换{(6*(#2-#1)/(#3*60))+#4)}为整数值?

enter image description here

答案1

您可以使用\pgfmathparse{floor(x)} \pgfmathresult

\documentclass[tikz,margin=.5cm]{standalone}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc} 
\usepackage{siunitx}
\usetikzlibrary{arrows,calc}
\newcommand{\ProgTemp}[4]% #1=T_amb,#2=T_plateau, #3=rate, #4=t_plateau
{
    % grid
    \draw[style=help lines, ystep=100, xstep=1, ultra thin, color=gray] (0,0) grid ({#4+(6*((#2-#1)/(#3*60))},{1.2*#2});
    % axes
    \draw[thick,->] (0,0) -- ({(6*(#2-#1)/(#3*60))+#4},0) node[right] {\huge time (\si{\hour})};
    \draw[thick,->] (0,0) -- (0,{1.2*#2}) node[above] {\huge T (\si{\celsius})};
    % xticks and yticks
    \pgfmathparse{floor(6*(#2-#1)/(#3*60))+#4)};
    \foreach \x in {1,2,...,\pgfmathresult}  \node at (\x, 0) [below] {\huge\x};
    \foreach \y in {100,200,...,#2}  \node at (0,\y) [left] {\huge\y};
    % temperature
    \begin{scope}[ultra thick, red]
        \draw (0,#1) -- ({(#2-#1)/(#3*60)},#2);
        \draw ({(#2-#1)/(#3*60)},#2) -- ({(#2-#1)/(#3*60)+#4},#2);
        \draw[thin, dashed, blue] ({(#2-#1)/(#3*60)+#4},#2) -- ({(1.5*(#2-#1)/(#3*60))+#4},#1);
        \draw ({(#2-#1)/(#3*60)+#4},#2) .. controls ({(2.0*(#2-#1)/(#3*60))+#4},{#1+0.2*(#2-#1)}) and ({(5.0*(#2-#1)/(#3*60))+#4},#1) .. ({(6*(#2-#1)/(#3*60))+#4},#1);
    \end{scope}
}
\begin{document}
    \begin{tikzpicture} [x=2cm,y=0.01cm]
        \ProgTemp{20}{500}{10}{3}
    \end{tikzpicture}
\end{document}

enter image description here

相关内容