我正在用 tikz 绘制一个轴,如下所示
\documentclass[12pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\xorigin}{-1}
\pgfmathsetmacro{\yorigin}{0}
\pgfmathsetmacro{\xend}{2}
\pgfmathsetmacro{\xstep}{0}
\pgfmathsetmacro{\ticksize}{0.1}
\coordinate (origin) at (\xorigin, \yorigin);
\coordinate (absend) at (\xend, \yorigin);
\draw [->] (origin) -- (absend);
\foreach \x in {\xorigin, \xstep,...,\xend}
\draw (\x, \yorigin + 0.5*\ticksize) -- (\x, \yorigin - \ticksize)
node[anchor=north] {$10^{\x}$};
\end{tikzpicture}
\end{document}
输出
只要\x
是正整数,这种方法就很好用。但如您所见,斧头上的第一个刻度标记为 $10^{-1.0}$。我怎样才能将此草图中的指数四舍五入为最接近的整数,以便第一个刻度标记为 $10^{-1}$?
答案1
您可以用\pgfmathprintnumber
它来处理该事情。
\documentclass[12pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\xorigin}{-1}
\pgfmathsetmacro{\yorigin}{0}
\pgfmathsetmacro{\xend}{2}
\pgfmathsetmacro{\xstep}{0}
\pgfmathsetmacro{\ticksize}{0.1}
\coordinate (origin) at (\xorigin, \yorigin);
\coordinate (absend) at (\xend, \yorigin);
\draw [->] (origin) -- (absend);
\foreach \x in {\xorigin, \xstep,...,\xend}
\draw (\x, \yorigin + 0.5*\ticksize) -- (\x, \yorigin - \ticksize)
node[anchor=north] {$10^{\pgfmathprintnumber\x}$};
\end{tikzpicture}
\end{document}