TransWikia.com

Rounding to the nearest negative integer

TeX - LaTeX Asked on July 27, 2021

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}$?

One Answer

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^{pgfmathprintnumberx}$};
end{tikzpicture}
end{document}

enter image description here

Correct answer by user229669 on July 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP