我不知道這是否重複,但如果是,我們可以將其關閉。
我一直在努力學習ConTeXt
。我特別喜歡在我下載的文檔中看到的標題和頁碼格式,這是透過使用Metapost
/實現的Metafun
。我一直試圖在\chapter
(透過LaTeX製作)中egreg
實現這種效果gmp
包裹。
為此,我複製並貼上了從以下位置找到的多個文件中的程式碼這裡。這是我現在得到的輸出。
用這個代碼。
\setuppapersize[A4][A4]
\setuplayout[topspace=0.5in, backspace=1in, header=24pt, footer=36pt,
height=middle, width=middle]
\def\MPclipOne#1#2#3#4#5%
{\startuseMPgraphic{clip:one}
w := #1; width := 100; wfactor := w/width;
h := #2; height := 100; hfactor := h/height;
%
color lightred; lightred := (.90,.50,.50);
color lightgray; lightgray := (.95,.95,.95);
color gray; gray := (.50,.50,.50);
%
def random_delta (expr d) =
d-(uniformdeviate 2d)
enddef;
%
z1 = (0,height);
z2 = (0,0);
z3 = (width,0);
z4 = (width,height);
%
z5 = (width+random_delta(.2width),height+random_delta(.2height));
z6 = (.5width+random_delta(.1width),height+random_delta(.1height));
%
pickup pencircle
xscaled (#3/wfactor)
yscaled (#3/(2*hfactor))
rotated 30;
%
draw z5..z1..z2..z3..z4..z6 withcolor #4;
%
pickup pencircle
xscaled (#3/wfactor)
yscaled (#3/hfactor);
%
draw z1 withcolor #5;
draw z2 withcolor #5;
draw z3 withcolor #5;
draw z4 withcolor #5;
draw z5 withcolor #5;
draw z6 withcolor #5;
%
newwidth := (xpart (urcorner currentpicture)) -
(xpart (llcorner currentpicture));
newheight := (ypart (urcorner currentpicture)) -
(ypart (llcorner currentpicture));
%
currentpicture := currentpicture
xscaled (w/newwidth) yscaled (h/newheight);
\stopuseMPgraphic
\useMPgraphic{clip:one}{}}
\defineoverlay
[KopAchtergrond]
[\MPclipOne{\overlaywidth}{\overlayheight}{10pt}{gray}{red}]
\def\HeadCommand#1#2%
{\alignedline{\v!outer}{\v!left}
{\framed
[\c!background=KopAchtergrond,
\c!width=\v!fit,
\c!height=\v!fit,
\c!frame=\v!off,
\c!strut=\v!no,
\c!offset=24pt,
\c!align=\v!middle]
{\doifmode{*sectionnumber}
{#1\kern.5em% strut niet geset, zou wel moeten
\hbox{\color[headrule]{\vrule\!!width1pt\!!height1.5\ht\strutbox\!!depth1.25\dp\strutbox}}%
\kern.5em}%
#2}}}
\setuphead
[\v!chapter]
[\c!command=\HeadCommand,
\v!appendix\c!label=none,
\c!page=,
\c!before=\vskip36pt plus 6pt minus 6pt,
\c!after=\vskip24pt]
\starttext
\chapter{Sample chapter}
\stoptext
請讓我了解這裡發生了什麼以及如何達到我想要的效果?
答案1
您缺少一個\unprotect
..\protect
塊和一個\unexpanded
.
\setuppapersize[A4][A4]
\setuplayout[topspace=0.5in, backspace=1in, header=24pt, footer=36pt,
height=middle, width=middle]
\unprotect
\def\MPclipOne#1#2#3#4#5%
{\startuseMPgraphic{clip:one}
w := #1; width := 100; wfactor := w/width;
h := #2; height := 100; hfactor := h/height;
%
color lightred; lightred := (.90,.50,.50);
color lightgray; lightgray := (.95,.95,.95);
color gray; gray := (.50,.50,.50);
%
def random_delta (expr d) =
d-(uniformdeviate 2d)
enddef;
%
z1 = (0,height);
z2 = (0,0);
z3 = (width,0);
z4 = (width,height);
%
z5 = (width+random_delta(.2width),height+random_delta(.2height));
z6 = (.5width+random_delta(.1width),height+random_delta(.1height));
%
pickup pencircle
xscaled (#3/wfactor)
yscaled (#3/(2*hfactor))
rotated 30;
%
draw z5..z1..z2..z3..z4..z6 withcolor #4;
%
pickup pencircle
xscaled (#3/wfactor)
yscaled (#3/hfactor);
%
draw z1 withcolor #5;
draw z2 withcolor #5;
draw z3 withcolor #5;
draw z4 withcolor #5;
draw z5 withcolor #5;
draw z6 withcolor #5;
%
newwidth := (xpart (urcorner currentpicture)) -
(xpart (llcorner currentpicture));
newheight := (ypart (urcorner currentpicture)) -
(ypart (llcorner currentpicture));
%
currentpicture := currentpicture
xscaled (w/newwidth) yscaled (h/newheight);
\stopuseMPgraphic
\useMPgraphic{clip:one}{}}
\defineoverlay
[KopAchtergrond]
[\MPclipOne{\overlaywidth}{\overlayheight}{10pt}{gray}{red}]
\unexpanded\def\HeadCommand#1#2%
{\alignedline{\v!outer}{\v!left}
{\framed
[\c!background=KopAchtergrond,
\c!width=\v!fit,
\c!height=\v!fit,
\c!frame=\v!off,
\c!strut=\v!no,
\c!offset=24pt,
\c!align=\v!middle]
{\doifmode{*sectionnumber}
{#1\kern.5em% strut niet geset, zou wel moeten
\hbox{\color[headrule]{\vrule\!!width1pt\!!height1.5\ht\strutbox\!!depth1.25\dp\strutbox}}%
\kern.5em}%
#2}}}
\setuphead
[\v!chapter]
[\c!command=\HeadCommand,
\v!appendix\c!label=none,
\c!page=,
\c!before=\vskip36pt plus 6pt minus 6pt,
\c!after=\vskip24pt]
\protect
\starttext
\chapter{Sample chapter}
\stoptext
但這確實是舊的 ConTeXt 程式碼。這裡TeX和Metapost之間的通訊是透過宏參數進行的。我只會使用鍵值參數來通訊。這是一種可能的解決方案,我使用dotcolot
key 來指定點的顏色。
\setupexternalfigures[location={local,global}]
\startuseMPgraphic{randomcircle}
width := 100; wfactor := OverlayWidth/width;
height := 100; hfactor := OverlayHeight/height;
%
color dotcolor; dotcolor := \MPcolor{\framedparameter{dotcolor}};
def random_delta (expr d) =
d-(uniformdeviate 2d)
enddef;
%
z1 = (0,height);
z2 = (0,0);
z3 = (width,0);
z4 = (width,height);
%
z5 = (width, height) randomized (0.2width, 0.2height);
z6 = (0.5width, height) randomized (0.1width, 0.1height);
%
pickup pencircle
xscaled (OverlayLineWidth/wfactor)
yscaled (OverlayLineWidth/(2*hfactor))
rotated 30;
%
draw z5..z1..z2..z3..z4..z6 withcolor OverlayLineColor;
%
pickup pencircle
xscaled (OverlayLineWidth/wfactor)
yscaled (OverlayLineWidth/hfactor);
%
draw z1 withcolor dotcolor;
draw z2 withcolor dotcolor;
draw z3 withcolor dotcolor;
draw z4 withcolor dotcolor;
draw z5 withcolor dotcolor;
draw z6 withcolor dotcolor;
%
newwidth := (xpart (urcorner currentpicture)) -
(xpart (llcorner currentpicture));
newheight := (ypart (urcorner currentpicture)) -
(ypart (llcorner currentpicture));
%
currentpicture := currentpicture
xscaled (OverlayWidth/newwidth) yscaled (OverlayHeight/newheight);
\stopuseMPgraphic
\defineoverlay
[randomcircle]
[\useMPgraphic{randomcircle}]
現在,覆蓋randomcircle
可以與任何接受覆蓋的命令一起使用。例如,一個框架和一個圖形:
這是使用以下方法獲得的:
\definecolor[gray][r=0.5, g=0.5, b=0.5]
\starttext
\startTEXpage[offset=10mm]
\dontleavehmode
\framed[background=randomcircle, dotcolor=red, framecolor=gray, offset=24pt, rulethickness=10pt, frame=off]
{ Hello World}
\hskip 6em
\externalfigure[cow.pdf]
[width=3cm,
background=randomcircle, dotcolor=red, framecolor=gray,
backgroundoffset=24pt, rulethickness=10pt, frame=off]
\stopTEXpage
\stoptext
現在,取得節標題周圍的圖形只需設定command
的鍵即可\setuphead
。