1

I am drawing an axis with tikz as follows

\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}

Which outputs

enter image description here

This works fine as long as \x is a positive integer. But as you can see, the first tick on the ax is labeled $10^{-1.0}$. How can I round the exponent in this sketch to the nearest integer so that the first tick is labeled $10^{-1}$?

1 Answer 1

2

You can use \pgfmathprintnumber to take care of that.

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .