图表中的垂直间距

图表中的垂直间距

考虑以下 MWE:

\documentclass[letterpaper]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{lipsum}
\usetikzlibrary{tikzmark,calc}
\usepackage{amsmath}
\begin{document}
    \lipsum[1-2]
    \[
        \tikzmarknode{base}{2}^{\tikzmarknode{exp}{3}}=2\cdot 2\cdot 2
    \]
    \begin{tikzpicture}[remember picture,overlay]
        \draw[stealth-,OrangeRed] (base)-- ($ (base.south west) + (-2ex,0) $) node[inner sep=1pt,left,font=\footnotesize\sffamily,OrangeRed] {base};
        \draw[stealth-,Cerulean] (exp) -- ($ (base.north east) + (2ex,2ex) $) node[inner sep=0pt, above right,font=\footnotesize\sffamily,Cerulean] {exponent};
    \end{tikzpicture}
    \lipsum[1]
\end{document}

在此处输入图片描述

如何确保图表上方和下方的间距合适?也就是说,我希望上方间距尊重上方和下方的数学间距,而不会渗入上方的文本。

此外,如果有可能或更好的方法来标记图表,我将不胜感激。

答案1

测量距离并将其相加。

\documentclass[letterpaper]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{lipsum}
\usetikzlibrary{tikzmark,calc}
\usepackage{amsmath}
\begin{document}
    \lipsum[1-2]
    \[
        \tikzmarknode{base}{2}^{\tikzmarknode{exp}{3}}=2\cdot 2\cdot 2
    \begin{tikzpicture}[remember picture,baseline={(aux.base)}]
      \begin{scope}[overlay]
        \draw[stealth-,OrangeRed] (base)-- ($ (base.south west) + (-2ex,0) $)
         node[inner sep=1pt,left,font=\footnotesize\sffamily,OrangeRed] (aux1){base};
        \draw[stealth-,Cerulean] (exp) -- ($ (base.north east) + (2ex,2ex) $) 
        node[inner sep=0pt, above right,font=\footnotesize\sffamily,Cerulean] 
        (aux2){exponent};
      \end{scope}       
      \path node(aux){\vphantom{.}}
        let \p1=($(aux1.north)-(aux.base)$),
            \p2=($(aux1.south)-(aux.base)$),
            \p3=($(aux2.north)-(aux.base)$),
            \p4=($(aux2.south)-(aux.base)$) in 
            foreach \z in {1,...,4} {(0,\y\z)};
    \end{tikzpicture}
    \]
    \lipsum[1]
\end{document}

在此处输入图片描述

也可以使用fit来稍微缩短代码。(这甚至押韵。;-)

\documentclass[letterpaper]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{lipsum}
\usetikzlibrary{tikzmark,calc,fit}
\usepackage{amsmath}
\begin{document}
    \lipsum[1-2]
    \[
        \tikzmarknode{base}{2}^{\tikzmarknode{exp}{3}}=2\cdot 2\cdot 2
    \begin{tikzpicture}[remember picture,baseline={(aux.base)}]
      \begin{scope}[overlay]
        \draw[stealth-,OrangeRed] (base)-- ($ (base.south west) + (-2ex,0) $)
         node[inner sep=1pt,left,font=\footnotesize\sffamily,OrangeRed] (aux1){base};
        \draw[stealth-,Cerulean] (exp) -- ($ (base.north east) + (2ex,2ex) $) 
        node[inner sep=0pt, above right,font=\footnotesize\sffamily,Cerulean] 
        (aux2){exponent};
        \node[fit=(aux1)(aux2),inner sep=0pt](auxf){};
      \end{scope}       
      \path node(aux){\vphantom{.}}
        let \p1=($(auxf.north)-(aux.base)$),
            \p2=($(auxf.south)-(aux.base)$) in 
            (0,\y1) (0,\y2);
    \end{tikzpicture}
    \]
    \lipsum[1]
\end{document}

相关内容