
我的程式碼是:
\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}