5

It's my first time asking here for help so please don't roast me.
I'm writing some macros to autodetect and mark peaks in a diagram (in tikz/pgfplots) using \ifdim. The problem is, I have two conditions to incorporate with this: peak is >615pt and <650pt but I can't get it to work. Below is the code I'm currently using and it works fine for marking peaks above one threshold (in this case 615pt). (the code is more or less copied from here Original Code)

\pgfplotsset{
/tikz/max node/.style={mark=none},
mark max/.style={
    point meta rel=per plot,
    visualization depends on={x \as \xvalue},
    scatter/@pre marker code/.code={
        \ifdim\pgfplotspointmetatransformed pt>615pt           
           \def\markopts{mark=none}
            \node[coordinate ,pin = {[rotate=-45]left,inner sep=3pt:{\pgfmathprintnumber[fixed,precision=2,zerofill=true]{\xvalue}}}] at (0,1) {};
        \else
            \def\markopts{mark=none}
        \fi
        \expandafter\scope\expandafter[\markopts]
    },
    scatter/@post marker code/.code={%
        \endscope
    },
    scatter
}}

Is it even possible to use \ifdim for multiple conditions? I would love having something like: \ifdim\pgfplotspointmetatransformed pt>615pt AND \ifdim\pgfplotspointmetatransformed pt<650pt

8

As Steven said, you can use

  \ifdim\mydim>615pt\relax
    \ifdim\mydim<650pt\relax
      in range%
    \else
      above 650%
    \fi
  \else
    below 615%
  \fi

If the code for the two out of range cases is the same and you don't want to duplicate it then you can structure it as

\ifdim\ifdim\mydim>615pt\mydim\else\maxdimen\fi<650pt\relax
  in range%
\else
  out of range%
\fi
  • Oh, I like that! – Steven B. Segletes Jan 13 '17 at 14:18
  • Thanks alot for the fast help. It solved my entire problem. Thanks to Steven as well! – Thomas Krause Jan 13 '17 at 14:50
3

Nest them.

\documentclass{article}
\newcommand\mytest{%
  \ifdim\mydim>615pt\relax%
    \ifdim\mydim<650pt\relax%
      in range%
    \else%
      above 650%
    \fi%
  \else%
    below 615%
  \fi}
\begin{document}
\newlength\mydim
\mydim=630pt\relax
\mytest

\mydim=500pt\relax
\mytest

\mydim=700pt\relax
\mytest
\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.