3

I'd like to apply different styles depending on the height/width of the text inside (before applying any style of course):

enter image description here

For now I defined the 3 styles, but I'm not sure how to have access to the node's content (or why not directly to the height/width of the node's content).

MWE

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\usetikzlibrary{shapes,shapes.geometric,shapes.misc}

\begin{document}

\tikzset{ nodeEmpty/.style={minimum width=.3mm, circle, fill=green}, nodeSmall/.style={minimum width=.3mm, circle, fill=green!70!black}, nodeLong/.style={minimum width=1cm, rounded rectangle, fill=green!50!black, inner xsep=3mm}, nodeAuto/.code={ %%% Goal: apply automatically the good style depending on the width of the text inside. Something like: if empty, apply nodeEmpty, if height is smaller than 2em and if ratio height/width > 0.5, apply nodeSmall, else apply nodeLong. % ??? } }

\begin{tikzpicture} \node[nodeEmpty] at (0,0) {}; \node[nodeSmall] at (2,0) {$\frac{\pi}{2}$}; \node[nodeLong] at (4,0) {$a+b+c+d$}; \end{tikzpicture}

\end{document}

tobiasBora
  • 8,684
  • I'm afraid you'll have to create new commands. – SebGlav Oct 05 '21 at 17:58
  • Really? Even with multiple compilations? Too bad :( People manage to do crazy things with tikz, I tought it was possible https://tex.stackexchange.com/questions/107227/dependent-node-size-in-tikz – tobiasBora Oct 05 '21 at 20:40
  • The post you're refering to is very interesting and could lead to a solution in your case. We have to study that. Someone would probably come with an answer. – SebGlav Oct 05 '21 at 21:19

1 Answers1

2

As suggested it is possible to define a command for the purpose:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\usetikzlibrary{shapes,shapes.geometric,shapes.misc,math,calc}

\begin{document}

\tikzset{ nodeEmpty/.style={minimum width=.3mm, circle, fill=green}, nodeSmall/.style={minimum width=.3mm, circle, fill=green!70!black}, nodeLong/.style={minimum width=1cm, rounded rectangle, fill=green!50!black, inner xsep=3mm}, nodeAuto/.code={ %%% Goal: apply automatically the good style depending on the width of the text inside. Something like: if empty, apply nodeEmpty, if height is smaller than 2em and if ratio height/width > 0.5, apply nodeSmall, else apply nodeLong. % ??? } } \newcommand{\NodeWithVariableStyle}[2]{\tikzmath{ if width("#2") == 0 then {let \st=nodeEmpty;} else {if height("#2") < scalar(2em)&& height("#2")/width("#2") > 0.5 then {let \st=nodeSmall;} else {let \st=nodeLong;}; }; { \node[\st] at #1 {#2}; }; }}%\NodeWithVariableStyle{position}{content} \begin{tikzpicture}

\NodeWithVariableStyle{(0,0)}{} \NodeWithVariableStyle{(2,0)}{$\frac{\pi}{2}$} \NodeWithVariableStyle{(4,0)}{$a+b+c+d$}

\end{tikzpicture} \end{document}

node with variable style

vi pa
  • 3,394
  • Thanks for your answer. I could indeed write a separate command (and it's what I do in practice), but I wanted to use a style also to be compatible with other softwares, like tikzit which only know about styles. Also, creating a new command makes it hard to combine multiple styles. But +1 for the nice usage of tikzmath! – tobiasBora Oct 22 '21 at 07:34