How to make this Metapost diagram adjust to the width of text?
I am using this to create a simple branched diagram, with one item, branches into two, and each of those two further branch into two. I'm running into two problems: (1) The lines cut right through the word "apple" and "banana", I need to place a node at the right of apple and banana that connects to sub-branches. In other words, I'd rather it have a broken line, ending at left of apple and then continuing at right of apple. (2) If you put longer text in the top category, it adjusts. And if you put longer text in the lowest branches, it adjusts, but I can't find a way to get the branches to adjust length to accommodate longer text in the middle, where apple and banana are. I tried using different code to find textwidth, but it isn't working: numeric text_width; text_width = width(textext("#2")); How can I make the diagram adjust if the words in the middle branches are longer? --Joel \starttext % Define the macro outside the Metapost code block \def\macroXa#1#2#3#4#5#6#7 { \startMPcode u := 1.5cm; % Reduced unit size for compact spacing % Main node positions z0 = (0, 0); % Fruit position z1 = (1u, 0); % Horizontal segment from Fruit z2 = (1u, 0.6u); % Apple position z3 = (1u, -0.6u); % Banana position z4 = (3u, 0.9u); % Fuji branch z5 = (3u, 0.3u); % Gala branch z6 = (3u, -0.3u);% Cavendish branch z7 = (3u, -0.9u);% Plantain branch z8 = (4u, 0.9u); % Fuji z9 = (4u, 0.3u); % Gala z10 = (4u, -0.3u); % Cavendish z11 = (4u, -0.9u); % Plantain % Main paths draw z0 -- z1; % Horizontal from Fruit draw z1 -- z2; % Line to Apple (90-degree angle) draw z1 -- z3; % Line to Banana (90-degree angle) % Apple sub-branches draw z2 -- (3u, 0.6u) -- z4; % Right angle to Fuji draw z2 -- (3u, 0.6u) -- z5; % Right angle to Gala % Banana sub-branches draw z3 -- (3u, -0.6u) -- z6; % Right angle to Cavendish draw z3 -- (3u, -0.6u) -- z7; % Right angle to Plantain % Final horizontal branches draw z4 -- z8; % Fuji draw z5 -- z9; % Gala draw z6 -- z10; % Cavendish draw z7 -- z11; % Plantain % Add labels using macro arguments label.lft(btex #1 etex, z0); % Fruit label.rt(btex #2 etex, z2); % Apple label.rt(btex #3 etex, z3); % Banana label.rt(btex #4 etex, z8); % Fuji label.rt(btex #5 etex, z9); % Gala label.rt(btex #6 etex, z10); % Cavendish label.rt(btex #7 etex, z11); % Plantain \stopMPcode } % Call the macro with labels \macroXa{Fruit}{Apple}{Banana}{Fuji}{Gala}{Cavendish}{Plantain} \macroXa{This text is longer}{Apple}{Banana}{Fuji}{Gala}{Cavendish}{Plantain} \macroXa{This text is longer}{Apple}{it needs to accomodate longer text here}{Fuji}{Gala}{Cavendish}{Plantain} \stoptext
Hi Joel, Rather than using label to make and draw the labels at the end, use thelabel at the beginning to make pictures of the labels, then draw the labels at the end. (See mpman.pdf, pp. 21-22, 29.) This allows you to use the dimensions of the labels in calculations for the positions. Gavin \starttext % Define the macro outside the Metapost code block \def\macroXa#1#2#3#4#5#6#7 { \startMPcode u := 1.5cm; % Reduced unit size for compact spacing picture l[]; l0 = thelabel.lft(textext("#1"), origin); % Fruit l2 = thelabel.rt(textext("#2"), origin); % Apple l3 = thelabel.rt(textext("#3"), origin); % Banana l8 := thelabel.rt(textext("#4"), origin); % Fuji l9 := thelabel.rt(textext("#5"), origin); % Gala l10 := thelabel.rt(textext("#6"), origin); % Cavendish l11 := thelabel.rt(textext("#7"), origin); % Plantain % Find required width numeric w[]; w2 := abs(urcorner l2 - ulcorner l2); w3 := abs(urcorner l3 - ulcorner l3); wmax := max(w2, w3); % Main node positions z0 = (0, 0); % Fruit position z1 = (1u, 0); % Horizontal segment from Fruit z2 = (1u, 0.6u); % Apple position z3 = (1u, -0.6u); % Banana position z4 = (2u+wmax, 0.9u); % Fuji branch z5 = (2u+wmax, 0.3u); % Gala branch z6 = (2u+wmax, -0.3u);% Cavendish branch z7 = (2u+wmax, -0.9u);% Plantain branch z8 = (3u+wmax, 0.9u); % Fuji z9 = (3u+wmax, 0.3u); % Gala z10 = (3u+wmax, -0.3u); % Cavendish z11 = (3u+wmax, -0.9u); % Plantain % Main paths draw z0 -- z1; % Horizontal from Fruit draw z1 -- z2; % Line to Apple (90-degree angle) draw z1 -- z3; % Line to Banana (90-degree angle) % Apple sub-branches draw (x2+w2,y2) -- (2u+wmax, 0.6u) -- z4; % Right angle to Fuji draw (x2+w2,y2) -- (2u+wmax, 0.6u) -- z5; % Right angle to Gala % Banana sub-branches draw (x3+w3,y3) -- (2u+wmax, -0.6u) -- z6; % Right angle to Cavendish draw (x3+w3,y3) -- (2u+wmax, -0.6u) -- z7; % Right angle to Plantain % Final horizontal branches draw z4 -- z8; % Fuji draw z5 -- z9; % Gala draw z6 -- z10; % Cavendish draw z7 -- z11; % Plantain % Add labels using macro arguments draw l0 shifted z0; % Fruit draw l2 shifted z2; % Apple draw l3 shifted z3; % Banana draw l8 shifted z8; % Fuji draw l9 shifted z9; % Gala draw l10 shifted z10; % Cavendish draw l11 shifted z11; % Plantain \stopMPcode } % Call the macro with labels \macroXa{Fruit}{Apple}{Banana}{Fuji}{Gala}{Cavendish}{Plantain} \macroXa{This text is longer}{Apple}{Banana}{Fuji}{Gala}{Cavendish}{Plantain} \macroXa{This text is longer}{Apple}{it needs to accommodate longer text here}{Fuji}{Gala}{Cavendish}{Plantain} \stoptext
On Dec 8, 2024, at 7:19 PM, Joel via ntg-context
wrote: I am using this to create a simple branched diagram, with one item, branches into two, and each of those two further branch into two.
I'm running into two problems:
(1) The lines cut right through the word "apple" and "banana", I need to place a node at the right of apple and banana that connects to sub-branches. In other words, I'd rather it have a broken line, ending at left of apple and then continuing at right of apple.
(2) If you put longer text in the top category, it adjusts. And if you put longer text in the lowest branches, it adjusts, but I can't find a way to get the branches to adjust length to accommodate longer text in the middle, where apple and banana are.
I tried using different code to find textwidth, but it isn't working:
numeric text_width; text_width = width(textext("#2"));
How can I make the diagram adjust if the words in the middle branches are longer?
--Joel
\starttext
% Define the macro outside the Metapost code block \def\macroXa#1#2#3#4#5#6#7 { \startMPcode u := 1.5cm; % Reduced unit size for compact spacing
% Main node positions z0 = (0, 0); % Fruit position z1 = (1u, 0); % Horizontal segment from Fruit z2 = (1u, 0.6u); % Apple position z3 = (1u, -0.6u); % Banana position z4 = (3u, 0.9u); % Fuji branch z5 = (3u, 0.3u); % Gala branch z6 = (3u, -0.3u);% Cavendish branch z7 = (3u, -0.9u);% Plantain branch z8 = (4u, 0.9u); % Fuji z9 = (4u, 0.3u); % Gala z10 = (4u, -0.3u); % Cavendish z11 = (4u, -0.9u); % Plantain
% Main paths draw z0 -- z1; % Horizontal from Fruit draw z1 -- z2; % Line to Apple (90-degree angle) draw z1 -- z3; % Line to Banana (90-degree angle)
% Apple sub-branches draw z2 -- (3u, 0.6u) -- z4; % Right angle to Fuji draw z2 -- (3u, 0.6u) -- z5; % Right angle to Gala
% Banana sub-branches draw z3 -- (3u, -0.6u) -- z6; % Right angle to Cavendish draw z3 -- (3u, -0.6u) -- z7; % Right angle to Plantain
% Final horizontal branches draw z4 -- z8; % Fuji draw z5 -- z9; % Gala draw z6 -- z10; % Cavendish draw z7 -- z11; % Plantain
% Add labels using macro arguments label.lft(btex #1 etex, z0); % Fruit label.rt(btex #2 etex, z2); % Apple label.rt(btex #3 etex, z3); % Banana label.rt(btex #4 etex, z8); % Fuji label.rt(btex #5 etex, z9); % Gala label.rt(btex #6 etex, z10); % Cavendish label.rt(btex #7 etex, z11); % Plantain \stopMPcode }
% Call the macro with labels \macroXa{Fruit}{Apple}{Banana}{Fuji}{Gala}{Cavendish}{Plantain} \macroXa{This text is longer}{Apple}{Banana}{Fuji}{Gala}{Cavendish}{Plantain} \macroXa{This text is longer}{Apple}{it needs to accomodate longer text here}{Fuji}{Gala}{Cavendish}{Plantain}
\stoptext
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror) archive : https://github.com/contextgarden/context wiki : https://wiki.contextgarden.net ___________________________________________________________________________________
participants (2)
-
Gavin
-
Joel