我想制作一张包含数字的 tikzpicture。它看起来像这样:
我的问题是,我不知道如何去掉每个数字后面的 .0。有人能帮我得到整数而不是浮点数吗?
以下是 tikzpicture 的简短代码片段:
\documentclass[a4paper, 11pt]{book}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
% First Layer
\edef\mya{0}
\foreach \x in {0.5,1.25,...,4.25}{
\draw[] (0,\x - 0.5) rectangle (1.5,\x);
\pgfmathparse{\mya + 1}
\xdef\mya{\pgfmathresult}
\node[] at(0.75,\x-0.25){\mya};
\draw[->] (1.5,\x-0.25) -> (3,\x-0.25);
}
% Second Layer
\edef\a{0}
\edef\b{0}
\foreach \x in {1.25,2.75,4.25}{
\draw[] (3,\x - 1.25) rectangle (4.5,\x);
\pgfmathparse{\b + 1}
\xdef\a{\pgfmathresult}
\pgfmathparse{\a + 1}
\xdef\b{\pgfmathresult}
\node[] at(3.75,\x-0.6333){[\a,\b]};
\draw[->] (4.5,\x-0.6333) -> (6.0,\x-0.6333);
}
\draw[] (6,0) rectangle (7.5, 4.25);
\node[] at(6.75,2.1){[1.0,6.0]};
\draw[] (-0.5, 2.85) rectangle (5, 4.75);
\node[] at(1.25,4.5){Generalisierung};
\draw[] (-0.5,4.75)rectangle (7.75, -0.5);
\node[] at(6.75,4.5){Hierarchie};
\node[] at(0.75,-0.25){Stufe 0};
\node[] at(3.75,-0.25){Stufe 1};
\node[] at(6.75,-0.25){Stufe 2};
\end{tikzpicture}
\end{center}
\end{document}
答案1
解决方案非常简单。在 pgfmath 中,可以显式地对数字进行类型转换,例如\pgfmathparse{int(\b+1)}
。生成的代码如下所示:
\documentclass[a4paper, 11pt]{book}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
% First Layer
\edef\mya{0}
\foreach \x in {0.5,1.25,...,4.25}{
\draw[] (0,\x - 0.5) rectangle (1.5,\x);
\pgfmathparse{int(\mya + 1)}
\xdef\mya{\pgfmathresult}
\node[] at(0.75,\x-0.25){\mya};
\draw[->] (1.5,\x-0.25) -> (3,\x-0.25);
}
% Second Layer
\edef\a{0}
\edef\b{0}
\foreach \x in {1.25,2.75,4.25}{
\draw[] (3,\x - 1.25) rectangle (4.5,\x);
\pgfmathparse{int(\b + 1)}
\xdef\a{\pgfmathresult}
\pgfmathparse{int(\a + 1)}
\xdef\b{\pgfmathresult}
\node[] at(3.75,\x-0.6333){[\a,\b]};
\draw[->] (4.5,\x-0.6333) -> (6.0,\x-0.6333);
}
\draw[] (6,0) rectangle (7.5, 4.25);
\node[] at(6.75,2.1){[1,6]};
\draw[] (-0.5, 2.85) rectangle (5, 4.75);
\node[] at(1.25,4.5){Generalisierung};
\draw[] (-0.5,4.75)rectangle (7.75, -0.5);
\node[] at(6.75,4.5){Hierarchie};
\node[] at(0.75,-0.25){Stufe 0};
\node[] at(3.75,-0.25){Stufe 1};
\node[] at(6.75,-0.25){Stufe 2};
\end{tikzpicture}
\end{center}
\end{document}