On Wed, 12 Oct 2011, Hans Hagen wrote:
On 12-10-2011 21:43, Otso Helenius wrote:
Hello,
I've been trying to come up with a few methods to add definitions to text with the help of the metafun and context manuals. The first method adds the definition as sort of a "superscript" on top of the word, and the second one marks multiple words with a curly bracket and adds the definition on top of the tip of the bracket.
Here is the (ugly and hackish) code:
\startuniqueMPgraphic{defline} path p; p:=(0,0)..controls (0,1) and (1,1)..(1,1)--(6,1)..controls(7,1) and (7,1)..(7,2)--(7,2)..controls(7,1) and (7,1)..(8,1)--(13,1)..controls(13,1) and (14,1)..(14,0); draw (0,0)--(0,12pt) withcolor white; draw p xscaled (\overlaywidth/15) yscaled 5 shifted (0,9mm) withpen pencircle scaled 0.25mm withcolor red; \stopuniqueMPgraphic
\defineoverlay[curlybracket][\includeMPgraphic{defline}]
\def\sentencedef[#1][#2]{\setbox\scratchbox=\hbox{#2}\hbox to \the\wd\scratchbox{\cbox{\hbox{\tfxx\color[red] #1}\\\hbox{\framed[background=curlybracket,frame=off]{#2}}}}}
\def\worddef[#1][#2]{\setbox\scratchbox=\hbox{#2}\hbox to \the\wd\scratchbox{\cbox{\hbox{\rotate[rotation=25]{\tfxx\color[red] #1}}\\\hbox{#2}}}}
\starttext
\hbox{\worddef[def1][Lorem] ipsum \worddef[def2][dolor] sit \worddef[def3][amet] consectetur \worddef[def4][adipiscing] elit.}
\hbox{\sentencedef[def5][Lorem ipsum] dolor sit \sentencedef[def6][amet consectetur] adipiscing elit.}
\stoptext
I haven't figured a way to add the curly bracket graphic neatly nor position the definition correctly on top of it.
I would be very interested in any advice regarding these two functions and how to rewrite both of them in a more elegant way.
Wikifyable ...
\startuniqueMPgraphic{defline}{width}
path p ; p := ( 0,0) .. controls (0,1) and ( 1,1) .. ( 1,1) -- ( 6,1) .. controls (7,1) and ( 7,1) .. ( 7,2) -- ( 7,2) .. controls (7,1) and ( 7,1) .. ( 8,1) -- (13,1) .. controls (13,1) and (14,1) .. (14,0) ; draw p xysized (\MPvar{width},ExHeight) withpen pencircle scaled .1ExHeight withcolor \MPcolor{red} ;
\stopuniqueMPgraphic
\defineoverlay[curlybracket][\includeMPgraphic{defline}]
\unexpanded\def\WordDef {\dodoubleargument\doWordDef}
\def\doWordDef[#1][#2]% {\dontleavehmode\hbox\bgroup \setbox\scratchbox\hbox{#2}% \raise\strutheight\hbox to \wd\scratchbox{\hss\rotate[rotation=25]{\tfxx\color[red]#1}\hss}% \hskip-\wd\scratchbox \box\scratchbox \egroup}
\unexpanded\def\LineDef {\dodoubleargument\doLineDef}
\def\doLineDef[#1][#2]% {\dontleavehmode\hbox\bgroup \setbox\scratchbox\hbox{#2}% \raise1.5\strutht\hbox to \wd\scratchbox{\hss\rotate[rotation=25]{\tfxx\color[red]#1}\hss}% \hskip-\wd\scratchbox \raise0.9\strutht\hbox to \wd\scratchbox{\uniqueMPgraphic{defline}{width=\the\wd\scratchbox}}% \hskip-\wd\scratchbox \box\scratchbox \egroup}
\starttext
\WordDef[def1][Lorem] ipsum \WordDef[def2][dolor] sit \WordDef[def3][amet] consectetur \WordDef[def4][adipiscing] elit.
\LineDef[def5][Lorem ipsum] dolor sit \LineDef[def6][amet consectetur] adipiscing elit.
\stoptext
Another solution, which might be easier to understand. This does not affect the lineheight. Depending on the usage, that may or may not be desirable. \unprotect \startuniqueMPgraphic{defline} path p ; p := ( 0,0) .. controls (0,1) and ( 1,1) .. ( 1,1) -- ( 6,1) .. controls (7,1) and ( 7,1) .. ( 7,2) -- ( 7,2) .. controls (7,1) and ( 7,1) .. ( 8,1) -- (13,1) .. controls (13,1) and (14,1) .. (14,0) ; draw p xysized (OverlayWidth,ExHeight) shifted (0,OverlayHeight) withpen pencircle scaled .1ExHeight withcolor \MPcolor{\framedparameter\c!framecolor} ; label.top((textext("\tfxx\color[red] \framedparameter\c!text") rotated 25), (OverlayWidth/2, OverlayHeight+ExHeight)); setbounds currentpicture to boundingbox OverlayBox ; \stopuniqueMPgraphic \defineoverlay[curlybracket][\includeMPgraphic{defline}] \defineframed[LineDef] [ \c!location=\v!low, \c!frame=\v!off, \c!background=curlybracket, \c!framecolor=red, ] \protect \starttext \leavevmode\LineDef[text=def5]{Lorem ipsum} dolor sit \LineDef[text=def6]{amet consectetur} adipiscing elit. \stoptext