TransWikia.com

Drawing automatically updating dashed arrows in tikz

TeX - LaTeX Asked on January 11, 2021

In their answers, "Zarko" and "Lazy squirrel" generously helped me to get started in tikz (I’ve never used it before, and it is daunting to me).

The following question, I know, smacks of lack of effort and warrants a "read the manual" statement. But I am wrapping up my thesis, and it would help me so much if someone would generously assist me with coding the dashed arrows (leftward and upward) indicated in the following image:

arrows

I would like the arrows to make such a perpendicular "path" from the "conventional capacitor" dot to the "supercapacitor" dot. Ideally the arrows would change automatically when I change the locations of those dots — to wherever my advisor decides they should go!

Thanks for considering helping. Here is my MWE:

documentclass[oneside,11pt]{book}

usepackage[semibold,tt=false]{libertine}
usepackage{libertinust1math}
usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
usepackage[subrefformat=parens]{subcaption}
usepackage{tikz}
usetikzlibrary{arrows.meta}
usepackage{siunitx}

begin{document}

begin{figure}[!h]
centering
begin{tikzpicture}[
         > = Stealth,
dot/.style = {circle, fill, minimum size=5pt, inner sep=0pt},
  N/.style = {align=left},
every label/.style = {label distance = 0pt, N},
lbl/.style 2 args={append after command={
(tikzlastnode)node[right=5pt,inner sep=0pt] (tmp){#1}
(tmp.south west)
node[anchor=north west,font=footnotesize,inner sep=0pt]{#2}}}
                        ]
 path[local bounding box=dots] (1,3) node[dot,lbl={battery}{second line}]{}
  (2,2) node[dot,lbl={supercapacitor}{second line}]{}
  (3,1) node[dot,lbl={conventional capacitor}{second line}]{};
%
 draw [<->] ([yshift=1cm]dots.north-|0,0)
  |- ([xshift=5mm]dots.east|-0,0)
   node[pos=0.25, N, above,rotate=90]     {energy density (Wh/kg)}
   node[pos=0.75, N, below]    {power density (W/kg)};
end{tikzpicture}
caption{Caption}
label{fig:ragone}
end{figure}
end{document}

mwe

4 Answers

  • Name the dots such that you can refer to them, e.g. dotsup and dotconv:

    node[dot,lbl={supercapacitor}{second line}](dotsup){}
    node[dot,lbl={conventional capacitor}{second line}](dotconv){}
    
  • To draw a single arrow with a corner: draw[dashed,->] (dotconv) -| (dotsup);

  • To draw two straight arrows, you can refer to the corner point by (dotconv-|dotsup).

    draw[dashed,->] (dotconv) -- (dotconv-|dotsup);
    draw[dashed,->] (dotconv-|dotsup) -- (dotsup);
    

enter image description here

documentclass{standalone}
usepackage{tikz}
usetikzlibrary{arrows.meta}

begin{document}

begin{tikzpicture}[
         > = Stealth,
dot/.style = {circle, fill, minimum size=5pt, inner sep=0pt},
  N/.style = {align=left},
every label/.style = {label distance = 0pt, N},
lbl/.style 2 args={append after command={
(tikzlastnode)node[right=5pt,inner sep=0pt] (tmp){#1}
(tmp.south west)
node[anchor=north west,font=footnotesize,inner sep=0pt]{#2}}}
                        ]
 path[local bounding box=dots] (1,3) node[dot,lbl={battery}{second line}]{}
  (2,2) node[dot,lbl={supercapacitor}{second line}](dotsup){}
  (3,1) node[dot,lbl={conventional capacitor}{second line}](dotconv){};

 draw [<->] ([yshift=1cm]dots.north-|0,0)
  |- ([xshift=5mm]dots.east|-0,0)
   node[pos=0.25, N, above,rotate=90]     {energy density (Wh/kg)}
   node[pos=0.75, N, below]    {power density (W/kg)};
 draw[dashed,->] (dotconv) -- (dotconv-|dotsup);
 draw[dashed,->] (dotconv-|dotsup) -- (dotsup);
end{tikzpicture}
end{document}

Answered by gernot on January 11, 2021

Huh, @gernot was faster (+1) ... I spend some time to rearrange your code and write comments in it, otherwise solution is similar: you need to names nodes which you like to connect with arrow and then simply draw lines between them as you wish:

documentclass[oneside,11pt]{book}

usepackage[semibold,tt=false]{libertine}
usepackage{libertinust1math}
usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
usepackage[subrefformat=parens]{subcaption}
usepackage{tikz}
usetikzlibrary{arrows.meta}
usepackage{siunitx}

begin{document}
    begin{figure}[!ht]
centering
begin{tikzpicture}[
         > = Stealth,
dot/.style = {circle, fill, minimum size=5pt, inner sep=0pt},
  N/.style = {align=left},
lbl/.style 2 args = {append after command={
                (tikzlastnode) node[right=5pt,inner sep=0pt] (tmp){#1}
                (tmp.south west) node[anchor=north west,font=footnotesize,inner sep=0pt]{#2}}}
                        ]
% axes
draw[<->]  (0,5) |-  (7,0)
    node[pos=0.25, N, above,rotate=90]     {energy density (Wh/kg)}
    node[pos=0.75, N, below]    {power density (Wh/kg)};
% nodes
path   (1,3) node (a) [dot,lbl={battery}{second line}]{}                   % aded name (a)
        (2,2) node (b) [dot,lbl={supercapacitor}{second line}]{}            % aded name (b)
        (3,1) node (c) [dot,lbl={conventional capacitor}{second line}]{};   % aded bane (c)
% dashed lines between nodes (c) and (b)
draw[densely dashed, ->] (c) -- (c -| b);
draw[densely dashed,->] (c -| b) -- (b);
end{tikzpicture}
    caption{Caption}
label{fig:ragone}
    end{figure}
end{document}

enter image description here

Answered by Zarko on January 11, 2021

This is the above Zarko's answer after some simplification that may easier for beginners.

enter image description here

documentclass[oneside,11pt]{book}
usepackage{tikz}
usetikzlibrary{arrows.meta}
begin{document}
begin{figure}[!ht]
centering
begin{tikzpicture}[> = Stealth,
dot/.style={circle,fill=blue,minimum size=5pt,inner sep=0pt}]

draw[->] (0,0)--(7,0) node[pos=.5,below]
{energy density (Wh/kg)};
draw[->] (0,0)--(0,5) node[pos=.5,rotate=90,above]
{power density (Wh/kg)};

path   
(1,3) node[dot] (A) {}
(2,2) node[dot] (B) {}
(3,1) node[dot] (C) {};
path[nodes={right=1mm,align=left}]
(A) node{battery[-1.5mm]footnotesize second line}
(B) node{supercapacitor[-1.5mm]footnotesize second line}
(C) node{conventional capacitor[-1.5mm]footnotesize second line};

draw[densely dashed,->,magenta] (C)--(C-|B);
draw[densely dashed,->,magenta] (C-|B)--(B);
end{tikzpicture}
caption{Caption}
label{fig:ragone}
end{figure}
end{document}

Update: we can align the dot and the first text row with this minor change:

enter image description here

path 
(A) 
+(1mm,0) node[right]{battery}
+(1mm,-3.5mm) node[right]{footnotesize second line}
(B) 
+(1mm,0) node[right]{supercapacitor}
+(1mm,-3.5mm) node[right]{footnotesize second line}
(C)
+(1mm,0) node[right]{conventional capacitor}
+(1mm,-3.5mm) node[right]{footnotesize second line}
;

Answered by Black Mild on January 11, 2021

I put, simply the code done with Mathcha that I like very much...

%% Compile and read me!
documentclass[a4paper,12pt]{article}
usepackage[semibold,tt=false]{libertine}
usepackage{libertinust1math}
usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
usepackage[subrefformat=parens]{subcaption}
usepackage{tikz}
usepackage{siunitx}

begin{document}
begin{figure}[!h]
centering
tikzset{every picture/.style={line width=0.75pt}}     
begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
draw [color={rgb, 255:red, 255; green, 0; blue, 255 }  ,draw opacity=1 ] [dash pattern={on 4.5pt off 4.5pt}]  (430.47,241.04) -- (364.52,241.04) ;
draw [shift={(361.52,241.04)}, rotate = 360] [fill={rgb, 255:red, 255; green, 0; blue, 255 }  ,fill opacity=1 ][line width=0.08]  [draw opacity=0] (10.72,-5.15) -- (0,0) -- (10.72,5.15) -- (7.12,0) -- cycle    ;
draw    (240,293) -- (240,69) ;
draw [shift={(240,66)}, rotate = 450] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (10.72,-5.15) -- (0,0) -- (10.72,5.15) -- (7.12,0) -- cycle    ;
draw    (240,293) -- (606.52,293) ;
draw [shift={(609.52,293)}, rotate = 180] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (10.72,-5.15) -- (0,0) -- (10.72,5.15) -- (7.12,0) -- cycle    ;
draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=1 ] (427.43,241.04) .. controls (427.43,239.36) and (428.79,238) .. (430.47,238) .. controls (432.15,238) and (433.52,239.36) .. (433.52,241.04) .. controls (433.52,242.72) and (432.15,244.09) .. (430.47,244.09) .. controls (428.79,244.09) and (427.43,242.72) .. (427.43,241.04) -- cycle ;
draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=1 ] (298.43,161.04) .. controls (298.43,159.36) and (299.79,158) .. (301.47,158) .. controls (303.15,158) and (304.52,159.36) .. (304.52,161.04) .. controls (304.52,162.72) and (303.15,164.09) .. (301.47,164.09) .. controls (299.79,164.09) and (298.43,162.72) .. (298.43,161.04) -- cycle ;
draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=1 ] (358.43,200.04) .. controls (358.43,198.36) and (359.79,197) .. (361.47,197) .. controls (363.15,197) and (364.52,198.36) .. (364.52,200.04) .. controls (364.52,201.72) and (363.15,203.09) .. (361.47,203.09) .. controls (359.79,203.09) and (358.43,201.72) .. (358.43,200.04) -- cycle ;
draw [color={rgb, 255:red, 255; green, 0; blue, 255 }  ,draw opacity=1 ] [dash pattern={on 4.5pt off 4.5pt}]  (361.52,241.04) -- (361.52,207.09) ;
draw [shift={(361.52,204.09)}, rotate = 450] [fill={rgb, 255:red, 255; green, 0; blue, 255 }  ,fill opacity=1 ][line width=0.08]  [draw opacity=0] (10.72,-5.15) -- (0,0) -- (10.72,5.15) -- (7.12,0) -- cycle    ;
% Text Node
draw (338,300) node [anchor=north west][inner sep=0.75pt]   [align=left] {energy density (Wh/kg)};
% Text Node
draw (207.5,252.5) node [anchor=north west][inner sep=0.75pt]  [rotate=-270] [align=left] {power density (Wh/kg)};
% Text Node
draw (309,152) node [anchor=north west][inner sep=0.75pt]   [align=left] {battery};
% Text Node
draw (308,168) node [anchor=north west][inner sep=0.75pt]   [align=left] {{footnotesize second line}};
% Text Node
draw (369,194) node [anchor=north west][inner sep=0.75pt]   [align=left] {supercapacitor};
% Text Node
draw (368,210) node [anchor=north west][inner sep=0.75pt]   [align=left] {{footnotesize second line}};
% Text Node
draw (440,233) node [anchor=north west][inner sep=0.75pt]   [align=left] {conventional capacitor};
% Text Node
draw (439,249) node [anchor=north west][inner sep=0.75pt]   [align=left] {{footnotesize second line}};
end{tikzpicture}
caption{Caption}
label{fig:ragone}
end{figure}
end{document}

enter image description here

Answered by Sebastiano on January 11, 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