4

The simplified example given below currently uses 11 iterations just for drawing at most 3 stems (vertical segments). It seems that my code is not efficient enough so I want to reduce the number of iteration from 11 to 3. Is it possible?

\documentclass[pstricks,margin=5mm]{standalone}
\usepackage{multido}
\usepackage[nomessages]{fp}

\makeatletter
\newcommand\Ruler[1]{%
\FPeval\Start{trunc(#1*10:0)}%
\FPeval\Stop{trunc(#1+1:1)}%
\bgroup
\psset{xunit=.1\psxunit}
\begin{pspicture}[linecap=1](0,-.5)(10,.5)
    \psline(10,0)
    \multido{\ix=0+1,\i=\Start+1}{11}{%
        \pst@mod{\i}{10}\rem
        \ifnum\rem=0
            \psline(\ix,0)(\ix,9pt)
            \uput[90](\ix,6pt){\the\numexpr\i/10}
        \else
            \pst@mod{\i}{5}\rem
            \ifnum\rem=0
                \psline(\ix,0)(\ix,4pt)
            \fi
        \fi
    }
        \psline[linecolor=red]{<-}(0,-2pt)(0,-10pt)
        \uput[-90](0,-6pt){\textcolor{red}{#1}}
        \psline[linecolor=red]{<-}(10,-2pt)(10,-10pt)
        \uput[-90](10,-6pt){\textcolor{red}{\Stop}}
\end{pspicture}\egroup}
\makeatother

\begin{document}
\Ruler{3.0}
\Ruler{3.1}
\Ruler{3.5}
\Ruler{3.6}
\end{document}

Enter image description here

Enter image description here

Enter image description here

Enter image description here

Edit

\Ruler{x} will draw a truncated ruler of length 1cm starting from x.

  • 2
    Welcome to TeX.SE! Could you please tell us where you got the code you seem to be not entirely happy with? – marmot Jul 15 '18 at 17:30
  • 1
    I understand that. What I would like to know is if you wrote it yourself or copied it from somewhere (which is fine but the source will be useful for writing an answer). And I am wondering if you would also be open to TikZ solutions. – marmot Jul 15 '18 at 17:36
6

I don't think you need to use multido for that.

\documentclass[pstricks,margin=5mm]{standalone}
\usepackage[nomessages]{fp}

\makeatletter
\newcommand\Ruler[1]{%
\FPeval\Start{trunc(#1*10:0)}%
\FPeval\Stop{trunc(#1+1:1)}%
\bgroup
\psset{xunit=.1\psxunit}
\begin{pspicture}[linecap=1](0,-.5)(10,.5)
    \psline(10,0)
    \pst@mod{\Start}{10}\rem
    \FPeval\xStart{trunc(10-\rem:0)}%
    \psline(\xStart,0)(\xStart,9pt)
    \FPeval\xLab{trunc(#1:0)}%
    \uput[90](\xStart,6pt){\the\numexpr\xLab+1}
    \ifnum\rem=0
        \psline(0,0)(0,9pt)
        \psline(10,0)(10,9pt)
        \uput[90](0,6pt){\xLab}
        \psline(5,0)(5,4pt)
    \else
        \ifnum\rem=5
            \psline(0,0)(0,4pt)
            \psline(10,0)(10,4pt)
        \else
            \FPeval\xMid{trunc(10-\rem+5:0)}%
            \pst@mod{\xMid}{10}\xMid%
            \psline(\xMid,0)(\xMid,4pt)
        \fi
    \fi
    \psline[linecolor=red]{<-}(0,-2pt)(0,-10pt)
    \uput[-90](0,-6pt){\textcolor{red}{#1}}
    \psline[linecolor=red]{<-}(10,-2pt)(10,-10pt)
    \uput[-90](10,-6pt){\textcolor{red}{\Stop}}
\end{pspicture}\egroup}
\makeatother

\begin{document}
\Ruler{3.0}
\Ruler{3.1}
\Ruler{3.5}
\Ruler{3.6}
\end{document}

enter image description here

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.