TransWikia.com

Vertical spacing in diagram

TeX - LaTeX Asked on April 6, 2021

Consider the following 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}}=2cdot 2cdot 2
    ]
    begin{tikzpicture}[remember picture,overlay]
        draw[stealth-,OrangeRed] (base)-- ($ (base.south west) + (-2ex,0) $) node[inner sep=1pt,left,font=footnotesizesffamily,OrangeRed] {base};
        draw[stealth-,Cerulean] (exp) -- ($ (base.north east) + (2ex,2ex) $) node[inner sep=0pt, above right,font=footnotesizesffamily,Cerulean] {exponent};
    end{tikzpicture}
    lipsum[1]
end{document}

enter image description here

How can I ensure proper spacing above and below the diagram? That is, I would like the overly to respect the math spacing above and below and not bleed out into the text above.

Also if there are possible or better ways of labeling the diagram I would appreciate it.

One Answer

Measure the distances and add them.

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}}=2cdot 2cdot 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=footnotesizesffamily,OrangeRed] (aux1){base};
        draw[stealth-,Cerulean] (exp) -- ($ (base.north east) + (2ex,2ex) $) 
        node[inner sep=0pt, above right,font=footnotesizesffamily,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,yz)};
    end{tikzpicture}
    ]
    lipsum[1]
end{document}

enter image description here

One can also use fit to shorten the code a bit. (This even rhymes.;-)

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}}=2cdot 2cdot 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=footnotesizesffamily,OrangeRed] (aux1){base};
        draw[stealth-,Cerulean] (exp) -- ($ (base.north east) + (2ex,2ex) $) 
        node[inner sep=0pt, above right,font=footnotesizesffamily,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}

Answered by user232027 on April 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP