我不知道这是否是重复的,但如果是的话,我们可以关闭它。
我一直在尝试学习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
键来指定点的颜色。
\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
。