我有一个通过调用 4 个函数生成的元字体图,我知道我将使用不同的参数多次使用这些函数。
为了避免疯狂地复制粘贴,我希望将这些函数放在自己的文件中,并且仅在相关或必要时将它们包含在 tex 文件中。
但是,现在我甚至不知道如何让它们成为自己的命令,更不用说从其他文件中包括它们了:
结果:
代码:
\documentclass[border=10cm]{standalone}
\usepackage{luamplib}
\mplibnumbersystem{double}
\usepackage[margin=0.5cm]{geometry}
\begin{document}
{\centering
\begin{mplibcode}
u:=1cm;
% Draw a lattice layer upside down
% parameters are: horizontal offset, level (height), thickness of % the lines, color of the lines
vardef inverted_layer(expr n,l,s,c)=
%declare variables
save parent, lc, rc;
pair parent, lc, rc;
parent:=(n, l);
% assign values of left and child nodes, forming a 'v' pattern
lc := (n-1, l+1);
rc := (n+1, l+1);
draw u*parent--u*rc withpen pencircle scaled s withcolor c;
draw u*parent--u*lc withpen pencircle scaled s withcolor c;
enddef;
% Draw and inverted lattice
% parameters are: horizontal offset, number of layers, thickness
% of the lines, color of the lines
vardef inverted_lattice(expr n,l, size, color)=
for i=0 upto l:
for j=0 upto i:
inverted_layer((j + n)*2 - i, i-(l+1), size, color);
endfor;
endfor;
enddef;
% Similar as above except the lattice isn;t upside down
vardef layer(expr n,l,s,c)=
save parent, lc, rc;
pair parent, lc, rc;
parent:=(n, l);
lc := (n-1, l-1);
rc := (n+1, l-1);
draw u*parent--u*rc withpen pencircle scaled s withcolor c;
draw u*parent--u*lc withpen pencircle scaled s withcolor c;
enddef;
vardef lattice(expr n,l, size, color)=
for i=0 upto l:
for j=0 upto i:
layer((j + n)*2 - i, -i, size, color);
endfor;
endfor;
enddef;
% Start figure
beginfig(0);
% Create labels for the bottom level
for i=-3 upto 7:
save j;
numeric j;
j := i - 2;
% No plus symbol for egatives
if j<0:
label.top(textext("\huge$K_{i"& decimal j &"}$"), (i*u*2 + 1*u,-6*u));
% no arithmetic symbols for 0
elseif j=0:
label.top(textext("\huge$K_{i}$"), (i*u*2+1*u,-6*u));
% regular labeling
else:
label.top(textext("\huge$K_{i+"& decimal j &"}$"), (i*u*2+1*u,-6*u));
fi
endfor;
for i=0 upto 5:
%create labels for the top level
if i-3<0:
label.top(textext("\huge$C_{i"& decimal(i-3)&"}$"), (i*u*2,0));
elseif i-3=0:
label.top(textext("\huge$C_{i}$"), (i*u*2,0));
else:
label.top(textext("\huge$C_{i+"& decimal(i-3) &"}$"), (i*u*2,0));
fi
% draw 5 regular lattices in black at different offsets
% so that they partially overlap
lattice(i,4,1, black);
endfor;
% draw the inverted red lattice with thick lines
inverted_lattice(2.5,4,3, red);
z0=u*(2,0);
z1=u*(3,-1);
z2=u*(2,-2);
z3=u*(1,-3);
z4=u*(0,-4);
z5=u*(1,-5);
z6=u*(2,-6);
% draw the blue path
draw z0--z1--z2--z3--z4--z5--z6 withpen pencircle scaled 3bp withcolor blue;
for i=0 upto 6:
fill fullcircle scaled 4bp shifted z[i];
endfor;
endfig;
\end{mplibcode}
\par}
\end{document}
答案1
您可以使用input
命令来执行此操作,即在您希望它们可用的环境input myfilewithmymacros.mp;
开始时执行此操作。如果您经常这样做,那么您可以使用命令(如下所示)在每个环境开始时输入文件。我还启用了标签,因此不需要该命令。 mplibcode
\everymplib
mplibcode
textext
\textext
\documentclass{article}
\begin{filecontents*}{makogandefs.mp}
% Draw a lattice layer upside down
% parameters are: horizontal offset, level (height), thickness of % the lines, color of the lines
vardef inverted_layer(expr n,l,s,c)=
%declare variables
save parent, lc, rc;
pair parent, lc, rc;
parent:=(n, l);
% assign values of left and child nodes, forming a 'v' pattern
lc := (n-1, l+1);
rc := (n+1, l+1);
draw u*parent--u*rc withpen pencircle scaled s withcolor c;
draw u*parent--u*lc withpen pencircle scaled s withcolor c;
enddef;
% Draw and inverted lattice
% parameters are: horizontal offset, number of layers, thickness
% of the lines, color of the lines
vardef inverted_lattice(expr n,l, size, color)=
for i=0 upto l:
for j=0 upto i:
inverted_layer((j + n)*2 - i, i-(l+1), size, color);
endfor;
endfor;
enddef;
% Similar as above except the lattice isn;t upside down
vardef layer(expr n,l,s,c)=
save parent, lc, rc;
pair parent, lc, rc;
parent:=(n, l);
lc := (n-1, l-1);
rc := (n+1, l-1);
draw u*parent--u*rc withpen pencircle scaled s withcolor c;
draw u*parent--u*lc withpen pencircle scaled s withcolor c;
enddef;
vardef lattice(expr n,l, size, color)=
for i=0 upto l:
for j=0 upto i:
layer((j + n)*2 - i, -i, size, color);
endfor;
endfor;
enddef;
\end{filecontents*}
\usepackage{luamplib}
\everymplib{input makogandefs.mp;}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
% Start figure
beginfig(0);
u:=1cm;
% Create labels for the bottom level
for i=-3 upto 7:
save j;
numeric j;
j := i - 2;
% No plus symbol for egatives
if j<0:
label.top("\huge$K_{i"& decimal j &"}$", (i*u*2 + 1*u,-6*u));
% no arithmetic symbols for 0
elseif j=0:
label.top("\huge$K_{i}$", (i*u*2+1*u,-6*u));
% regular labeling
else:
label.top("\huge$K_{i+"& decimal j &"}$", (i*u*2+1*u,-6*u));
fi
endfor;
for i=0 upto 5:
%create labels for the top level
if i-3<0:
label.top("\huge$C_{i"& decimal(i-3)&"}$", (i*u*2,0));
elseif i-3=0:
label.top("\huge$C_{i}$", (i*u*2,0));
else:
label.top("\huge$C_{i+"& decimal(i-3) &"}$", (i*u*2,0));
fi
% draw 5 regular lattices in black at different offsets
% so that they partially overlap
lattice(i,4,1, black);
endfor;
% draw the inverted red lattice with thick lines
inverted_lattice(2.5,4,3, red);
endfig;
\end{mplibcode}
\end{document}
编译完上述内容后,请尝试以下文档:
\documentclass{article}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
input makogandefs.mp;
beginfig(0);
u:=1cm;
inverted_lattice(2.5,4,3, red);
endfig;
\end{mplibcode}
\end{document}
如果您厌倦了input makogandefs.mp;
在每个块的开头打字mplibcode
,那么\everymplib
可以像上面一样使用命令,它将自动插入。