
Mein Code ist:
\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}
Die Ausgabe ist:
Ich möchte in der obigen Ausgabe keinen Dezimalpunkt. Ich möchte, dass es einfach 0, 1 und 2 sind. Was mache ich falsch? Wie behebe ich das?
Antwort1
Eine einfache Möglichkeit besteht in diesem Fall darin, einfach zu verwenden, int()
um eine Ganzzahl zu erhalten.
\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}
Antwort2
Dies foreach
kann vereinfacht werden und eine Ganzzahl ( count=\x
) verwenden, bei der keine Konvertierung zur Vermeidung von Dezimalstellen erforderlich ist.
\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}