1

I have the following MWE

\documentclass{article}
\usepackage{tikz}


\begin{document}

\begin{tikzpicture}[{x=(1mm, 0)},{y=(0,1mm)}]
%-> DEFINITIONS
\def\plthgt{50}                         % plot height
\pgfmathsetmacro\pltstp{\plthgt/4}      % plot step value

%-> DRAWING THE PLOT
\draw[thick]
    (0, 0)--
        (0, \plthgt)
    ;

\foreach \y in {0, \pltstp, ..., \plthgt}
    \pgfmathsetmacro\yvalue{4000*\y/\plthgt}
    \draw
        (0, \y)--
            (-2, \y)
            node[left]{\yvalue}
        ;
\end{tikzpicture}



\end{document}

I am trying to draw this:

enter image description here

I get something quite close when I do \pgfmathsetmacro\yvalue{4*\y/\plthgt} and node[left]{\yvalue 00}, with the inconvenient printing of 0.000 and the remaining values with a . separator.

Any solotuion for this case? Thank you!

1

Just change the order of operations. That is, compute the quotient first and then multiply:

\pgfmathtruncatemacro\yvalue{4000*(\y/\plthgt)}

In order to get integers, use \pgfmathtruncatemacro.

\documentclass{article}
\usepackage{tikz}


\begin{document}

\begin{tikzpicture}[{x=(1mm, 0)},{y=(0,1mm)}]
%-> DEFINITIONS
\def\plthgt{50}                         % plot height
\pgfmathsetmacro\pltstp{\plthgt/4}      % plot step value

%-> DRAWING THE PLOT
\draw[thick]
    (0, 0)--
        (0, \plthgt)
    ;

\foreach \y in {0, \pltstp, ..., \plthgt}
    \pgfmathtruncatemacro\yvalue{4000*(\y/\plthgt)}
    \draw
        (0, \y)--
            (-2, \y)
            node[left]{\yvalue}
        ;
\end{tikzpicture}



\end{document}

enter image description here

If you want to go to higher values, you need to make extra efforts such as

\documentclass{article}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic} 

\begin{document}

\begin{tikzpicture}[{x=(1mm, 0)},{y=(0,1mm)}]
%-> DEFINITIONS
\def\plthgt{50}                         % plot height
\pgfmathsetmacro\pltstp{\plthgt/4}      % plot step value

%-> DRAWING THE PLOT
\draw[thick]
    (0, 0)--
        (0, \plthgt)
    ;
\begin{scope}[/pgf/fixed point arithmetic]
\foreach \y in {0, \pltstp, ..., \plthgt}
 {
    \pgfmathtruncatemacro\yvalue{40000*(\y/\plthgt)}
    \draw
        (0, \y)--
            (-2, \y)
            node[left]{\yvalue}
        ;}
\end{scope}     
\end{tikzpicture}

\end{document}
  • It works! Thank you! – Brasil May 8 at 21:28
  • I got a new problem. Why doesn't work when I try doing \pgfmathtruncatemacro\yvalue{40000*(\y/\plthgt)}?! – Brasil May 8 at 21:38
  • 1
    @Brasil If you want to go to even larger values, you need to load extra stuff to deal with it. Options include \usetikzlibrary{fixedpointarithmetic} (which requires the fp package), \usetikzlibrary{fpu} or some external packages like xfp. Which of those are you familiar/comfortable with? – user121799 May 8 at 21:43
  • I known none of them. I am still new to latex world. But I will try these you just suggest. Thank you again! :) – Brasil May 8 at 21:44
  • 1
    @Brasil I added an explicit example that works. – user121799 May 8 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.