1

How can I put abs inside a coordinate calculation in TikZ? I have tried the following but it doesn't work (see lines 4 and 5 inside the MWE).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\asd}[1]{\node[circle,draw] at ($(1+#1,0)$) {B};} %this works
\newcommand{\zxc}[1]{\node[circle,draw] at ($abs(#1),0$) {B};} %this doesn't
\begin{document}
\begin{tikzpicture}
\node[circle,draw] at (1,0) {A};
\zxc{-2}
\end{tikzpicture}
\end{document}
  • 1
    ($({abs(#1)},0)$) seems to work, ($abs(#1),0$) would never work anyway, as you are missing the inner ()'s – daleif Sep 23 '16 at 15:20
  • @daleif Sorry - I had removed the inner () in my attempts to find a solution before posting here. I confirm that throwing in the extra pair of braces works (but I have no idea why...); if you post this suggestion as an answer I will be happy to accept it. – Federico Poloni Sep 23 '16 at 15:28
1

You will need to use ($({abs(#1)},0)$), firstly we of course need () inside the coordinate calculation. Secondly in {abs(#1)}, the {}'s are needed to ensure that the expression is passed on to evaluation and thus does not croak the parser that might be looking for ()'s and |- etc

  • 1
    Only the curly braces are needed, ({(abs(#1)},0), to keep the parentheses balanced, when TeX reads the coordinate. – Heiko Oberdiek Sep 23 '16 at 17:40

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.