TeX - LaTeX Asked by vincenzopalazzo on June 9, 2021
I miss my self during a simple process to create an array (programing concept) and make some line under the array to indicate some range.
My actual status if the following
documentclass[letterpaper,12pt]{article}
usepackage{tikz}
newcounter{nodeidx}
setcounter{nodeidx}{1}
usetikzlibrary{arrows.meta,
shapes.multipart}
usetikzlibrary{arrows}
%++++++++++++++++++++++++++++++++++++++++
usetikzlibrary{calc}
newcommand{nodes}[1]{%
foreach num in {#1}{
node[minimum size=6mm, draw, rectangle] (arabic{nodeidx}) at (arabic{nodeidx},0) {num};
stepcounter{nodeidx}
}
}
newcommand{brckt}[4]{% from, to, lvl, text
draw (#1.south west) ++($(-.1, -.1) + (-.1*#3, 0)$) -- ++($(0,-.1) + (0,-#3*1.25em)$) -- node [below] {#4} ($(#2.south east) + (.1,-.1) + (.1*#3, 0) + (0,-.1) + (0,-#3*1.25em)$) -- ++($(0,#3*1.25em) + (0,.1)$);%
}
begin{document}
begin{center}
begin{center}
begin{tikzpicture}
pgftransparencygroup
nodes{2, 4, 3, 1, 6, 7, 8, 9, 1, 7}
endpgftransparencygroup
pgftransparencygroup
brckt{1}{6}{0}{Query(0, 5)}
endpgftransparencygroup
pgftransparencygroup
brckt{4}{10}{0}{Query(3, 9)}
endpgftransparencygroup
end{tikzpicture}
end{center}
end{center}
end{document}
And I want to avoid the overlapping between the line, like in this pictures
A solution plus some tips:
count
from foreach
usetikzlibrary
is enough, keeps everything compacted and ordered.level
is now an optional argument, so you don't need to give it if not needed.documentclass[tikz, margin=10pt]{standalone}
usetikzlibrary{
calc,
%arrows.meta,
%shapes.multipart,
%arrows
}
newcommand{nodes}[1]{%
foreach num [count=n starting from 0] in {#1}{% no need for an external counter
node[minimum size=6mm, draw, rectangle] (nn) at (n,0) {num};
}
}
newcommand{brckt}[4][1]{% [ lvl ] { from, to, text }
coordinate (left) at ($(n#2.south west)+(-2mm,-1mm)$);
coordinate (right) at ($(n#3.south east)+(2mm,-1mm)$);
draw (left) -- ($(left)+(0,-1mm*#1)$) --node[below,midway,font=scriptsize] {#4} ($(right)+(0,-1mm*#1)$) -- (right);
}
begin{document}
begin{tikzpicture}
%pgftransparencygroup
nodes{2, 4, 3, 1, 6, 7, 8, 9, 1, 7}
%endpgftransparencygroup
%pgftransparencygroup
brckt[2]{0}{5}{Query(0, 5)}
%endpgftransparencygroup
%pgftransparencygroup
brckt{3}{9}{~~~~~~~Query(3, 9)}% used ~ to push the label
%endpgftransparencygroup
end{tikzpicture}
end{document}
Correct answer by Alenanno on June 9, 2021
With use of loops, libraries chains
and positioning
, and determining different amplitudes for brackets (brckt
):
documentclass[letterpaper,12pt]{article}
usepackage{tikz}
usetikzlibrary{chains,
positioning}
newcommand{brckt}[4]{% from, to, lvl, text
draw (#1.south west) -- ++(0,-#3) -| node[pos=0.25, below] {#4} (#2.south east); % <---
}
begin{document}
begin{center}
begin{tikzpicture}[
node distance = 2mm,
start chain = going right,
N/.style = {draw, minimum size = 5mm, inner sep=0pt, outer sep=1mm,
below=2mm, on chain} % <---
]
foreach i [count=j] in {2, 4, 3, 1, 6, 7, 8, 9, 1, 7} % <---
node (nj) [N] {i};
brckt{n1}{n6}{1mm}{Query(0, 5)} % <---
brckt{n4}{n10}{2mm}{Query(3, 9)}% <---
end{tikzpicture}
end{center}
end{document}
Answered by Zarko on June 9, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP