How do you automate drawings like the one below?
These drawings follow some rules. As shown above there is a page with a list of numbered two axes fields. Each field is an individual \begin{tikzpicture}\blank ... \connectlogically\end{tikzpicture}
.
(a) In each field there are horizontal arrows. Each arrow is drawn by the macro \pointX #1,#2|#3|#4|#5
(black) or \pointK #1,#2|#3|#4|#5
(gray). This draws an arrow which start at coordinate (#1,#2)
, is #3
units long (this number is also labeled above the arrow, centered) and below or above there is a dot with coordinates(#1 + 0.5*#3 ,#4)
, and a label #5
to the right of the arrow tip. A definition example of such command is on the MWE.
(b) If possible, two other commands, namely \pointXo
and \pointKo
, should be made. These commands take 4 not 5 arguments. They use as its own x coord
the x coordinate of the arrow tip defined by the last issued \point<K/X>
plus the number placed on the arrow tip label of that last issued \point<K/X>
. This would be equivalent to a coordinate like (#1+#3+#5,<y coord>)
where the arguments are retrived from the last issued \point<K/X>
and the <y coord>
is the first argument of the \point<K/X>o
.
(c) The main issue: lets assume there's an arrow
Ki
at y positionyKi
which its tail is atxKi-1
and tip atxKi-2
. There are also other arrows around this one, namely arrowsKj
, following the same structure. Now, these arrows should be connected by vertical lines to its vertical closer one (min(abs(yKi - yKj))
) that meets this condition:xKi-1 <= xKj-1 < xKi-2
. I words: if there are arrowsj
in which thej
tail is between ani
arrow tail and head, the one closest toi
is verttically connected. The goal is to have a command that does this automatically as when you have lots of arrows and lots of drawings this becomes extremely exhaustive, this command is here referred to as\connectlogically
(d) Ideally, you should also be able to manually instruct LaTeX to draw a connection between line segments of the same color, by giving the starting node coordinates of a pair of steps you want connected. However, this connects their centers by a bent line.
* All lengths and positions can be discrete, if that is easiest to implement. Then integer multiples of 1/8 is all that is required, such as 4 3/4 or -2 1/8.
Below is a MWE with a definition of the \pointX
macro and a test case that covers all (I think) possible scenarios, below it there's the current output and the desired output after issuing the command \connectlogically
.
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\tikzset{dot/.style={circle, fill, minimum size=.4em, outer sep=0pt, inner sep=0pt}}
\def\pointX #1,#2|#3|#4|#5.{%
\draw[black, ->] (#1,#2) -- node[above]{#3} (#1+#3,#2) node[right]{#5} (#1+#3/2,#2+#4) node[dot]{};
}
\def\pointK #1,#2|#3|#4|#5.{%
\draw[gray, ->] (#1,#2) -- node[above]{#3} (#1+#3,#2) node[right]{#5} (#1+#3/2,#2+#4) node[dot]{};
}
\newcount\blankcount
\def\blank{%
\advance\blankcount by 1
\draw[black!64,->](0,0)--node[left]{$x\atop y$} (0,8);
\draw[black!32](0,3)--(16,3)node{};
\draw[black!64,->](0,0)--node[below]{$a\atop b$} (16,0);
\node (x\the\blankcount) at (16,8) {\the\blankcount};
}
\begin{document}
\begin{tikzpicture}
\blank
\pointX 1/2,1|3|1|0.
\pointX 7/2,1/2|2|2|-1/2. % This should be \pointXo 1/2|2|2|-1/2.
\pointX 5,2|2|-1|0. % This should be \pointXo 2|2|-1|0.
\pointX 7,3/2|1|3|0. % This should be \pointXo 3/2|1|3|0.
\pointX 1/2,2|5/2|1|19/4.
\pointK 2,4|2|-2|-3/4.
\pointK 13/4,5|1|-4|0. % This should be \pointKo 5|1|-4|0.
\pointX 31/4,3|3|3|3. % This should be \pointXo 3|3|3|3.
%\connectlogically % This should connect everything automatically
\end{tikzpicture}
\end{document}
EDIT: There are two great answers; 50 (E.D.) and 100 (G.S.) both deserve a bounty.
K
? Are grey only connected to grey and black only to black? Why isn't the 1/2 connected to anything? It overlaps with a line below which is also the same colour (if that's the rule). It would be easier not to useX
which is, after all, rather likex
, ifX
refers to black lines or what have you. (Note: it would be easier for the example even ifX
is standard notation and clear to you given your disciplinary background.) It might help a bit to explain briefly what this is representing so people can try to make sense of it. – cfr Nov 15 '16 at 15:17