TikZ 数学运算宏

TikZ 数学运算宏

我有以下代码

\documentclass[12pt,oneside]{book}

\setlength{\parindent}{0em}
\setlength{\parskip}{1em}
\usepackage[a4paper,bindingoffset=0.2in,%
left=0.5in,right=0.5in,top=1in,bottom=1in,%
footskip=.25in]{geometry}
\usepackage{fourier}
\usepackage{amsfonts,amsmath,amssymb,amsthm}
\usepackage{mathrsfs,mathtools}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc,arrows,arrows.meta,shapes,tikzmark,decorations.markings,decorations.pathreplacing}
\usepackage[makeroom]{cancel}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{array}
\usepackage{caption}
\usepackage{float}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{enumitem}

\usepackage{xcolor,colortbl}
\usepackage[colorlinks=true,breaklinks,plainpages=false,linkcolor=blue!50!black,urlcolor=blue!50!black,citecolor=blue!50!black,bookmarks=true,bookmarksopenlevel=2]{hyperref}


\usepackage{fancyhdr}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\cfoot{\thepage}

\begin{document}
\frontmatter
\pagestyle{empty}

\begingroup
\addtolength{\jot}{1em}
\[
\begin{aligned}
&\quad\left(
\tikz[baseline,remember picture]{\node[fill=green!20,anchor=base] (t1){$ AX $};}\pm
\tikz[baseline,remember picture]{\node[fill=blue!20,anchor=base] (t2){$ CY $};}\right)\left(
\tikz[baseline,remember picture]{\node[fill=cyan!20,anchor=base] (n1){$ EW $};}\pm
\tikz[baseline,remember picture]{\node[fill=magenta!20,anchor=base] (n2){$ GZ $};}\right)\\
\end{aligned}
\]
\endgroup

\begin{tikzpicture}[remember picture,overlay] %% use here too
\draw[green,thick,->] (t1.north) to [bend left] (n1.north);
\draw[green,thick,->] (t1.north) to [bend left] (n2.north);
\draw[blue,thick,->] (t2.south) to [bend right] (n2.south);
\draw[blue,thick,->] (t2.south) to [bend right] (n1.south);
\end{tikzpicture}


\end{document}

产生

在此处输入图片描述

该输出可以显示两个二项式之间的分配性质。现在,我想知道是否有可能创建一个宏来实现类似的输出,无论表达式中涉及多少个项,即单项式、二项式、三项式等。

提前致谢。

答案1

这个答案包含明确的结果,不应该向孩子展示(正如@egreg在他的评论中所提到的)。

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{positioning}
\tikzset{
  from/.style={fill={red!\col!blue}},
  to/.style={fill={green!\col!red}},
  symb/.style={inner sep=0},
  node distance=2pt
}
% --------------------
\def\froms{A,B,C}
\def\tos{D,...,G}
% --------------------
\begin{document}
  \begin{tikzpicture}
    % -------- FIRST PART
    \path
      foreach[
        count=\i,
        remember=\i as \j (initially 0),
        evaluate={\col=30*\i}
      ] \from in \froms{
        \ifnum \i > 1
          node[right=of F\j,symb] (O){$\pm$}
        \else
          node[symb](O){$($}
        \fi
        node[right=of O,from] (F\i){$\from$}
      }
      \pgfextra{\xdef\numfrom{\j}}
    ;
    % -------- SECOND PART
    \path
      foreach[
        count=\i,
        remember=\i as \j (initially 0),
        evaluate={\col=20*\i}
      ] \from in \tos{
        \ifnum \i > 1
          node[right=of T\j,symb] (O){$\pm$}
        \else
          node[right=of F\numfrom,symb](O){$)($}
        \fi
        node[right=of O,to] (T\i){$\from$}
      }
      node[right=of T\j,symb] (O){$)$}
      \pgfextra{\xdef\numto{\j}}
    ;
    % -------- ARROWS PART
    \foreach[evaluate={\coli=30*\i}] \i in {1,...,\numfrom}
      \foreach[evaluate={\colj=15*\j}] \j in {1,...,\numto}
      \path ([xshift=\numto pt-2*\j pt]F\i.north) edge[draw={green!\colj!red!\coli!blue},out=30+10*\j,in=130-5*\i,-latex] ([xshift=3*\i pt-1.5*\numfrom pt]T\j.north);

  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容