Conditionals within tikz node specification

Conditionals within tikz node specification



I would like to specify custom hierarchies of tikz nodes where I do a few geometry calculations related to text depth, text width, text height, and so on.



In the process of writing custom commands to automate some of these calculations, I end up wanting to use TeX conditionals mixed in with tikz code. I read in another question that pgfextra can be used for this. However, it seems that this only works at the level of the tikzpicture environment.



A minimal example of what I would like to do is the following:


documentclass[class=minimal, border=0pt]standalone

usepackagetikz

newififDebug
%Debugtrue
Debugfalse

newcommandMyNode
node[anchor=north west,
minimum width=5cm,
minimum height=5cm,
fill=green,
pgfextraifDebug opacity=0.2,fi
] at (current page.north west)

begindocument
begintikzpicture
MyNode;
endtikzpicture
enddocument



The line containing pgfextra is giving me trouble. What should I do to conditionally change arguments to node?



Thanks!




2 Answers
2



Welcome to TeX.SE! Please try to avoid all pgfextra stuff. You can achieve almost everything with pgfkeys, also here.


pgfextra


documentclass[class=minimal, border=0pt]standalone

usepackagetikz

newififDebug
%Debugtrue
Debugfalse

tikzsetDebug/.code=ifDebugpgfkeysalsoopacity=0.2fi

newcommandMyNode
node[anchor=north west,
minimum width=5cm,
minimum height=5cm,
fill=green,
Debug,
] at (current page.north west)

begindocument
begintikzpicture
Debugtrue
MyNode;
endtikzpicture
enddocument



enter image description here



If I comment out Debugtrue, I get.


Debugtrue



enter image description here



With a Tikz style (implemented with the /.code handler) this is rather straightforward:


/.code


documentclass[class=minimal, border=0pt]standalone

usepackagetikz

newififDebug
Debugtrue
%Debugfalse

tikzset
my node/.code=
tikzset
anchor=north west,
minimum width=5cm,
minimum height=5cm,
fill=green,

ifDebug
tikzsetopacity=0.2
fi



begindocument
begintikzpicture
node[my node];
Debugfalse
node[my node] at (6,0);
endtikzpicture
enddocument



enter image description here



As mentioned by marmot in his answer you should stay away from pgfextra in general. My personal preference is also to stay away from custom commands where styles can do the same, but that really is personal.


pgfextra


style






You were faster, +1. I don't know why someone would upvote only my answer, but not yours. Sad.

– marmot
Sep 10 '18 at 12:29






@marmot Well if someone thinks your solution is cleaner (which I could agree with) then it's a normal reaction :)

– Max
Sep 10 '18 at 12:33



Thanks for contributing an answer to TeX - LaTeX Stack Exchange!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)