metapost注释

metapost注释

Metapost 是一款功能强大的绘图工具,但不确定它是否比 tikz 更适合注释。是否可以使用 metapost 绘制此注释图?

tikz代码:

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\tikzstyle{boxa} = [top color=white,bottom color=blue!20!]
\tikzstyle{boxb} = [top color=white,bottom color=red!20!]
\tikzstyle{curly} = [decorate,decoration={brace,amplitude=10pt},xshift=0pt,yshift=2pt]                 
% Input:
%   #1 Line offset (optional)
%   #2 Line angle
%   #3 line start offset
%   #4 Line end offset
%   #5 Line label
% Example:
%   \lineann[1]{30}{1}{4}{$L_1$}
\newcommand{\lineann}[5][0.5]{%
    \begin{scope}[rotate=#2,blue,inner sep=2pt]
        \draw[dashed, blue!40] (#3,0) -- +(0,#1)
            node [coordinate, near end] (a) {};
        \draw[dashed, blue!40] (#4,0) -- +(0,#1)
            node [coordinate, near end] (b) {};
        \draw[|<->|] (a) -- node[fill=white] {#5} (b);
    \end{scope}
}
\begin{document}

\begin{tikzpicture}[scale=1]
\def\dx{2.0}
\def\dy{.5}
\filldraw[thick,boxa] (0,0) rectangle node{$P_1$} +(\dx,\dy);
\filldraw[thick,boxa] (\dx,0) rectangle node{$P_2$} +(\dx,\dy);
\filldraw[thick,boxa] (2*\dx,0) rectangle node{$P_3$} +(\dx,\dy);
\filldraw[thick,boxb] (3*\dx,0) rectangle node{$P_4$} +(\dx,\dy);

\draw [curly] (0,\dy)--(4*\dx,\dy) node [black,midway,yshift=14pt] {\footnotesize $T_0$};
\lineann[-10pt]{0}{3*\dx}{4*\dx}{\footnotesize $T_1$}
\end{tikzpicture}
\end{document}

输出:

在此处输入图片描述

答案1

当然,“更容易”取决于您的观点和经验,但 MP 中没有根本的限制来阻止它绘制这种类型的图表。

在此处输入图片描述

与其他工具相比,普通 MP 内置的复杂功能较少,但从某种程度上来说,这让生活变得更简单,因为需要学习的内容更少。

下面我展示了两种不同的方法。对于括号标签,我创建了一个brace_label函数,对于箭头标签,我刚刚绘制了它。创建函数需要做更多的工作,但可以使主图源更容易理解。

我也使用过纯色。但是,如果您真的想要渐变色,没有什么可以阻止您添加渐变色 - 您只需要绘制足够多的条纹,逐渐从一种颜色变为另一种颜色 - 这并不难,只是在纯 MP 中有点乏味 - 但是,如果您经常想要这样做,您可以定义(或找到)一个例程来为您执行此操作。

prologues := 3;
outputtemplate := "%j%c.eps";

vardef brace_label(expr s,a,b) text t_ = 
   save m,theta; pair m;
   theta := angle (b-a); m := .5[a,b]+(10 up rotated theta);
   label.top(s,m) rotatedabout(m, theta) t_;
   draw a+(2 up rotated theta) { dir (60+theta) }
       .. a+((6,6) rotated theta)
       -- m+((-6,-4) rotated theta)
       .. { dir (60+theta) } m { dir (theta-60) } 
       .. m+((+6,-4) rotated theta)
       -- b+((-6,6) rotated theta)
       .. { dir (theta-60) } b+(2 up rotated theta) 
       withpen pencircle scaled .4 t_;
enddef;

beginfig(1);

dx = 2cm; dy = dx/4;
path b[];
for i=1 upto 4: 
  b[i] = unitsquare xscaled dx yscaled dy shifted ((i-1)*dx,0); 
  fill b[i] withcolor .2[white, if i=4: red else: blue fi];
  draw b[i];
endfor

label(btex $P_1$ etex, center b1);
label(btex $P_2$ etex, center b2);
label(btex $P_3$ etex, center b3);
label(btex $P_4$ etex, center b4);

brace_label(btex $\scriptstyle T_0$ etex, ulcorner b1, urcorner b4);

drawoptions(withcolor .67 blue);
ahlength := 3;
path a; a = (llcorner b4 -- lrcorner b4) shifted 6 down;
drawdblarrow a;
draw llcorner b4 -- point 0 of a dashed withdots scaled .3;
draw lrcorner b4 -- point 1 of a dashed withdots scaled .3;
picture t; t = thelabel(btex $\scriptstyle T_1$ etex, point 1/2 of a);
unfill bbox t; draw t;
drawoptions();

endfig;
end.

相关内容