
總結一下:我想在Asymptote畫下圖。
我的程式碼如下。
size(5cm);
draw(scale(8, 4)*unitsquare);
label("$\underbrace{123}+\underbrace{456}=\underbrace{789}$", (4, 3));
draw((2.1, 2.5)--(1, 1), Arrow);
draw((3.95, 2.5)--(4, 1), Arrow);
draw((5.9, 2.5)--(7, 1), Arrow);
label("\strut$a$", (1, 1.1), S);
label("\strut$b$", (4, 1.1), S);
label("\strut$c$", (7, 1.1), S); // \strut is used to make the baselines horizontally aligned.
問題:在這 3draw
行中,箭頭起點的 x 座標是硬編碼的。
如何從支架尖端的位置自動計算箭頭起點的位置?
(能夠計算標籤的寬度會有所幫助,但我也不知道如何。)
類似於tikzmark
中所示的功能https://tex.stackexchange.com/a/145696/250119會有所幫助,但我不知道 Asymptote 中有任何此類功能。
答案1
部分解決方案。有點改編自https://sourceforge.net/p/asymptote/discussion/409349/thread/3fc73fb8/。
如果你仔細想想,標籤相對於使用者座標的大小直到程式最後才確定——這肯定會導致問題。所以,這只是一個部分解決方案它假設unitsize
是固定的。
基本上,它建構了 5 個標籤對象,然後將每個對象放入 a 中picture
,然後測量其大小min()/max()/size()
以決定框底部邊緣的中點。
注意,為了使min()
etc.傳回正確的值,unitsize()
必須正確設定子圖的值。
很遺憾,這種方法破壞了間距---我不知道如何解決它。
var unitsize=1cm;
unitsize(unitsize);
string template="$\underbrace{1}$";
picture a;
unitsize(a, unitsize);
// draw the labels, and compute the coordinate of the bottom points along the way
label(a, baseline("$\underbrace{123}$", template), (0, 0), align=NE);
pair bottom1=((min(a, user=true).x+max(a, user=true).x)/2, min(a, user=true).y);
label(a, baseline("$+$", template), (max(a, user=true).x, 0), align=NE);
real u=max(a, user=true).x;
label(a, baseline("$\underbrace{456}$", template), (max(a, user=true).x, 0), align=NE);
pair bottom2=((u+max(a, user=true).x)/2, min(a, user=true).y);
label(a, baseline("$=$", template), (max(a, user=true).x, 0), align=NE);
real u=max(a, user=true).x;
label(a, baseline("$\underbrace{789}$", template), (max(a, user=true).x, 0), align=NE);
pair bottom3=((u+max(a, user=true).x)/2, min(a, user=true).y);
// compute shift such that the text is centered at x = 4
pair ashift=(4-size(a, user=true).x/2, 3);
// draw the text on currentpicture
add(shift(ashift)*a);
// some other text for comparison
label("$\underbrace{123}+\underbrace{456}=\underbrace{789}$", (4, 4), align=N);
label("$123+456=789$", (4, 5), align=N);
// draw the arrows
draw(ashift+bottom1--(1, 1), Arrow);
draw(ashift+bottom2--(4, 1), Arrow);
draw(ashift+bottom3--(7, 1), Arrow);
label(baseline("$a$"), (1, 1.1), S);
label(baseline("$b$"), (4, 1.1), S);
label(baseline("$c$"), (7, 1.1), S);