使用 \mpcolor 的背景颜色

使用 \mpcolor 的背景颜色

我正在使用以下代码,我想让背景颜色与 beamer 样式的颜色相对应。我尝试了该命令,background:=\mpcolor{block body example.bg};但它不起作用。

\documentclass[10pt]{beamer}
\usepackage{xcolor,luamplib}
\usetheme{Boadilla}
\begin{document}
\begin{frame}
\begin{exampleblock}{}\centering\leavevmode
\begin{mplibcode}
input mptrees;
beginfig(1)
dirtree:=-90;
typeprob:=2;
% background:=\mpcolor{block body example.bg};
draw tree[1][1](1.25cm,5cm)("P","$\frac{1}{2}$","F","$\frac{1}{2}$");
draw tree[2][1](1.25cm,1.5cm)("R","$\frac{1}{5}$","B","$\frac{1}{5}$","N","$\frac{3}{5}$");
draw tree[2][2](1.25cm,1.5cm)("R","$\frac{1}{5}$","B","$\frac{1}{5}$","N","$\frac{3}{5}$");
endfig;
\end{mplibcode}
\end{exampleblock}
\end{frame}
\end{document}

beamer-luamplib

答案1

由于该\mpcolor命令被设计为在withcolor运算符之后使用,因此它不适用于简单的赋值情况,例如background。但是,xcolor包提供了一个非常有用的命令\extractcolorspecs,我们可以在 OP 的示例中使用它,如下所示:

\documentclass[10pt]{beamer}
\usepackage{luamplib}
\usetheme{Boadilla}
\begin{document}
\mplibforcehmode
\begin{frame}
  \begin{exampleblock}{}\centering
    \extractcolorspecs{block body example.bg}\modelcmd\colorcmd
    \begin{mplibcode}
      input mptrees;
      beginfig(1)
      dirtree:=-90;
      typeprob:=2;
      background:=(\colorcmd);
      draw tree[1][1](1.25cm,5cm)("P","$\frac{1}{2}$","F","$\frac{1}{2}$");
      draw tree[2][1](1.25cm,1.5cm)("R","$\frac{1}{5}$","B","$\frac{1}{5}$","N","$\frac{3}{5}$");
      draw tree[2][2](1.25cm,1.5cm)("R","$\frac{1}{5}$","B","$\frac{1}{5}$","N","$\frac{3}{5}$");
      endfig;
    \end{mplibcode}
  \end{exampleblock}
\end{frame}
\end{document}

在第 8 行,\modelcmd得到为rgb\colorcmd得到为0.9,0.95,0.9(xcolor 手册 2.13)。因此,后者放在括号中即可变成 metapost 颜色表达式。

答案2

使用 TikZ,我们可以在路径上放置更多节点。我认为使用 Metapost 可以做类似的事情,或者最好转移到 TikZ ^^

在此处输入图片描述

\documentclass{beamer}
\usepackage{tikz}
\usetheme{Boadilla}
\begin{document}
\begin{frame}
\begin{exampleblock}{}\centering\leavevmode
\begin{tikzpicture}[yscale=2,xscale=1.5,font=\sffamily]
\path
(0,0)    coordinate (O)
(-2,-1)  node (F) {F} 
+(0,-1)  node (B1) {B}
+(-1,-1) node (N1) {N}
+(1,-1)  node (R1) {R}
(2,-1)   node (P) {P}
+(0,-1)  node (B2) {B}
+(-1,-1) node (N2) {N}
+(1,-1)  node (R2) {R};

\path 
(O)--(F) node[pos=.6] (OF) {$\frac{1}{2}$}
(O)--(P) node[pos=.6] (OP) {$\frac{1}{2}$}
(F.south)--(N1) node[pos=.6] (FN1) {$\frac{3}{5}$}
(F.south)--(B1) node[pos=.6] (FB1) {$\frac{1}{5}$}
(F.south)--(R1) node[pos=.6] (FR1) {$\frac{1}{5}$}
(P.south)--(N2) node[pos=.6] (PN2) {$\frac{3}{5}$}
(P.south)--(B2) node[pos=.6] (PB2) {$\frac{1}{5}$}
(P.south)--(R2) node[pos=.6] (PR2) {$\frac{1}{5}$}; 

\draw 
(O)--(OF) (OF)--(F) 
(O)--(OP) (OP)--(P)
(F.south)--(FB1) (FB1)--(B1) 
(F.south)--(FN1) (FN1)--(N1) 
(F.south)--(FR1) (FR1)--(R1)
(P.south)--(PB2) (PB2)--(B2)
(P.south)--(PN2) (PN2)--(N2)
(P.south)--(PR2) (PR2)--(R2);
\end{tikzpicture}
\end{exampleblock}
\end{frame}
\end{document}

相关内容