
Что я делаю:
Я хочу нарисовать рисунок (как показано ниже), чтобы проиллюстрировать сокращение некоторых интервалов до узлов в линии.
Сначала я определяюnewcommand
интервал for (т.е. \op) с тремя аргументами: начальной точкой, конечной точкой и именем. Затем рисуется линия (или цепочка). Наконец, интервал и соответствующий ему узел в линии соединяются. Весь код: (Примечание:Вы также можете прочитать и изменить общий код по адресуSharedLatex(Прежде чем будет принят какой-либо ответ.)
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc, chains}
\title{Reference Arguments in Newcommand}
\author{hengxin}
\date{31 December 2013}
\begin{document}
\begin{tikzpicture}[]
% new command: inverval
\newcommand{\op}[3] % #1: start point; #2: end point; #3: interval name
{
\coordinate (start) at #1; % start point
\coordinate (end) at #2; % end point
\coordinate (mid) at ($0.5*#1 + 0.5*#2 + (0,0.8cm)$);
\draw[ultra thick, blue, |-|] (start) -- (end); % draw the interval
\node (#3) [font = \huge] at (mid) {#3}; % attach the operation name
}
% define three intervals
\op{(0,2)}{(2,2)}{$a$};
\op{(4,3)}{(6,3)}{$b$};
\op{(7,2)}{(10,2)}{$c$};
% contract them into single nodes in a line (or a chain)
\begin{scope}[font = \huge, start chain = schedule, node distance = 3.0cm, every join/.style = {very thick, ->, red}]
\foreach \opi in {a, b, c}
\node (\opi) [circle, draw, on chain, join, label = {[] below : $\opi$}] {};
\end{scope}
% connect interval and its corresponding node in the line
\begin{scope}[cedge/.style = {->, dashed, draw, thick}]
\draw [cedge] (0,2) to (a);
\draw [cedge] (2,2) to (a);
\draw [cedge] (4,3) to (b);
\draw [cedge] (6,3) to (b);
\draw [cedge] (7,2) to (c);
\draw [cedge] (10,2) to (c);
\end{scope}
\end{tikzpicture}
\end{document}
Что я хочу сделать:
Как вы видите, на последнем шаге (соединение интервала и соответствующего ему узла в линии) я жестко закодировал координаты начальных и конечных точек каждого интервала, например \draw [cedge] (0,2) to (a); \draw [cedge] (2,2) to (a);
. Это, безусловно, утомительно и подвержено ошибкам. Поэтому,
Есть ли какой-нибудь элегантный способ сослаться на две точки (как аргументы
newcommand
) каждого интервала без жесткого кодирования?
решение1
В своей \op
команде вы можете дать имя каждому пределу вашего интервала (используя #3
как часть имени).
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, chains}
\begin{document}
\begin{tikzpicture}
% new command: inverval
\newcommand{\op}[3]{ % #1: start point; #2: end point; #3: operation name
\coordinate (start #3) at #1; % start point
\coordinate (end #3) at #2; % end point
\draw[ultra thick, blue, |-|] (start #3) -- (end #3) % draw the interval
node[pos=.5,above=8mm,font=\huge, text=black] {$#3$}; % attach the operation name
}
% define three intervals
\op{(0,2)}{(2,2)}{a};
\op{(4,3)}{(6,3)}{b};
\op{(7,2)}{(10,2)}{c};
% contract them into single nodes in a line (or a chain)
\begin{scope}[font=\huge, start chain=schedule, node distance=3.0cm,
every join/.style={very thick, ->, red}]
\foreach \opi in {a, b, c} {
\node [circle,draw,on chain,join,label={[]below:$\opi$}] (\opi) {};
}
\end{scope}
% connect interval and its corresponding node in the line
\begin{scope}[cedge/.style = {->, dashed, draw, thick}]
\foreach \interval in {a,b,c}{
\draw [cedge] (start \interval) to (\interval);
\draw [cedge] (end \interval) to (\interval);
}
\end{scope}
\end{tikzpicture}
\end{document}