第一个例子按预期打印了 0 到 3 的整数,但第二个示例将所有输出更改为十进制,尽管计算仅涉及整数。我该如何避免这种情况?
代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\title{Example}
\author{}
\date{October 5th, 2022}
\begin{document}
\maketitle
%First
\begin{tikzpicture}
\foreach \i in {0,...,3}
\draw (\i,0) node {$\pgfmathprint{\i}$};
\end{tikzpicture}
%Second
\begin{tikzpicture}
\foreach \i in {0,...,3}
\draw (\i,0) node {$\pgfmathprint{\i+1}$};
\end{tikzpicture}
\end{document}
输出:
0 1 2 3
1.0 2.0 3.0 4.0
答案1
尝试这个:
\documentclass{article}
\usepackage{tikz,siunitx}
\sisetup{round-mode = places, round-precision = 0} % fix # of decimal places
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,3}
\pgfmathsetmacro{\j}{\i+1}
\draw (\i,0) node {\num{\j}};
\end{tikzpicture}
\end{document}
编辑 如果需要一个或多个小数位,请修改一行代码:
\sisetup{round-mode = places, round-precision = 1} % fix # of decimal places