Beamer 中的方程式、箭頭和註釋

Beamer 中的方程式、箭頭和註釋

我喜歡 tikZ 與 beamer 結合可以突出顯示方程式的方式(它也適用於 beamerarticle 和文章模式),如下所示範例 我經常使用此功能,但我認為程式碼很難「隨時隨地」操作。我想創建一個newcommand(?)\EqNComments(方程式和註釋) 我可以在我的程式碼中輕鬆調用。

    \EqNComments
    {$\Delta$}{=}{Le point Delta}
    {$\mathcal{N}(d_1)$}{+}{Hedge ratio}
    {$K.e^{-rt}.\mathcal{N}(d_2)$}{}{Strike actualised}

在此輸入影像描述

這是與 3x3 元素配合得很好的 MWE。

  1. newcommand我被參數9 的限制所困擾
  2. 我無法輸入 a\foreach來優化我的程式碼
  3. 一定有更聰明的方法來處理這件事...

    \documentclass{beamer}
    %\documentclass[11pt,twoside]{article}
    %\usepackage[envcountsect]{beamerarticle}
    \usetheme{Warsaw}
    \usepackage{amsmath}
    \usepackage{amssymb}
    \usepackage{tikz}
    \usetikzlibrary{arrows,shapes}
    
    \tikzset{
        Col1/.style= {fill=blue!20,anchor=base,rounded corners=2pt},
        Col2/.style= {Col1, fill=red!20},
        Col3/.style= {Col1, fill=green!20},
    }
    
    \newcommand{\EqNComments}[9]
    {
    
    \tikzstyle{every picture}+=[remember picture]
    \tikzstyle{na} = [baseline=-.5ex]
    
    \begin{frame}
        \begin{equation}
                        \tikz[baseline] {\node[Col1] (d1) {#1};}  #2
                        \tikz[baseline] {\node[Col2] (d2) {#4};}  #5
                        \tikz[baseline] {\node[Col3] (d3) {#7};}  #8                    
    %                   \tikz[baseline] {\node[Col3] (d4) {#10};} #11                    
        \end{equation}
    
        \begin{itemize}
            \item #3 
            \tikz[na] \node[coordinate] (s1) {};
            \item #6 
            \tikz[na] \node[coordinate] (s2) {};        
            \item #9  
            \tikz[na] \node[coordinate] (s3) {};
    %       \item #12  
    %       \tikz[na] \node[coordinate] (s4) {};        
        \end{itemize}
    
        \begin{tikzpicture}[overlay]
            \path[->] (s1) edge [bend left]     (d1);
            \path[->] (s2) edge [bend right]    (d2);
            \path[->] (s3) edge [out=0, in=-90] (d3);
    %       \path[->] (s4) edge [out=0, in=-90] (d4);       
        \end{tikzpicture}
    \end{frame}
    } 
    
    \begin{document}
    
    \EqNComments
    {$\Delta$}{=}{Le point Delta}
    {$\mathcal{N}(d_1)$}{+}{Hedge ratio}
    {$K.e^{-rt}.\mathcal{N}(d_2)$}{-}{Strike actualised}
    %{dividendes}{.}{Not working here if I try to extend the command to 12 arguments}
    
    \end{document}
    

答案1

這就是做類似的事情。它使用tikzmark庫,這是一個更好的工具,因為除其他外,它不會破壞間距並檢測我們所處的模式。

\tmn{<quantity>}{<annotation>}

只要您將它們放入AnnotatedEquation環境中,它們就會被清單註釋,類似於您正在做的事情。節點將在定義的範圍內使用 styles 等Col1Col2您可以使用可選參數來添加一些鍵。

\documentclass{beamer} 
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newcounter{mytn}
\makeatletter
\newcommand{\tmn}[3][]{\stepcounter{mytn}%
\tikzmarknode[Col\the\numexpr\value{mytn}-\mytn@start\relax/.try,inner xsep=2pt,%
minimum height=1.6em,#1]{mytn-\number\value{mytn}}{#2}%
\expandafter\gdef\csname tmn@annot@\number\value{mytn}\endcsname{#3}}
\newenvironment{AnnotatedEquation}{\edef\mytn@start{\number\value{mytn}}%
\begin{equation}}{\end{equation}%
\edef\mytn@end{\number\value{mytn}}%
\ifnum\mytn@end>\mytn@start
\begin{itemize}
 \foreach \X in {\the\numexpr\mytn@start+1,...,\mytn@end} 
 {\item \tikzmarknode{mytn-annot-\X}{\csname tmn@annot@\X\endcsname}%
 \begin{tikzpicture}[overlay,remember picture]
  \draw[-stealth] (mytn-annot-\X.east) to[out=0,in=-90] (mytn-\X.south);
 \end{tikzpicture}} 
\end{itemize}
\fi}
\makeatother
\tikzset{ Col1/.style= {fill=blue!20,anchor=base,rounded corners=2pt}, 
Col2/.style= {Col1, fill=red!20}, 
Col3/.style= {Col1, fill=green!20}, }
\begin{document}
\begin{frame}[t]
\frametitle{Some annotated equation}
\begin{AnnotatedEquation}
 \tmn{\Delta}{Le point Delta}= \tmn{\mathcal{N}(d_1)}{Hedge ratio}-
 \tmn{K\cdot \mathrm{e}^{-r\,t}\cdot\mathcal{N}(d_2)}{Strike actualised} 
\end{AnnotatedEquation}
\end{frame}
\begin{frame}[t]
\frametitle{Another annotated equation}
\begin{AnnotatedEquation}
 \tmn{\Delta}{Le point Delta}= \tmn{\mathcal{N}(d_1)}{Hedge ratio}-
 \tmn{K\cdot \mathrm{e}^{-r\,t}\cdot\mathcal{N}(d_2)}{Strike actualised} 
 +\tmn{\xi}{a Weyl spinor}
 +\tmn{\eta}{another Weyl spinor}
\end{AnnotatedEquation}
\end{frame}
\end{document}

在此輸入影像描述

這是一個適用於 的版本external

\documentclass{beamer} 
\usepackage{tikz}
\usetikzlibrary{tikzmark,external}
\newcounter{mytn}
\makeatletter
\newcommand{\tmn}[3][]{\stepcounter{mytn}%
\tikzmarknode[Col\the\numexpr\value{mytn}-\mytn@start\relax/.try,inner xsep=2pt,%
minimum height=1.6em,#1]{mytn-\number\value{mytn}}{#2}%
\expandafter\gdef\csname tmn@annot@\number\value{mytn}\endcsname{#3}}
\newenvironment{AnnotatedEquation}{\edef\mytn@start{\number\value{mytn}}%
\tikzexternaldisable
\begin{equation}}{\end{equation}%
\edef\mytn@end{\number\value{mytn}}%
\ifnum\mytn@end>\mytn@start
\begin{itemize}
 \foreach \X in {\the\numexpr\mytn@start+1,...,\mytn@end} 
 {\item \tikzmarknode{mytn-annot-\X}{\csname tmn@annot@\X\endcsname}%
 \begin{tikzpicture}[overlay,remember picture]
  \draw[-stealth] (mytn-annot-\X.east) to[out=0,in=-90] (mytn-\X.south);
 \end{tikzpicture}} 
\end{itemize}
\fi\tikzexternalenable}
\makeatother
\tikzset{ Col1/.style= {fill=blue!20,anchor=base,rounded corners=2pt}, 
Col2/.style= {Col1, fill=red!20}, 
Col3/.style= {Col1, fill=green!20}, }
\begin{document}
\begin{frame}[t]
\frametitle{Some annotated equation}
\begin{AnnotatedEquation}
 \tmn{\Delta}{Le point Delta}= \tmn{\mathcal{N}(d_1)}{Hedge ratio}-
 \tmn{K\cdot \mathrm{e}^{-r\,t}\cdot\mathcal{N}(d_2)}{Strike actualised} 
\end{AnnotatedEquation}
\end{frame}
\begin{frame}[t]
\frametitle{Another annotated equation}
\begin{AnnotatedEquation}
 \tmn{\Delta}{Le point Delta}= \tmn{\mathcal{N}(d_1)}{Hedge ratio}-
 \tmn{K\cdot \mathrm{e}^{-r\,t}\cdot\mathcal{N}(d_2)}{Strike actualised} 
 +\tmn{\xi}{a Weyl spinor}
 +\tmn{\eta}{another Weyl spinor}
\end{AnnotatedEquation}
\end{frame}
\end{document}

附錄:這是一個更複雜但也更通用的版本,允許調整箭頭。這可以透過使用密鑰來完成special arrow。例如,

\tmn[special arrow={out=90,annotations/out anchor=north}]{\Delta}{Le point Delta}= \tmn{\mathcal{N}(d_1)}{Hedge ratio}

指示箭頭從註釋文字的北邊出來,並以 90 度角離開。預設值為東錨點和 0 度。這在處理分數分子時特別有用。

\documentclass{beamer} 
\usepackage{tikz}
\usetikzlibrary{tikzmark,external}
\newcounter{mytn}
\makeatletter
\newcommand{\tmn}[3][]{\stepcounter{mytn}%
\tikzmarknode[annotations/Col\the\numexpr\value{mytn}-\mytn@start\relax/.try,inner xsep=2pt,%
minimum height=1.6em,#1]{mytn-\number\value{mytn}}{#2}%
\expandafter\gdef\csname tmn@annot@\number\value{mytn}\endcsname{#3}}
\tikzset{special arrow/.code={%
\expandafter\xdef\csname tmn@arrow@style@\number\value{mytn}\endcsname{#1}}}
\newenvironment{AnnotatedEquation}{\edef\mytn@start{\number\value{mytn}}%
\tikzexternaldisable
\begin{equation}}{\end{equation}%
\edef\mytn@end{\number\value{mytn}}%
\ifnum\mytn@end>\mytn@start
\begin{itemize}
 \foreach \X in {\the\numexpr\mytn@start+1,...,\mytn@end} 
 {\item \tikzmarknode{mytn-annot-\X}{\csname tmn@annot@\X\endcsname}%
 \begin{tikzpicture}[overlay,remember picture]
  \ifcsname tmn@arrow@style@\X\noexpand\endcsname
   \draw[-stealth,out=0,in=-90,
    style/.expanded=\csname tmn@arrow@style@\X\noexpand\endcsname] 
    (mytn-annot-\X.\pgfkeysvalueof{/tikz/annotations/out anchor}) 
    to (mytn-\X.\pgfkeysvalueof{/tikz/annotations/in anchor});
  \else
   \draw[-stealth,out=0,in=-90] (mytn-annot-\X.\pgfkeysvalueof{/tikz/annotations/out anchor})
    to (mytn-\X.\pgfkeysvalueof{/tikz/annotations/in anchor});
  \fi
 \end{tikzpicture}} 
\end{itemize}
\fi\tikzexternalenable}
\makeatother
\tikzset{annotations/.cd, 
Col1/.style= {fill=blue!20,anchor=base,rounded corners=2pt}, 
Col2/.style= {annotations/Col1, fill=red!20}, 
Col3/.style= {annotations/Col1, fill=green!20}, 
in anchor/.initial=south,out anchor/.initial=east}
\begin{document}
\begin{frame}[t]
\frametitle{Some annotated equation}
\begin{AnnotatedEquation}
 \tmn{\Delta}{Le point Delta}= \tmn{\mathcal{N}(d_1)}{Hedge ratio}-
 \tmn{K\cdot \mathrm{e}^{-r\,t}\cdot\mathcal{N}(d_2)}{Strike actualised} 
\end{AnnotatedEquation}
\end{frame}
\begin{frame}[t]
\frametitle{Another annotated equation}
\begin{AnnotatedEquation}
 \tmn[special arrow={out=90,annotations/out anchor=north}]{\Delta}{Le point Delta}= \tmn{\mathcal{N}(d_1)}{Hedge ratio}-
 \tmn{K\cdot \mathrm{e}^{-r\,t}\cdot\mathcal{N}(d_2)}{Strike actualised} 
 +\frac{\tmn[special arrow={in=-160,annotations/in anchor=west}]{\xi}{a Weyl spinor}}{\tmn{\eta}{another Weyl spinor}}
\end{AnnotatedEquation}
\end{frame}
\end{document}

在此輸入影像描述

進一步的改進是可能的,但需要實際使用它的使用者的輸入。

相關內容