对于每个循环,当我不希望它显示小数点时

对于每个循环,当我不希望它显示小数点时

我的代码是:

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[timeSlot/.style={draw, rectangle, minimum size =1cm}]
        \foreach [count=\x] [evaluate=\x as \xx using \x-1] \val in {J1, J2}
        {
            \node (node\x) [timeSlot] at (\x,0) {\val};
            \node[above] at (node\x.north west) {\xx};
        }
        \node[above] at (node2.north east) {2};
        \draw[ultra thick] (node1.south west) rectangle (node2.north east);
\end{tikzpicture}
\end{document}

输出为:

分数输出

我不希望上面的输出中有小数点。我希望它们只是 0、1 和 2。我做错了什么?我该如何解决?

答案1

在这种情况下,一个简单的方法就是用来int()获取一个整数。

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[timeSlot/.style={draw, rectangle, minimum size =1cm}]
  \foreach [count=\x] [evaluate=\x as \xx using int(\x-1)] \val in {J1, J2}
  {
    \node (node\x) [timeSlot] at (\x,0) {\val};
    \node[above] at (node\x.north west) {\xx};
  }
  \node[above] at (node2.north east) {2};
  \draw[ultra thick] (node1.south west) rectangle (node2.north east);
\end{tikzpicture}
\end{document}

整数

答案2

可以foreach简化并使用count=\x不需要任何转换的整数()来避免小数。

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[timeSlot/.style={draw, rectangle, minimum size =1cm}]
        \foreach [count=\x from 0] \val in {J1, J2}
        {
            \node (node\x) [timeSlot] at (\x,0) {\val};
            \node[above] at (node\x.north west) {\x};
        }
        \node[above] at (node1.north east) {2};
        \draw[ultra thick] (node0.south west) rectangle (node1.north east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容