TeX - LaTeX Asked on March 2, 2021
In some cases I create some (plenty of) buttons of the kind
defmyshow{1}% 1 'yes', '0' no
and then ifnummyshow=1 .... fi
But I had to hear, this is not so correct.
So: What is the correct (and unproblematic) way to create a button like that? Maybe something with pgfkeys
?
documentclass[border=2mm, tikz]{standalone}
usepackage{tikz}
begin{document}
defmyshow{1}% 1 'yes', '0' no
begin{tikzpicture}[]
coordinate[label=A](A) at (0,0);
coordinate[label=right:B](B) at (2,0);
coordinate[label=C](C) at (2,1);
ifnummyshow=1
draw[red, thick] (A) -- (B) -- (C) --cycle;
else% nothing
fi
end{tikzpicture}
end{document}
You should under no circumstances try to overwrite commands like show
, meaning
. There is also no need for that. Apart from the /.is if
key that could be combined with gernot's comment there is the /.is choice
key handler. Here is yet another way which avoids the namespace problems and works with ifnum
. Under the hood tikz
works a bit like this, too.
documentclass[border=2mm, tikz]{standalone}
usepackage{tikz}
begin{document}
pgfkeys{/cis/.cd,show/.initial=1}
begin{tikzpicture}[]
coordinate[label=A](A) at (0,0);
coordinate[label=right:B](B) at (2,0);
coordinate[label=C](C) at (2,1);
ifnumnumexprpgfkeysvalueof{/cis/show}=1
draw[red, thick] (A) -- (B) -- (C) --cycle;
else% nothing
fi
end{tikzpicture}
begin{tikzpicture}[/cis/show=0]
coordinate[label=A](A) at (0,0);
coordinate[label=right:B](B) at (2,0);
coordinate[label=C](C) at (2,1);
ifnumnumexprpgfkeysvalueof{/cis/show}=1
draw[red, thick] (A) -- (B) -- (C) --cycle;
else% nothing
fi
end{tikzpicture}
end{document}
As for /.is if
.
documentclass[border=2mm, tikz]{standalone}
usepackage{tikz}
begin{document}
newififcisshow
tikzset{cis/.cd,show/.is if=cisshow,show/.default=true,show=true}
begin{tikzpicture}[]
coordinate[label=A](A) at (0,0);
coordinate[label=right:B](B) at (2,0);
coordinate[label=C](C) at (2,1);
ifcisshow
draw[red, thick] (A) -- (B) -- (C) --cycle;
else% nothing
fi
end{tikzpicture}
begin{tikzpicture}[cis/show=false]
coordinate[label=A](A) at (0,0);
coordinate[label=right:B](B) at (2,0);
coordinate[label=C](C) at (2,1);
ifcisshow
draw[red, thick] (A) -- (B) -- (C) --cycle;
else% nothing
fi
end{tikzpicture}
end{document}
As mentioned already, /.is choice
is another option, but this makes most sense if the options are not just Boolean.
Correct answer by user231225 on March 2, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP